25. OpenGL Vertex Coloring (Version 2.0)

Introduction

Vertex coloring is a simple yet effective way to enhance the visual appeal of your OpenGL applications. By assigning colors to individual vertices of a shape, you can create gradients and dynamic color effects. This technique is commonly used in both 2D and 3D graphics to add depth and variety to objects.

In this tutorial, you’ll learn how to apply colors to specific vertices, creating a blended effect across a quad. We’ll also discuss how this method can be applied to other shapes.

What is Vertex Coloring?

Vertex coloring involves assigning a color to each vertex of a shape. When OpenGL renders the shape, it interpolates the colors between vertices, creating smooth gradients. This allows you to:

  • Create colorful transitions and effects.
  • Highlight specific parts of an object.
  • Enhance visual depth and realism.

In OpenGL, vertex colors are defined using the glColor3f function, which specifies the red, green, and blue components of a color. These colors are applied immediately to the subsequent vertex specified with glVertex3f.

Steps to Add Vertex Colors

Let’s go through the process of applying vertex colors to a quad.

  1. Begin defining the shape with glBegin().
  2. Set the color for the first vertex using glColor3f().
  3. Specify the vertex position using glVertex3f().
  4. Repeat for each subsequent vertex, assigning a different color to each one.
  5. End the shape with glEnd().

Here’s an example:

  1. glBegin(GL_QUADS); // Start defining the quad  
  2. glColor3f(1, 0, 0); // Red  
  3. glVertex3f(-0.5, -0.5, 0.0); // Bottom-left corner  
  4.   
  5. glColor3f(0, 1, 0); // Green  
  6. glVertex3f(-0.5, 0.5, 0.0); // Top-left corner  
  7.   
  8. glColor3f(0, 0, 1); // Blue  
  9. glVertex3f(0.5, 0.5, 0.0); // Top-right corner  
  10.   
  11. glColor3f(1, 1, 1); // White  
  12. glVertex3f(0.5, -0.5, 0.0); // Bottom-right corner  
  13. glEnd(); // Finish the quad  

Blending Colors Across the Shape

When OpenGL renders this quad, it automatically blends the colors between vertices. This creates a gradient effect, with red transitioning to green, green to blue, and so on.

Applying to Other Shapes

Vertex coloring isn’t limited to quads. You can apply it to any shape, including triangles, polygons, and even 3D models. The principle remains the same: assign a color to each vertex and let OpenGL handle the interpolation.

Tutorial Code

Here’s the complete code for rendering a colored quad in OpenGL:

  1. #include <GL/gl.h>  
  2. #include <GL/glut.h>  
  3.   
  4. // Function to draw the colored quad  
  5. void square(void) {  
  6.     glBegin(GL_QUADS); // Start defining the quad  
  7.     glColor3f(1, 0, 0); // Red  
  8.     glVertex3f(-0.5, -0.5, 0.0); // Bottom-left corner  
  9.   
  10.     glColor3f(0, 1, 0); // Green  
  11.     glVertex3f(-0.5, 0.5, 0.0); // Top-left corner  
  12.   
  13.     glColor3f(0, 0, 1); // Blue  
  14.     glVertex3f(0.5, 0.5, 0.0); // Top-right corner  
  15.   
  16.     glColor3f(1, 1, 1); // White  
  17.     glVertex3f(0.5, -0.5, 0.0); // Bottom-right corner  
  18.     glEnd(); // Finish the quad  
  19. }  
  20.   
  21. // Display callback function  
  22. void display(void) {  
  23.     glClearColor(0.0, 0.0, 0.0, 1.0); // Set background to black  
  24.     glClear(GL_COLOR_BUFFER_BIT);     // Clear the color buffer  
  25.     glLoadIdentity();                 // Reset transformations  
  26.   
  27.     glTranslatef(0, 0, -1);           // Move the quad into view  
  28.     square();                         // Draw the quad  
  29.     glFlush();                        // Render the scene  
  30. }  
  31.   
  32. // Reshape callback function  
  33. void reshape(int w, int h) {  
  34.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);  
  35.     glMatrixMode(GL_PROJECTION);  
  36.     glLoadIdentity();  
  37.     gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);  
  38.     glMatrixMode(GL_MODELVIEW);  
  39. }  
  40.   
  41. // Main function  
  42. int main(int argc, char **argv) {  
  43.     glutInit(&argc, argv);                      // Initialize GLUT  
  44.     glutInitDisplayMode(GLUT_SINGLE);              // Single-buffered display  
  45.     glutInitWindowSize(500, 500);                  // Set window size  
  46.     glutInitWindowPosition(100, 100);              // Set window position  
  47.     glutCreateWindow("Vertex Coloring Example");   // Create the window  
  48.     glutDisplayFunc(display);                      // Register display callback  
  49.     glutReshapeFunc(reshape);                      // Register reshape callback  
  50.     glutMainLoop();                                // Enter the event loop  
  51.     return 0;  
  52. }  

Tips for Experimentation

  • Change the colors in glColor3f to see different effects.
  • Try adding more vertices to create complex shapes with gradients.
  • Experiment with 3D shapes like cubes or spheres for dynamic visuals.

If you have any questions or run into issues, feel free to email me at swiftless@gmail.com. Happy coding!

  • March 25, 2010