Home Basic OpenGL functions Translate and Rotate functions
Translate and Rotate functions

Lets say you have a point in space (x,y,z). You want to translate it 10 units on x, 20 on y and 30 on z. What are the new coordinates? Well obviously (x+10,y+20,z+30). You don't need to be a rocket scientist to calculate that, but what about rotating that point 30 degrees on X axis? Now that's hard question. So we do need to do the math by the book. The books says that applying a 3D transformation to a point means multiplying the point (the vector actually) with a transformation matrix. These are the most basic matrices (check out the translation matrix - it was intuitive enough to do it in the head) :

 

Transformation matrixes

 

OpenGL maintains a stack of matrices. The current matrix is the top of the stack, and every time you use a matrix transformation function it is applied to the current matrix. That stack it's self is only modified when you explicitly push or pop from it.

The following functions provide basic transformations on the matrix:

  • glTranslate(x, y, z);
  • glRotate (angle, x, y, z);
  • glScale (x, y, z);


The calculations and formulas behind these transformations are pretty complicated to explain and you are probably not interested in them anyway. Just as an example multipling the current matrix with the following matrix would result in a 3D rotation with alfa degrees on x, beta on y, gama on z and a translation with (xt,yt,zy) of everything draw afterwards:

 

3D Rotation matrix

 

Don't panic...you don't need to understand the math behind the transformation. What I described above can be done opengl with the following 4 operations:

glRotate(gama,0,0,1);
glRotate(beta,0,1,0);
glRotate(alfa,1,0,0);
glTranslate(xt,yt,zt);

One thing you should be carefully of is that the order in matrix multiplication matters so the order in which you call these functions obviously matters too.

When you call glRotate(angle,1,0,0) you tell opengl to rotate everything you are going to draw next by the given angle on the X axis. Why X axis? The rotation center is always considered (0,0,0) so the direction (1,0,0) points on the X axis.

So before doing a rotation you have to translate your self in the rotation origin and only then apply to rotation.

So lets say you what to draw an object at (3,3,3) and you want to rotate it 30 degrees on Y around the (1,1,1) point. Initially you are in (0,0,0) so this is what you would do:

glTranslate(1,1,1); // Translate by 1 on x, 1 on y and 1 on z to get to the rotation center
glRotate(30,0,1,0); // Apply the rotation. The origin is now in (1,1,1)
glTranslate(2,2,2); // Translate by 2 on each axis to from (1,1,1) to (3,3,3).
....draw your stuff now ....

Remember that applying these transformations only modifies the matrix that is being used to draw things and will not alter anything that is already on the screen! Everything you draw after the transformation function calls will use you modified matrix and will be rotated and translated as you intended.

To get back to you original coordinate system you would have to apply the inverse transformation in reverse order. You you rotate 30 degrees on Y then you'd have to rotate -30 on Y to get back. In the example above we did 3 transformations and it's already getting complicated to reverse. That's where the matrix stack comes in. Before messing with the matrix you could save it (push it on the stack) and after you are done with it you can get original matrix back with only one Pop. The following function push and pop the matrix:

glPushMatrix() copies the matrix on the top of the stack. This would be like a Save function

glPopMatrix() would be the equivalent of ‘load’. This gets rid of the top matrix in the stack, and sets the matrix underneath it as the new current matrix. So, a glPopMatrix() could undo immediately the result of several or even hundreds of transformations.

Now we can also explain what that glLoadIdentity() we used in all our example until now does. It simply loads that identity matrix (1's on the main diagonal and 0 in the rest) as the current transformation matrix. Multiplying anything with the identity matrix means it remains unchanged right? Looks like a good point to start with :D
 
Copyright © 2010 Tudor Carean. All Rights Reserved.