Help stamp out GL_PROJECTION abuse.

By Steve Baker
One of the most common OpenGL programming errors is misuse of the GL_PROJECTION matrix stack. This matrix stack is often perceived as being the correct place to put the position and orientation of the 'camera' - but this is not in fact the case.

Unfortunately, when someone does go and put their camera transform into the GL_PROJECTION matrix (instead of into GL_MODELVIEW where it belongs) - the consequences are rather subtle:

So, it's easy to see how someone could have a program that pretty much works correctly - that would appear to fail when the programmer first tries to play with Fog - or first notices a peculiar lighting anomaly. Perhaps one might not notice until switching to a new OpenGL implementation causes the behaviour to break.

Hence, most people are very disbelieving when their problems are diagnosed as projection abuse.

The Golden Rule.

The rule is:

The only functions that should be called when the glMatrixMode is set to GL_PROJECTION are:

Mitigating Circumstances.

It is actually possible to store the camera position in the GL_PROJECTION matrix under certain VERY specific circumstances - and if you are VERY familiar with OpenGL and attempting to use it only for rasterization. However, it is pretty much always the case that if you are asking the question then you don't understand enough to deal with the answer. If you don't already know what I mean then don't do it.

OK, So how DO I set up the Camera?

Well, you need to push the inverse of the camera position/orientation transform onto the GL_MODELVIEW stack before you start drawing polygons. If you use gluLookAt to do this, it's fairly painless - but using glRotate/glTranslate is likely to cause problems for all but the simplest camera motion since you want the INVERSE of the camera location.

What I do is to compose the camera matrix by hand, and then invert it before doing a glLoadMatrix. However, that does require a certain confidence with matrix operations that many people seem to lack.