In the last tutorial I taught you how to display Polygons and Quads on the screen. In this tutorial I will teach you how to add 2 different types of coloring to the objects. Flat coloring makes a polygon or quad one solid color. Smooth coloring blends colors specified at each point (vertex) together, creating a nice looking blend of colors.

Using the code from the last tutorial, we will be adding to the DrawGLScene procedure. I will rewrite the entire procedure below, so if you plan to modify the last lesson, you can replace the DrawGLScene procedure with the code below, or just add code to the DrawGLScene procedure that is not already in the last tutorial.

GLvoid DrawGLScene(GLvoid)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glTranslatef(-1.5f,0.0f,-6.0f);

	glBegin(GL_POLYGON);

If you remember from the last tutorial, this is the section of code to draw the triangle on the left half of the screen. The next line of code will be the first time we use the command glColor3f. The three parameters in the brackets are red, green and blue intensity values. The values can be from 0.0f to 1.0f. It works the same way as the color values used to clear the background of the screen.

We are setting the color to red (full red intensity, no green, no blue). The first vertex at the top of the triangle is the next line of code, and will be drawn using the current color which is red. Anything we draw from now on will be red until we change the color to something other than red.

		glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
		glVertex3f( 0.0f, 1.0f, 0.0f);

We've placed the first vertex on the screen, setting it's color to red. Now before we place the second vertex we'll change the color to green. That way the second vertex which is the left corner of the triangle will be set to green.

		glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green
		glVertex3f(-1.0f,-1.0f, 0.0f);

Now we're on the third and final vertex. Just before we draw it, we set the color to blue. This will be the right corner of the triangle. As soon as the glEnd() command is issued, the polygon will be filled in. But because it has a different color at each vertex, rather than one solid color throughout, the color will spread out from each corner, eventually meeting in the middle, where the colors will blend together. This is smooth coloring.

		glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
		glVertex3f( 1.0f,-1.0f, 0.0f);
	glEnd();

	glTranslatef(3.0f,0.0f,0.0f);

Now we will draw a solid blue colored square. It's important to remember that anything drawn after the color has been set will be drawn in that color. Every project you create down the road will use coloring in one way or another. Even in scenes where everything is texture mapped, glColor3f can still be used to tint the color of textures, etc. More on that later.

So to draw our square all one color, all we have to do is set the color once to a color we like (blue in this example), then draw the square. The color blue will be used for each vertex because we're not telling OpenGL to change the color at each vertex. The final result... a blue square.

	glColor3f(0.5f,0.5f,1.0f);				// Set The Color To Blue One Time Only
	glBegin(GL_QUADS);
		glVertex3f(-1.0f, 1.0f, 0.0f);
		glVertex3f( 1.0f, 1.0f, 0.0f);
		glVertex3f( 1.0f,-1.0f, 0.0f);
		glVertex3f(-1.0f,-1.0f, 0.0f);
	glEnd();
}

In this tutorial I have tried to explain in as much detail, how to add flat and smooth coloring to your OpenGL polygons. Play around with the code, try changing the red, green and blue values to different numbers. See what colors you can come up with. If you have comments or questions please email me. If you feel I have incorrectly commented something or that the code could be done better in some sections, please let me know. I want to make the best OpenGL tutorials I can. I'm interested in hearing your feedback.

Jeff Molofee (NeHe)

* DOWNLOAD Visual C++ Code For This Lesson.
* DOWNLOAD Delphi Code For This Lesson. ( Conversion by Brad Choate )
* DOWNLOAD Linux Code For This Lesson. ( Conversion by Richard Campbell )
* DOWNLOAD BeOS Code For This Lesson. ( Conversion by Chris Herborth )