Navigation: back up next notes exercises
| glMatrixMode(GL_MODELVIEW) | selects the model-view matrix for subsequent matrix operations |
| glTranslate(x,y,z) | constructs translation matrix and multiply the currently selected matrix with the translation matrix |
| glRotate(th_rad,vx,vy,vz) | constructs rotation matrix and multiply the currently selected matrix with the rotation matrix |
| glScale(x,y,z) | constructs scaling matrix and multiply the currently selected matrix with the scaling matrix |
| glLoadIdentity() | sets the currenlty selected matrix to identity |
| glPushMatrix() | saves the currently selected matrix to the matrix stack |
| glPopMatrix() | sets the currently selected matrix equal to the top entry of the matrix stack and pops the stack |
| glLoadMatrix(... m) | sets the currently selected matrix equal to the t |
| glMultMatrix(... m) | replaces the current matrix with m * current matrix |
Matrix M can be setup as follows:
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef(2.0, 1.0, 0.0);
glRotatef( -3.14/2.0, 0.0, 0.0, 1.0);
glScalef(2.0, 2.0, 2.0);
...
Which produces the matrix M = trans(2,1,0) rot(z,-90) scale(2,2,2) ....
Another way of loading M is to use:
glMatrixMode( GL_MODELVIEW ); glLoadMatrixf( A );
...
Mere, M = A
Yet another way
of constructing a model-view matrix is:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultMatrix(A);
glMultMatrix(B);
Here, M = B A I
The following opengl code draws four instances of a square using geometric transformations. DrawSquare() draws a unit square centered at (0,0), i.e, a square with unit area.
... glMatrixMode(GL_MODELVIEW); /* select the model-view matrix for subsequent matrix operations */ glLoadIdentity(); /* make the model-view matrix identity */ DrawSquare(); /* draw the square 1 */ glTranslatef(5.0, 0.0, 0.0); /* translate along z-axis by -5 units*/ DrawSquare; /* draw the square 2 */ glScalef (2.0, 5.0, 1.0); /* scale. notice that scaling can be different along each dimension */ DrawSquare(); /* draw square 3. notice how it is affected by previous transformations (translation & rotation), so one can combine transformations. */ glLoadIdentity (); /* we need to clear the matrix; otherwise the translation will also apply to the second square */ glScalef (3.0, 2.0, 1.0); /* scale. notice that scaling is different along each dimension */ DrawSquare(); /* draw the square 4 */ ...
Four instances of a square are drawn using the same primitive "square drawing" function by using transformations.

The following code generates the above figure.
... glMatrixMode(GL_MODELVIEW); /* select the model-view matrix for subsequent matrix operations */ glLoadIdentity(); /* model-view matrix = identity */ glMultMatrix(m); /* model-view matrix = m * model-view matrix */ DrawLink(); /* draws Link 1 */ glPush(); /* saves the model-view matrix */ glTranslate(x1,y1,z1); /* model-view = translation * model_view */ DrawLink(); /* draws Link 2 */ glRotation(th_rad,vx1,vy1,vz1); /* model-view = rotation * model-view */ glTranslation(x2,y2,z2); /* model-view = translation * model_view */ DrawLink(); /* draws Link 4 */ glPop(); /* model-view matrix = identity */ glPush(); /* again save model-view matrix*/ glRotation(th_rad,vx2,vy2,vz2); /* model-view = rotation * model-view */ glTranslation(x3,y3,z3); /* model-view = translation * model_view */ DrawLink(); /* draws Link 3 */ glPop(); /* model-view matrix = identity */ ...
Also see 3D Geometric Transforms
Translation, rotation, scaling, and shearing are all examples of geometric transformations. Multiple copies of an object can be created by drawing the same object using a series of different transformation matrices.

(image courtesy of Robert Lansdale)
A transformation that preserves the parallelism of lines, but not necessarily angles or lengths.
Suppose we transform an object (by translation, scale, rotation, shear) then a point p(x,y) of the object will be moved another point p'(x',y') of the transformed object. We want to derive the relationship between p' and p.

translation |
trans(a,b): |
scaling |
scale(a,b): |
shear |
shearx(a): sheary(a): |
rotation |
Imagine z-axis coming out of the paper. We define counter clock-wise rotation about z-axis as positive rotation and clock-wise rotation about z-axis as negative, i.e., we are following the right hand convention. If you align your right thumb with the positive z-axis (coming out of the page), the direction of the curl of your fingers is positive rotation. rot(zaxis,angle_rad): |

The transformations defined above can be rewritten using 3x3 matrices and homogeneous coordinates. The general form of the matrix is:

The expressions derived earlier correspond to the following transformation matrices.
| Translation Matrix | |
| Scaling Matrix | ![]() |
| Rotation Matrix | ![]() |
A series of transformations can be accumulated into a single transformation matrix. Suppose our object is defined as follows:
Suppose we wish to place the object in our scene like this:
This can be accomplished in several ways, one of which is:
Substituting (1) in (2) gives:

Consider building the following model of a hand with one finger:
This can be constructed using a transformation hierarchy. In the following scene graph, circles represent transformations and squares represent geometry. The pseudocode on the left can be used to draw the scene.
f1: trans(d_hand,0) rot(z,th1) M=M*Thand |
![]() |
Now consider drawing a hand with three identical fingers. We can create a more complex scene graph which uses multiple instances of a finger scene graph, as shown below. Because each of the fingers is defined relative to the hand coordinate system, a way is needed to restore the hand coordinate system before beginning to draw each finger. This is done through the pushMatrix() and popMatrix() function calls.
|
M=M*Thand draw_finger() { |
![]() |
Many graphics systems maintain a stack for the current transformation matrix:

An affine transformation can be completely specified by its action on three points. What is the matrix for the transformation that maps the points A(0,0) B(1,0) and C(1,0) to the points A'(1,2) B'(1,1) and C'(2,1) respectively?
Hint: 2D rotation, scaling, shear and translation are all affine transformations, of the form
p' = Mp, where p=[x,y,1]T and M is a 3x3 matrix of the form: |
|
Assuming two-dimensional transformations, show that the following pairs of operations do or do not commute:
Reflections: Assume a two-dimensional space.
Show that rot(z,theta1) rot(z,theta2) = rot(z,theta1 + theta2)
Find the inverse of the following transformation matrix, assuming that the vectors A, B, and C are an orthonormal set. (Hint: there is a 'clean' solution).
[ Ax Bx Cx Tx ] [ Ay By Cy Ty ] [ Az Bz Cz Tz ] [ 0 0 0 1 ]