You Like My Tutorials? Click The Banner To Keep 'Em Coming!

glGameDeveloper(): The Game Developer's Source for OpenGL Coding
Main Menu
Latest News
Archived News
Tutorials
Links
E-Mail Me

Other
Message Board
My Guestbook

Tutorials -- Drawing and Coloring Primitives
 


Drawing and Coloring Primitives in OpenGL Page 1 of 3
< Previous Jump to Page: 1 | 2 | 3 Next >

Drawing and Coloring Primitives in OpenGL

This tutorial follows the previous tutorial on how to draw a triangle. If you haven't done that tutorial yet, it's best to read that one first.

We only drew one triangle last time, so this time we'll go two steps ahead. First lets draw the triangle in different colors.

Go back to our code where we have the lines that actually draw the triangle.

glBegin(GL_POLYGON);
	glNormal3f( 1, 0, 0);
	glVertex3f( 0,  1, 0);
	glVertex3f(-1, -1, 0);
	glVertex3f( 1, -1, 0);
glEnd();

Delete this code and change it to the following code. Lets make one corner of the triangle red, one blue, and one green (from now on we'll refer to this as simply rgb). This way we can see how to use each of these colors separately. You can also combine them to make other colors, and also use floating point (0.5, 0.75, 0.3333, etc.) values to represent the amount of each color to use.

glBegin(GL_POLYGON);
	glNormal3f( 1, 0, 0);
	
	glColor4f(1, 0, 0, 1);	glVertex3f( 0,  1, 0);
	glColor4f(0, 1, 0, 1);	glVertex3f(-1, -1, 0);
	glColor4f(0, 0, 1, 1);	glVertex3f( 1, -1, 0);
glEnd();

The function glColor4f() lets us tell OpenGL what the current color should be. It takes 4 parameters (because we called glColor4f and not glColor3f): red green, blue, and alpha. Alpha is how transparent/opaque the current color is. For now we'll leave alpha to be 1, which means no transparency.

Note that you can call glColor each time you draw a vertex. This lets us draw each vertex a certain color, rather than the whole triangle. Also note how OpenGL does a fade between each color, which in the end looks very nice.

Play around with this until you get the hang of drawing vertices in different colors. Then move on and we'll draw something in 3d.

Drawing and Coloring Primitives in OpenGL Page 1 of 3
< Previous Jump to Page: 1 | 2 | 3 Next >

This site best viewed with MS Internet Explorer 5.0 at 16-bit color and at least 800x600.

All Contents of this site are Copyright 1999, 2000 Joe Zimmerman. No part of this site (code, HTML, etc.) may be reproduced without my permission.

Click Here!