Open GL Super Bible

Previous Table of Contents Next


Projection Transformations

The projection transformation is applied to your final Modelview orientation. This projection actually defines the viewing volume and establishes clipping planes. More specifically, the projection transformation specifies how a finished scene (after all the modeling is done) is translated to the final image on the screen. You will learn about two types of projections in this chapter: orthographic and perspective.

In an orthographic projection, all the polygons are drawn on screen with exactly the relative dimensions specified. This is typically used for CAD, or blueprint images where the precise dimensions are being rendered realistically.

A perspective projection shows objects and scenes more as they would appear in real life than in a blueprint. The trademark of perspective projections is foreshortening, which makes distant objects appear smaller than nearby objects of the same size. And parallel lines will not always be drawn parallel. In a railroad track, for instance, the rails are parallel, but with perspective projection they appear to converge at some distant point. We call this point the vanishing point.

The benefit of perspective projection is that you donít have to figure out where lines converge, or how much smaller distant objects are. All you need to do is specify the scene using the Modelview transformations, and then apply the perspective projection. It will work all the magic for you.

Figure 7-6 compares orthographic and perspective projections on two different scenes.


Figure 7-6  Two examples of orthographic vs. perspective projections

In general, you should use orthographic projections when you are modeling simple objects that are unaffected by the position and distance of the viewer. Orthographic views usually occur naturally when the ratio of the objectís size to its distance from the viewer is quite small (say, a large object thatís far away). Thus, an automobile viewed on a showroom floor can be modeled orthographically, but if you are standing directly in front of the car and looking down the length of it, perspective would come into play. Perspective projections are used for rendering scenes that contain many objects spaced apart, for walk-through or flying scenes, or for modeling any large objects that may appear distorted depending on the viewerís location. For the most part, perspective projections will be the most typical.

Viewport Transformations

When all is said and done, you end up with a two-dimensional projection of your scene that will be mapped to a window somewhere on your screen. This mapping to physical window coordinates is the last transformation that is done, and it is called the viewport transformation. The viewport was discussed briefly in Chapter 3, where you used it to stretch an image or keep a scene squarely placed in a rectangular window.

Matrix Munching

Now that youíre armed with some basic vocabulary and definitions of transformations, youíre ready for some simple matrix mathematics. Letís examine how OpenGL performs these transformations and get to know the functions you will call to achieve your desired effects.

The mathematics behind these transformations are greatly simplified by the mathematical notation of the matrix. Each of the transformations we have discussed can be achieved by multiplying a matrix that contains the vertices, by a matrix that describes the transformation. Thus all the transformations achievable with OpenGL can be described as a multiplication of two or more matrices.

What Is a Matrix?

A matrix is nothing more than a set of numbers arranged in uniform rows and columnsóin programming terms, a two-dimensional array. A matrix doesnít have to be square, but each row or column must have the same number of elements as every other row or column in the matrix. Figure 7-7 presents some examples of matrices. (These donít represent anything in particular but only serve to demonstrate matrix structure.) Note that a matrix can have but a single column.


Figure 7-7  Examples of matrices

Our purpose here is not to go into the details of matrix mathematics and manipulation. If you want to know more about manipulating matrices and hand-coding some special transformations, see Appendix B for some good references.

The Transformation Pipeline

To effect the types of transformations described in this chapter, you will modify two matrices in particular: the Modelview matrix, and the Projection matrix. Donít worry, OpenGL gives you some high-level functions that you can call for these transformations. Only if you want to do something unusual do you need to call the lower-level functions that actually set the values contained in the matrices.

The road from raw vertex data to screen coordinates is a long one. Figure 7-8 is a flowchart of this process. First, your vertex is converted to a 1 x 4 matrix in which the first three values are the x, y, and z coordinates. The fourth number is a scaling factor that you can apply manually by using the vertex functions that take four values. This is the w coordinate, usually 1.0 by default. You will seldom modify this value directly but will apply one of the scaling functions to the Modelview matrix instead.


Figure 7-8  The vertex transformation pipeline

The vertex is then multiplied by the Modelview matrix, which yields the transformed eye coordinates. The eye coordinates are then multiplied by the Projection matrix to yield clip coordinates. This effectively eliminates all data outside the viewing volume. The clip coordinates are then divided by the w coordinate to yield normalized device coordinates. The w value may have been modified by the Projection matrix or the Modelview matrix, depending on the transformations that may have occurred. Again, OpenGL and the high-level matrix functions will hide all this from you.

Finally, your coordinate triplet is mapped to a 2D plane by the viewport transformation. This is also represented by a matrix, but not one that you will specify or modify directly. OpenGL will set it up internally depending on the values you specified to glViewport.


Previous Table of Contents Next