|

OpenGL/ FPC - Chapter 7 - by Delax
With the knowledge of transformations it's just a small step to rotation. With a rotation, the whole matrix gets rotated around one or multiple axis.
Picture our 3D coordinate system. There is the X axis, going horizontally left and right. There is the Y axis, running vertically up and down. And the Z axis, going in and out of our screen. Now we rotate the system around the X axis. The Y axis is moving towards us until it points in and out of the screen. The Z axis is now pointing up and down. That would be a rotation 90 degrees around the X axis.
As we can see glRotatef() is called with 4 parameters. The first one is the angle of our rotation. The others are X7 Y/ Z values of the vector from which we will rotate.
Now you could rotate the matrix every time the main loop is called for the same amount. If we would not reset the matrix, it would be at it's last position and a steady rotation would be the result. Rotate a little, draw, rotate a little, draw, ... quite simple to understand. But as the complexity of the scenes/ objects increase, we will have to reset the matrix eventually. So here comes another way.
We reset the matrix every round and use an increasing variable. So it would first rotate a certain degree, then the degree gets bigger every time the main loop is called. And there we have a steady rotation. It does the same, but is another way of accomplishing it.
Well, it sounds complicated, but with a little bit of playing around, it gets clear. So here is an example of rotating a matrix around it's Y axis.
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glRotatef( rotation, 0.0, 1.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 source code here. Just take a look at it, pop some new values in the rotations and see what happenes.
Delax/ Sundancer Inc.
[delax@sundancerinc.de]
Back to previous page
|