What you’ll learn in this chapter:
In Chapter 6, you learned how to draw points, lines, and various primitives in 3D. To turn a collection of shapes into a coherent scene, you must arrange them in relation to one another and to the viewer. In this chapter, you’ll start moving shapes and objects around in your coordinate system. (Actually, you don’t move the objects, but rather shift the coordinate system to create the view you want.) The ability to place and orient your objects in a scene is a crucial tool for any 3D graphics programmer. As you will see, it is actually very convenient to describe your objects’ dimensions around the origin, and then translate and rotate the objects into the desired position.
Yes, this is the dreaded math chapter. However, you can relax—we are going to take a more moderate approach to these principles than some texts.
The keys to object and coordinate transformations are two modeling matrices maintained by OpenGL. To familiarize you with these matrices, this chapter strikes a compromise between two extremes in computer graphics philosophy. On the one hand, we could warn you, “Please review a textbook on linear algebra before reading this chapter.” On the other hand, we could perpetuate the deceptive reassurance that you can “learn to do 3D graphics without all those complex mathematical formulas.” But we don’t agree with either camp.
In reality, yes, you can get along just fine without understanding the finer mathematics of 3D graphics, just as you can drive your car every day without having to know anything at all about automotive mechanics and the internal combustion engine. But you’d better know enough about your car to realize that you need an oil change every so often, that you have to fill the tank with gas regularly and change the tires when they get bald. This makes you a responsible (and safe!) automobile owner. If you want to be a responsible and capable OpenGL programmer, the same standards apply. You want to understand at least the basics, so you know what can be done and what tools will best suit the job.
So, even if you don’t have the ability to multiply two matrices in your head, you need to know what matrices are and that they are the means to OpenGL’s 3D magic. But before you go dusting off that old linear algebra textbook (doesn’t everyone have one?), have no fear—OpenGL will do all the math for you. Think of it as using a calculator to do long division when you don’t know how to do it on paper. Though you don’t have to do it yourself, you still know what it is and how to apply it. See—you can have your cake and eat it too!
Transformations make possible the projection of 3D coordinates onto a 2D screen. Transformations also allow you to rotate objects around, move them about, and even stretch, shrink, and wrap them. Rather than modifying your object directly, a transformation modifies the coordinate system. Once a transformation rotates the coordinate system, then the object will appear rotated when it is drawn. There are three types of transformations that occur between the time you specify your vertices and the time they appear on the screen: viewing, modeling, and projection. In this section we will examine the principles of each type of transformation, which you will find summarized in Table 7-1.