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 Triangles
 


Drawing Triangles in OpenGL Page 4 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 Next >

The Code (con't)

Finally, lets set the color, position our triangle, and draw it.

glColor4f(1, 1, 1, 1);

rotate++;

glTranslatef(0, 0, -10);
glRotatef(rotate, 0, 1, 0);
glBegin(GL_POLYGON);
	glNormal3f( 1, 0, 0);
	glVertex3f( 0,  1, 0);
	glVertex3f(-1, -1, 0);
	glVertex3f( 1, -1, 0);
glEnd();

First let me explain how OGL functions are set up. They all start with "gl". Then you have the name of what the function does. Some functions, if they have more than one parameter, will then have a number stating how many parameters you want to send it. Finally, the function may be ended with a "f", "d", or "i", depending if you're passing a "float", a "double", or an "integer" variable for each parameter.

In the above code, we first set the current OpenGL color to white. I'll have another tutorial on working with colors very soon.

The line "rotate++" increases our rotation amount. The next line moves the triangle out in front of us so we can see it. This is done by "translating" on the Z-Axis with a negative value. You can change this to move the triangle closer or farther away.

The glRotatef command rotates the triangle. It uses our rotate variable. The next three parameters tell OGL whether or not to rotate on each axis. Putting 0 means to not rotate, and 1 means to rotate. In this case, we're rotating on the Y axis to spin it horizontally.

glBegin() must be called before each time you render a point, line, or polygon. In our code, we're rendering a GL_POLYGON. Here's a list of other things you can render:

  • GL_POINTS
  • GL_LINES
  • GL_LINE_LOOP
  • GL_LINE_STRIP
  • GL_TRIANGLES
  • GL_TRIANGLE_STRIP
  • GL_TRIANGLE_FAN
  • GL_QUADS
  • GL_QUAD_STRIP
  • GL_POLYGON

Play around with each one and see what happens. Some might result in the same effect, others might make things look wierd.

The glVertex3f() commands tell OpenGL where we want to render vertices of a polygon, line, or points. Play around with these too to get a feel for rendering objects in 3d.

That's it for the triangle tutorial. As always, let me know if you have any problems or questions, and stay tuned for the next tutorial(s).

Drawing Triangles in OpenGL Page 4 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 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!