25. OpenGL Vertex Coloring
This OpenGL tutorial will show you how to add colors to specific vertices in your shape. It uses the same color commands but the code is now written just before each vertex.
Vertex coloring is nothing really special
it is just normal coloring, only setting the colors
to specific vertices instead of whole objects.
This then blends the colors into each other
forming a nice little rainbow type thing
with the colors I have chosen.
So anyway, I did this with a normal quad
but it does work with any shape you choose
Here it is:
I have started my quad.
I then set the color red for the first vertex.
I then draw that vertex.
The next is green.
Then blue.
And finally white.
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex3f(-0.5, -0.5, 0.0);
glColor3f(0,1,0);
glVertex3f(-0.5, 0.5, 0.0);
glColor3f(0,0,1);
glVertex3f(0.5, 0.5, 0.0);
glColor3f(1,1,1);
glVertex3f(0.5, -0.5, 0.0);
glEnd();
Just remember to call each vertex after you call the color
for this to work.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. |
//Vertex coloring is nothing really special //it is just normal coloring only setting the colors //to specific vertices instead of whole objects. //This then blends the colors into each other //forming a nice little rainbow type thing //with the colors I have chosen. //So anyway, I did this with a normal quad //glBegin(GL_QUADS); //Just remember to call each vertex after you call the color #include <GL/gl.h> void square (void) { void display (void) { void reshape (int w, int h) { int main (int argc, char **argv) {
|