| |||||||||||
|
Main Menu Other |
Tutorials -- Drawing Triangles | ||||||||||
|
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:
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).
|
|||||||||||