Game Development Reference
In-Depth Information
glMatrixMode(GL_PROJECTION);
gluPerspective(60, (double) width / height, 1, 1000);
glutPostRedisplay();
}
Using transformations
Drawing the display begins by clearing anything that is already there.
Since we are using a depth buffer it is quicker to tell OpenGL to clear both
the colour buffer and the depth buffer in a single command. This is done
by bitwise Or-ing the two constants 'GL_COLOR_BUFFER_BIT' and
'GL_DEPTH_BUFFER_BIT'. This is followed by switching the matrix
mode to the modelview matrix and clearing this using the 'glLoadIdentity'
function call. Now we come to the interesting bit; here we use
'glTranslatef' and 'glRotatef' to alter the contents of the modelview matrix.
'glTranslate' has the effect of moving the viewer in relation to any
geometry that is later going to be drawn. The parameters are the
movement along the x -, y - and z -axes respectively. In this example we
use the variable, z value; this is set with the mouse move callback function
'motion'. Using this method we can adjust the z position of the object.
After translating the object we rotate it. 'glRotatef' has four parameters;
the first is an angle, the remaining three give the axis around which this
rotation operates. In this example we rotate the object using Euler angles
around the standard axes. When objects are transformed using the
modelview matrix, the effect is as though the first operation performed is
the last one specified. In this example all the rotations would be performed
before the translation.
In matrix notation, if T is the translation and P is the first rotation
specified, H the second and B the last, the overall result would be
TPHBv
if v is the vertex being operated on.
Having set up the matrix, we can now draw some geometry. In the
example we use a function that is part of the source code, to draw a
simple cube, 'DrawCube'. Having drawn the cube, the off-screen buffer is
flipped to the front with the GLUT function 'glutSwapBuffers'.