Step 1 Z-buffering. The Z-buffer stores how far a pixel is away from the camera in camera space (normalized device coordinate, more precisely. Typically, it ranges [0, 1]). Limited precision: 16-bit, 24-bit, and 32-bit depth value The Z-buffer is necessary to resolve visibility: a) Clear the z-buffer b) Enable the z-buffer Commands: § glClear( GL_DEPTH_BUFFER_BIT | … colors … ); § glEnable( GL_DEPTH_TEST ); § glutInitDisplayMode( GLUT_DEPTH | … colors … | … double … );
Orthographic Projection glOrtho (left, right, bottom, top, near, far) § Viewing volume is a box. § Objects appear same size irrespective of their distance from the camera. § Use it to show messages
Perspective Projection gluPerspective (fov, aspectratio, near, far) § Viewing volume is a truncated pyramid. § Farther objects appear small and closer objects appear big. § These commands calculates the projection matrix and multiplies the current projection matrix by it.
Notes § Changing the distance between the near and far clipping planes can have a big effect on the dimensions of the view volume and the angle of the projectors, making it tricky to set up a reasonable view volume for a given scene. § Where and how to incorporate it into your program? a) Set once per frame (put it in your display function). Some overhead, but safer b) Set once in init function, and update whenever window resizes
Step 3 § Set the camera parameters before you define other transformations to model objects in the scene. § Set camera transformation once per frame (in case you have to move your camera interactively) § Try to restore camera matrix each frame. Do not accumulate the viewing matrix, the accumulation might be skewed due to accumulation of computational errors glMatrixMode(GL_MODELVIEW);
gluLookAt § Viewing transformation can be specified using the command gluLookAt ( eye_x, eye_y, eye_z, center_x, center_y, center_z, up_x, up_y, up_z ) § It encapsulates a series of rotation and translation commands. Viewing transformation can be specified using following commands too. This, however, makes your life harder! § glTranslate {f,d} (x, y, z) § glRotate {f,d} (angle, x, y, z) § glScale {f,d} (x, y, z)
gluLookAt glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( 0.0, 1.0, 1.0, /* eye is at (0,1,1) */ 0.0, 1.0, 2.0, /* look at point (0,1,2), viewing direction vector is (0,0,1) */ 0.0, 1.0, 0.0 /* up is in positive Y direction */ );
Reading 3D geometric transformation & viewing: § (Required) Read Hearn Baker ch5 p261-p277, p283 – p291 § (Required) Read Hearn Baker ch7 p344-p398, focus on perspective projection (pp 368-383), and 3D viewing in OpenGL (pp 383-389) Z-buffer: § Red Book (pdf): ch10, focus on depth buffer related content § Hearn Baker ch9 p531-p534 Front / Back face culling: § Red Book (pdf): ch2 p43-p44