Section 3.4. Perspective and Parallel Projections


3.4. Perspective and Parallel Projections

In addition to specifying the position and orientation of the camera, applications need to specify other camera attributes, such as field of view, near and far clip plane distance, and whether to use a perspective or parallel projection. Applications should specify these parameters with the projection matrix.

After performing eye-coordinate operations, such as lighting, OpenGL transforms vertex data into clip-coordinate space by using the projection matrix. This matrix determines the shape and size of the view volume. When the data is in clip-coordinate space, OpenGL clips any geometry outside the view volume and discards it.

The form of the projection matrix determines the clip-coordinate w value. Parallel projection matrices always produce 1.0 for the clip-coordinate w value, whereas perspective matrices produce a range of clip-coordinate w values. OpenGL transforms clip coordinates into normalized device coordinates by dividing the clip-coordinate x, y, and z values by the clip-coordinate w value. For the perspective case, this division causes distant geometry to appear smaller than near geometry.

OpenGL provides two commands that create projection matrices: glOrtho() and glFrustum().


void glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdou-
  ble top, GLdouble near, GLdouble far );


Creates a parallel projection matrix and multiplies it onto the top of the active matrix stack. The six parameters define the six sides of a parallel projection view volume. Both near and far are distances from the eye to the near and far clip planes. All values have unlimited range.

glOrtho() generates a GL_INVALID_VALUE error if left=right, bottom=top, or near=far.

To create a parallel projection, first set the matrix mode to GL_PROJECTION; then load an identity matrix and call glOrtho().

Applications use glOrtho() to create parallel projections. Several applications also use glOrtho() to specify geometry directly in window-coordinate space. Assume, for example, that an application window is 800 pixels wide and 600 pixels high. The following code creates a one-to-one mapping between object coordinates and window coordinates:

 glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( 0., 799., 0., 599., -1., 1. ); 


(This code also requires the correct viewport. See the next section, "The Viewport," for more information.)

To create a perspective projection, OpenGL provides the glFrustum() routine. glFrustum() allows applications to create a large variety of perspective projection view volumes. In general, applications don't use glFrustum(), as GLU provides a simpler interface: gluPerspective().


void gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble near,
  GLdouble far );


Creates a perspective projection matrix and multiples it onto the top of the active matrix stack. fovy defines a field-of-view angle in degrees in the y direction, with the field of view in x defined by (fovy* aspect). near and far are positive distances from the eye to the near and far clipping planes.

Compared with glFrustum(), gluPerspective() allows applications to specify the field of view and aspect ratio directly, and always creates a symmetrical view volume, which satisfies most application rendering requirements.

The example code that comes with this book makes extensive use of gluPerspective(). The following code, from the VertexArray example, creates a perspective projection with a 50 degree field of view, and near and far clipping planes at 1.0 and 10.0 coordinate units in front of the eye:

 glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 50., (double)w/(double)h, 1., 10. ); 


In the above code, w and h are the window width and height in pixels.

Applications generally set the projection matrix when the program starts and then again every time the user changes the display window size. GLUT applications need only set the projection matrix in the reshape callback function, because GLUT calls the reshape callback when it first opens the window.

The Viewing example code (refer to Figure 3-3) demonstrates how to alternate between parallel and perspective projections.




OpenGL Distilled
OpenGL Distilled
ISBN: 0321336798
EAN: 2147483647
Year: 2007
Pages: 123
Authors: Paul Martz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net