Professional Documents
Culture Documents
OpenGL Video Tutorial - Color
OpenGL Video Tutorial - Color
php
OpenGL tutorial
Home Intro Forums Reference
Lesson 3: Color
Watch the video now!
Summary * Text version * Exercises * Download source
Solid Coloring
Now let's add a little color to our program. We'll start with the code from the previous lesson, with some of the comments
removed. First, we add glEnable(GL_COLOR_MATERIAL) to the end of initRendering, in order to enable colors. Let's make a
couple of changes to drawScene.
//Trapezoid
glVertex3f(-0.7f, -0.5f, 0.0f);
glVertex3f(0.7f, -0.5f, 0.0f);
glVertex3f(0.4f, 0.5f, 0.0f);
glVertex3f(-0.4f, 0.5f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(1.0f, 1.0f, 0.0f);
glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glScalef(0.7f, 0.7f, 0.7f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.75f, 0.0f);
//Pentagon
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f, 1.0f, 0.0f);
glRotatef(_angle, 1.0f, 2.0f, 3.0f);
//Triangle
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glEnd();
1 of 4 9/25/2007 11:04 AM
OpenGL Video Tutorial - Color http://www.videotutorialsrock.com/opengl_tutorial/color/text.php
The changes are simple enough. We just added two calls to glColor3f. Whenever we call glColor3f, we change the current
color to the indicated RGB color. Everything we draw afterwards is drawn using the new current color.
RGB is a very common way to represent colors on computers. Using the RGB system, we specify each color as a combination of
red, blue, and green light components, where each component ranges from 0 to 1. If you're not familiar with RGB, I recommend
that you look it up online and become familiar with it.
Note that unlike with transformation functions, we can can glColor3f between glBegin-glEnd blocks; this is what we do in
the second call to glColor3f.
Color Blending
Now, let's do a little color blending. We'll make the following changes to the code:
glBegin(GL_QUADS);
//Trapezoid
glColor3f(0.5f, 0.0f, 0.8f);
glVertex3f(-0.7f, -0.5f, 0.0f);
glColor3f(0.0f, 0.9f, 0.0f);
glVertex3f(0.7f, -0.5f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.4f, 0.5f, 0.0f);
glColor3f(0.0f, 0.65f, 0.65f);
glVertex3f(-0.4f, 0.5f, 0.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(1.0f, 1.0f, 0.0f);
glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glScalef(0.7f, 0.7f, 0.7f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.75f, 0.0f);
//Pentagon
glVertex3f(-0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
2 of 4 9/25/2007 11:04 AM
OpenGL Video Tutorial - Color http://www.videotutorialsrock.com/opengl_tutorial/color/text.php
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f, 1.0f, 0.0f);
glRotatef(_angle, 1.0f, 2.0f, 3.0f);
glBegin(GL_TRIANGLES);
//Triangle
glColor3f(1.0f, 0.7f, 0.0f);
glVertex3f(0.5f, -0.5f, 0.0f);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);
glEnd();
Pretty cool, huh? We can use a different color for each vertex, and OpenGL will automatically blend smoothly between the colors
of the different vertices.
3 of 4 9/25/2007 11:04 AM
OpenGL Video Tutorial - Color http://www.videotutorialsrock.com/opengl_tutorial/color/text.php
4 of 4 9/25/2007 11:04 AM