Open GL Super Bible
Projection TransformationsThe 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 76 compares orthographic and perspective projections on two different scenes.
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 walkthrough 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 TransformationsWhen all is said and done, you end up with a twodimensional 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 MunchingNow 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 twodimensional 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 77 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.
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 handcoding some special transformations, see Appendix B for some good references. The Transformation PipelineTo 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 highlevel functions that you can call for these transformations. Only if you want to do something unusual do you need to call the lowerlevel functions that actually set the values contained in the matrices. The road from raw vertex data to screen coordinates is a long one. Figure 78 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.
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 highlevel 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.
