Adding Colour
 
Last Update - 01/01/2005

Colouring the Polygon

In this example, we will add to the basic setup from the first tutorial.
To colour our polygon, we simply specify a colour using glColor3f() before drawing the vertices. The function takes three parameters, a red, green and blue value.
glColor has many variants, some take four values and cover RGB and alpha values. Other versions take integer or double arguments. The float version used here is the one that you will probably see most commonly in OpenGL coding.


  glBegin(GL_TRIANGLES); // Draw our Triangle with coloured vertices      

   glColor3f(1.0f,0.0f,0.0f); 
   glVertex3f( 0.0f, 0.8f,0.0f);
    
   glColor3f(0.0f,1.0f,0.0f); 
   glVertex3f( 0.7f,-0.6f,0.0f);
    
   glColor3f(0.0f,0.0f,1.0f); 
   glVertex3f(-0.7f,-0.6f,0.0f);

  glEnd();

If we specify a colour with a single call to glColor3f(), that colour will be used for all subsequent drawing operations. In this example we specify a different colour for each vertex, the result is a smoothly blended transition between the colours.

Single colour specified Blended colour (One colour per vertex)

There are still problems with the code. The polygon distortion will be addressed in the next tutorial.

Get the source and Win32 binary Here

Codehead - 01/01/2005

Problems, ideas, better routines ?
Mail me or drop into the forums


Home EMail Forum