Friday, January 26, 2007

NeHe Lesson 03: Adding Color

This lesson adds some color to the dark realm of OpenGL magic. Go read what the Master has to say about color incantations.

I'll show you the whole spell here for your convenience. This lesson adds directly to the previous one.


/**
* The drawing function. Here we will draw the triangle and the quad.
* Now with color.
*/
void drawGLScene()
{
// Clear The Screen And The Depth Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset The Current Modelview Matrix
glLoadIdentity();

// Move Left 1.5 Units And Into The Screen 6.0
glTranslatef(-1.5f, 0.0f, -6.0f);

// Drawing Using Triangles
glBegin(GL_TRIANGLES);
// Set The Color To Red
glColor3f(1.0f, 0.0f, 0.0f);
// Top
glVertex3f(0.0f, 1.0f, 0.0f);

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

// Set The Color To Blue
glColor3f(0.0f, 0.0f, 1.0f);
// Bottom Right
glVertex3f(1.0f, -1.0f, 0.0f);
// Finished Drawing The Triangle
glEnd();

// Move Right 3 Units
glTranslatef(3.0f, 0.0f, 0.0f);

// Set The Color To Blue One Time Only
glColor3f(0.5f, 0.5f, 1.0f);

// Draw A Quad
glBegin(GL_QUADS);
// Top Left
glVertex3f(-1.0f, 1.0f, 0.0f);
// Top Right
glVertex3f(1.0f, 1.0f, 0.0f);
// Bottom Right
glVertex3f(1.0f, -1.0f, 0.0f);
// Bottom Left
glVertex3f(-1.0f, -1.0f, 0.0f);
// Done Drawing The Quad
glEnd();
}


You can download the code from here.