In 3D graphics, viewing is the process of establishing the perspective and orientation with which the scene should be rendered. Like a photographer properly setting up his camera, an OpenGL programmer should establish a view. Figure 4 shows how the view is set up for the example program.
In OpenGL, establishing a view means loading the projection and model-view matrices with the right contents. To modify the projection matrix, call glMatrixMode(GL_PROJECTION). Calculating the right matrix by hand can be tricky. The GLU library has two useful routines that make the process easy.
GLU's gluPerspective routine allows you to specify a field of view angle, an aspect ratio, and near and far clipping planes. It multiplies the current projection matrix with one created according to the routine's parameters. Since initially the projection matrix is an identity matrix, glxdino's gluPerspective call effectively loads the projection matrix.
Another GLU routine, gluLookAt, can be used to orient the eye-point for the model-view matrix. Notice how glMatrixMode(GL_MODELVIEW) is used to switch to the model-view matrix. Using gluLookAt requires you to specify the eye-point's location, a location to look at, and a normal to determine which way is up. Like gluPerspective, gluLookAt multiplies the matrix it constructs from its parameters with the current matrix. The initial model-view matrix is the identity matrix so glxdino's call to gluLookAt effectively loads the model-view matrix.
After the gluLookAt call, glPushMatrix is called. Both the model-view and projection matrices exist on stacks that can be pushed and popped. Calling glPushMatrix pushes a copy of the current matrix onto the stack. When a rotation happens, this matrix is popped off and another glPushMatrix is done. This newly pushed matrix is composed with a rotation matrix to reflect the current absolute orientation. Every rotation pops off the top matrix and replaces it with a newly rotated matrix.
Notice that the light positions are not set until after the model-view matrix has been properly initialized.
Because the location of the viewpoint affects the calculations for lighting, separate the projection transformation in the projection matrix and the modeling and viewing transformations in the model-view matrix.