A Walkthrough: Moving around in the world
In this demo, you move around on the top of a large green disk. The top of the disk lies in the xz-plane. Various objects are scattered about the disk for you to look at. You can turn left and right. You can move forward and backward in the direction that you are facing. You can tilt your gaze upwards and downwards (but only to a limited extent).
The display area on the left shows your view of the world. The display area on the bottom right shows the view from above, with your position marked with a white arrow.
Your view is determined by four variables: The variables x and z give the x- and z-coordinates of your position. (Your y-coordinate is always 2, which is meant to be the height of your eyes above the plane of the disk. That's 2 meters; you are rather tall.) The variable facing gives the angle that your gaze makes with the negative direction of the z-axis. The variable headTilt gives that angle that your gaze makes with the horizontal. The viewing transformation is then given by setting up a modelview transform as follows, before drawing the world:
glLoadIdentity(); glRotatef(-headTilt,1,0,0); glRotatef(-facing,0,1,0); glTranslatef(-x,-2,-z);
Remember that viewing transforms are applied in the order in which they occur in the code, so the view is first rotated to account for the tilt of your head, then rotated to the direction that you are facing, then translated to the position of your eyes. Also remember that the effect of a viewing transform is the inverse of the effect that it would have on objects as a modeling transform. This accounts for all the negative values in the commands. As a modeling transform, the effect would be to translate the world by (−x,−2,−z), then rotate it by −facing, then tilt it by −headTilt.