[ Previous | Next | Contents | Glossary | Home | Search ]
GL3.2 Version 4.1 for AIX: Programming Concepts

Controlling the Order of Transformations

Each time you specify a transformation such as rotation or translation, the software automatically generates a transformation matrix that specifies the amount by which the object's coordinate system is to be premultiplied. This transformation matrix is loaded atop a hardware stack in the graphics pipeline. When you specify a series of transformations, the software loads each successive transformation matrix onto the stack and modifies the current transformation matrix.

Because the graphics pipeline maintains a stack of matrices, you can save transformation matrices that define a particular state by manipulating the matrix stack.

Hierarchical Drawing with the Matrix Stack

A drawing is often composed of copies of simpler drawings, each of which may itself be composed of still simpler drawings. For example, if you were writing a program to draw a picture of a bicycle, you might want to have a subroutine that draws a wheel. Calling that subroutine twice, appropriately translated, would draw two wheels. The wheel itself might be drawn by calling the spoke-drawing subroutine 36 times, appropriately rotated. In a still more complicated drawing of many bicycles, you could call the bicycle-drawing routine many times.

Suppose the bicycle is described in a coordinate system where the bottom bracket (the hole through which the pedal crank's axle runs) is the origin. You would draw the frame relative to this origin, but translate forward a few inches before drawing the front wheel (defined relative to its axis). You would then remove the forward translation to get back to the bicycle's frame of reference, translate back, and draw another instance of the wheel.

What happens mathematically is this: suppose the modeling transformation that describes the bicycle's frame of reference is M, and that S and T are transformations (relative to M) to move forward for drawing the front wheel and move back for the back wheel, respectively. Moreover, suppose you want to draw the wheel using transformation SM for the front wheel and transformation TM for the back wheel.

This is easily done using the matrix stack. At any point in a drawing, there is a current matrix on top of the matrix stack, composed of all the transformations thus far. In the bicycle example, we call this collection of transformations M. Any vertex is transformed by the top matrix, which is just what you want to do for drawing the frame.

The pushmatrix and popmatrix subroutines push and pop the matrix stack. The pushmatrix subroutine pushes the matrix stack down and copies the current matrix to the new top. Following a call to the pushmatrix subroutine, there would be two copies of M on top. A call to the translate subroutine (by a translation matrix S) leaves the stack with SM on top and M underneath. The wheel is then drawn once using the SM transformation. A call to the popmatrix subroutine then eliminates the SM on top, leaving M. A second call to the pushmatrix subroutine makes two copies of M.

Translating by T puts TM on top, so you can now draw the back wheel. After the matrix stack is popped again, M is on top, and you can draw the rest of the frame. The code for drawing the bicycle would look similar to this:

 ... /* code to get M on top of the stack */
pushmatrix();
translate(-dist_to_back_wheel, 0.0, 0.0);
drawwheel();
popmatrix();
pushmatrix();
translate(dist_to_front_wheel, 0.0, 0.0);
drawwheel();
popmatrix();
drawframe();

As shown in the code, the drawwheel subroutine can itself push and pop matrices before calling the drawspoke subroutine.

pushmatrix Subroutine

The pushmatrix subroutine pushes down the transformation stack, duplicating the current matrix. For example, if the transformation stack contains one matrix, M, then after a call to the pushmatrix subroutine, it contains two copies of M. You can modify only the top copy. The syntax is as follows:

void pushmatrix()

popmatrix Subroutine

The popmatrix subroutine pops the transformation stack. When this subroutine executes, the matrix on top of the stack is lost. The syntax is as follows:

void popmatrix()

[ Previous | Next | Contents | Glossary | Home | Search ]