# OpenGL: Transformations

I'm not sure if I correctly understand 3D transformations in OpenGL. Let's assume I'm using the typical matrix stack.

It seems like you move the world X units over, drop in a bag of verts (a mesh) and move the world back by popping the matrix off the stack, then you repeat the process to place another model?

Say I wanted to place a model at (10, 0, 0) and another at (0, 10, 0). I think I would...

PUSH move(10,0,0);
DRAW model A;
POP;
PUSH move(0,10,0);
DRAW model B;
POP;


Some questions come to mind, like for example what does loading an identity matrix do in the stack?

Am I right to assume that these operations in effect move the world around, then you draw the mesh at 0,0,0 in world space when you've moved the world to your liking?

Or is it the other way around, does GL "apply" the matrix stack to your mesh as you draw it?

Thanks, -Cody

OpenGL is a state-machine, it has a current matrix.

There are several functions to manipulate the current matrix:

• glLoadIdentity, glLoadMatrix overwrite the current matrix.
• glTranslate, glScale, glRotate, glMultMatrix multiply the current matrix with the appropriate matrix generated by these functions from the right.

Now, whenever you draw something it is transformed using the current modelview matrix (and then using the projection matrix). The stack itself is not used in any way in those transformations, it is merely for storing matrices. When you push the current matrix onto the stack the current matrix doesn't change. When you pop, the current matrix is overwritten with the matrix from the top of stack.

Also, OpenGL's camera is always at the origin looking in the -Z direction. You can't move the camera but you can “move the world around the camera” into your preferred position.

It's the other way around. Changing the transformation matrix doesn't do anything to what has already been drawn. It only affects what is going to be drawn. Think of your matrix stack as a bunch of filters which your vertices get sent through when you send them to the video card.

An identity matrix does - by definition - nothing when multiplied with another matrix.

• I've heard of people loading or adding or somehow using the identity matrix in the stack for various reasons. Just wondering if pushing the identity matrix does anything to the stack? Also, it's been noted that a rotation say 45 degrees and then a translation will translate the object in the direction of the rotation. IE if the object was at 0,0 and was rotated 45 then translated in X by 10 the object would end up at 7(ish), 7(ish) in "world space" I don't entirely understand this. It seems like those filters are modifying the coordinate system as well for all drawn verts. – Cody Smith Feb 10 '13 at 22:01
• An identity matrix is useful as a starting point to create modifications from it, but multiplying a vector or matrix with an unmodified identity matrix is a no-op. Modifying the coordinate system is exactly what the projection matrix does - it matches the xyz coordinates of the world to xyz coordinates on the screen (z is used for depth buffering). – Philipp Feb 10 '13 at 23:04