Menu
Home
DS
PC
Tutorials
Links
Contact


Affiliates
NeHe 3 - Vertex Colors

 

Finally, we get to add color to the triangle and quad from the previous lesson.

Lesson 3 Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int DrawGLScene()								// Here's Where We Do All The Drawing
{
	glLoadIdentity();								// Reset The Current Modelview Matrix
	glTranslatef(-1.5f,0.0f,-6.0f);				// Move Left 1.5 Units And Into The Screen 6.0
	glBegin(GL_TRIANGLES);						// Drawing Using Triangles
		glColor3f(1.0f,0.0f,0.0f);					// Set The Color To Red
		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top

		glColor3f(0.0f,1.0f,0.0f);					// Set The Color To Green
		glVertex3f(-1.0f,-1.0f, 0.0f);			// Bottom Left
		glColor3f(0.0f,0.0f,1.0f);					// Set The Color To Blue
		glVertex3f( 1.0f,-1.0f, 0.0f);			// Bottom Right
	glEnd();								// Finished Drawing The Triangle
	glTranslatef(3.0f,0.0f,0.0f);						// Move Right 3 Units

	glColor3f(0.5f,0.5f,1.0f);						// Set The Color To Blue One Time Only
	glBegin(GL_QUADS);							// Draw A Quad
		glVertex3f(-1.0f, 1.0f, 0.0f);					// 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

	glEnd();								// Done Drawing The Quad
	return TRUE;
}

Analyze:

Line 6: glColor3f(1.0f, 0.0f, 0.0f);
this creates a red diffuse color for everything rendered after this call. glColor3f( R,G,B ) has a maximum value of 1.0f, actually, it does (r,g,b) % 1 for each, compared to 15-bit(31) and 24-bit(255). If you have a texture applied (covered in a different lesson), you can use this to hint the colors of the texture, i.e. make a greyscale image and use glColor3f to give it the real color (Halo uses diffusion).
To make it so it does not modify the currently applied texture, call glColor3f(1,1,1);

With other glColor3f() functions called, the colors will blend together.


After you've had your fun with colors, check out the next lesson: Translations and Rotations