|

OpenGL/ FPC - Chapter 6 - by Delax
Topic in this chapter will be translating. Let me put one thing straight right now: translating has nothing to do with "translating one object into an other". Translating means shifting the object in space, not morphing it.
Imagine a triangle around the center of our matrix ( 0, 0, 0). The vertices would look like this: ( 0,-1, 0); (-1,-1, 0); ( 1,-1, 0);
There are two ways to move this triangle to the right. The first one is to shift the triangle vertices: ( 1,-1, 0); ( 0,-1, 0); ( 2,-1, 0); The other way is to shift the whole matrix to the right and then draw the triangle around the new center of the matrix. And that's a translation.
Translations are done with glTranslate(). You need to give 3 parameters, reassembling the x/ y/ z value the matrix is shifted. The new center of the matrix will be at these coordinates. Here now the source to our little triangle translation.
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.9,0.0,0.0);
glBegin( GL_TRIANGLES );
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
Get the complete code example here. The whole matrix is moved 0.9 units to the right.
With glLoadIdentity(); you do a matrix reset, putting it to it's original coordinates. If you delete the line the matrix would be moved every round (a round is one run of the main loop). And thats about it.
Also, note something else. This time we are using a new color for every vertex. That means that each vertex is drawn in it's own color and then the color "blends" into the color of the next vertex that is connected to it. So our triangle is now colored.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|