Clarification (Projection Matrix & ModelView Matrix)?

I’ve seen numerous codes and everyone is putting glMode(GL_PROJECTION) in a certain place in the code. For example, some people put it in Reshape function, other put it Display function and other put it in initialize function. I know what they are for, however, I’m not sure where should I put them in my code and by which order? glLoadIdentity(), glMatrixMode(GL_PROJECTION), and glMatrixMode(GL_MODELVIEW).

The simple answer is: put it where you need it and nowhere else. Every state change incurs a cost and changing the projection is, in most cases done rarely. One case is the resize() function where a change of window dimension leads to a change is aspect ratio used during projection matrix calculation.

Doing that stuff in display is, for instance, useful when you set up a temporary camera that needs a different projection than the main camera. A good example is shadow map calculation.

In general: If you can, don’t use legacy GL at all. Write your stuff yourself and forget about the the matrix stack altogether.

Change the projection matrix when your screen changes aspect as thokra mentioned, or when you want to “zoom”, or achieve some wide-angle look, or go orthographic for a very flat look. BTW, there is only one “wideness” of angle that will give you an undistorted projection, for example using gluPerspective() with a fovy value of the half-angle the height of your window makes from your eye, typically 10-30 degrees, exactly arctan(window height/2/eye-screen-distance), in degrees. You don’t want to change your projection matrix every time you move or rotate the camera, that’s the view matrix, or the view factor in the model-view matrix.

A great tutorial to drop all the old legacy GL, and learn the new OpenGL design of versions 3 and 4 is at opengl-tutorial org (Search for opengl tutorial, and it comes up first.)