Contents / Previous / Next


The Order of Transformation Matrices

Now let's talk about the order in which you specify a series of transformations. All viewing and modeling transformations are represented as 4x4 matrices. Each successive glMultMatrix*() or transformation command multiplies a new 4x4 matrix M by the current modelview matrix C to yield CM. Finally, vertices v are multiplied by the current modelview matrix.

This process means that the last transformation command called in your program is actually the first one applied to the vertices: CMv.

Thus, if you like to think in terms of a grand, fixed coordinate system - in which matrix multiplications affect the position, orientation, and scaling of your model - you have to think of the multiplications as occurring in the opposite order from how they appear in the code.

Example: A rotation R about the origin and a translation T along the x-axis.
If you want the object to appear on the axis after the operations, the rotation must occur first, followed by the translation:

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 glMultMatrixf(T);                     /* translation */
 glMultMatrixf(R);                     /* rotation */
 draw_the_object();

Another way to view matrix multiplications is to forget about a grand, fixed coordinate system in which your model is transformed and instead imagine that a local coordinate system is tied to the object you're drawing. All operations occur relative to this changing coordinate system. With this approach, the matrix multiplications now appear in the natural order in the code.
To see this in the translation-rotation example, begin by visualizing the object with a coordinate system tied to it. The translation operation moves the object and its coordinate system down the x-axis. Then, the rotation occurs about the (now-translated) origin, so the object rotates in place in its position on the axis.
This second approach can be problematic, however, in cases where scaling occurs.

Regardless of which analogy you are using, the code is the same, but how you think about it differs.

You normally issue viewing transformation commands in your program before any modeling transformations. This way, a vertex in a model is first transformed into the desired orientation and then transformed by the viewing operation. Since the matrix multiplications must be specified in reverse order, the viewing commands need to come first.