Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

OpenGL Video Tutorial - Color http://www.videotutorialsrock.com/opengl_tutorial/color/text.

php

OpenGL tutorial
Home Intro Forums Reference

Lesson 3: Color
Watch the video now!
Summary * Text version * Exercises * Download source

Graphical Programming Free Tutorial Download Learn NLP at Home


Powerful, intuitive programming w/ Over 750 free programming language Learn from the Experts! Neurosemantics and
LabVIEW--online tutorials & evals tutorials, lessons, and how-to's. NLP DVDs and CDs
www.ni.com programmingtutorials.org www.nlp-video.com

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.

glColor3f(0.5f, 0.0f, 0.8f);


glBegin(GL_QUADS);

//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);

glVertex3f(-0.5f, 0.0f, 0.0f);


glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);

glVertex3f(-0.5f, 0.0f, 0.0f);


glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);

glEnd();

glPopMatrix();
glPushMatrix();
glTranslatef(-1.0f, 1.0f, 0.0f);
glRotatef(_angle, 1.0f, 2.0f, 3.0f);

glColor3f(0.0f, 0.65f, 0.65f);


glBegin(GL_TRIANGLES);

//Triangle
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.0f, 0.5f, 0.0f);
glVertex3f(-0.5f, -0.5f, 0.0f);

glEnd();

After we make these changes, our program looks like this.

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.

Downloadable Macro Books Computer Scrapbooking Pivot Table Tutorial


Get over 1200 Excel visual basic macro Free eBook & how-to tutorial movies Computer Patterns in data instantly emerge with our
examples with explanations Scrapbooking is now easy! software. Free download
www.add-ins.com/macro_examples www.ScrapGirls.com www.NumberGo.com

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);

glVertex3f(-0.5f, 0.0f, 0.0f);


glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, 0.0f, 0.0f);

glVertex3f(-0.5f, 0.0f, 0.0f);


glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.5f, 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();

Here's what our program looks like now:

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.

Setting the Background Color


Just one more thing. Let's change the background color from black to sky blue. To do this, we just add a call to
glClearColor(0.7f, 0.9f, 1.0f, 1.0f) to the end of initRendering. The first three parameters are the RGB color of
the background. We just put 1 for the last value; you don't have to worry about what it means. Now we have the following:

3 of 4 9/25/2007 11:04 AM
OpenGL Video Tutorial - Color http://www.videotutorialsrock.com/opengl_tutorial/color/text.php

That's it! Adding color is pretty straightforward.

Next is "Lesson 4: Lighting".

Summary * Text version * Exercises * Download source

Free Hardware Books 3D Graphing: Interactive Nikon, Canon User Guides


Find hardware books (pdf format) free Easy to use 3D Graphing Calculator. Rotate Tutorial DVDs & original Manuals at
download ebooks. your graph interactively! CameraBooks.com.
hardwarebooks.net www.webgraphing.com www.CameraBooks.com

Home | Intro | Forums | Quick Reference | Links | About | Contact

© 2007 Bill Jacobs


"Rock on!" - Bill

4 of 4 9/25/2007 11:04 AM

You might also like