Now we’ll extend our two-dimensional coordinate system into the third dimension and add a depth component. Figure 2-11 shows the Cartesian coordinate system with a new axis, z. The z-axis is perpendicular to both the x- and y-axes. It represents a line drawn perpendicularly from the center of the screen heading toward the viewer. (We have rotated our view of the coordinate system from Figure 2-7 to the left with respect to the y-axis, and down and back with respect to the x-axis. If we hadn’t, the z-axis would come straight out at you and you wouldn’t see it.) Now we specify a position in three-dimensional space with three coordinates—x, y, and z. Figure 2-11 shows the point (–4, 4, 4) for clarification.
You’ve seen how to specify a position in 3D space using Cartesian coordinates. No matter how we might convince your eye, however, pixels on a screen have only two dimensions. How does OpenGL translate these Cartesian coordinates into two-dimensional coordinates that can be plotted on a screen? The short answer is “trigonometry and simple matrix manipulation.” Simple? Well, not really—we could actually go on for many pages and lose most of our readers who didn’t take or don’t remember their linear algebra from college explaining this “simple” technique. You’ll learn more about it in Chapter 7, and for a deeper discussion you can check out the references in Appendix B. Fortunately, you don’t need to understand the math in order to use OpenGL to create graphics.
All you really need to understand to get the most from this book is a concept called projection. The 3D coordinates are projected onto a 2D surface (the window background). It’s like tracing the outlines of some object behind a piece of glass with a black marker. When the object is gone or you move the glass, you can still see the outline of the object with its angular edges. In Figure 2-12 a house in the background is traced onto a flat piece of glass. By specifying the projection, you specify the clipping volume (remember clipping areas?) that you want displayed in your window, and how it should be translated.
You will mostly be concerned with two main types of projections in OpenGL. The first is called an orthographic or parallel projection. You use this projection by specifying a square or rectangular clipping volume. Anything outside this clipping area is not drawn. Furthermore, all objects that have the same dimensions appear the same size, regardless of whether they are far away or nearby. This type of projection (shown in Figure 2-13) is most often used in architectural design or CAD (computer aided design).
You specify the clipping volume in an orthographic projection by specifying the far, near, left, right, top, and bottom clipping planes. Objects and figures that you place within this viewing volume are then projected (taking into account their orientation) to a 2D image that appears on your screen.
A second and more common projection is the perspective projection. This projection adds the effect that distant objects appear smaller than nearby objects. The viewing volume (Figure 2-14) is something like a pyramid with the top shaved off. This shaved off part is called the frustum. Objects nearer to the front of the viewing volume appear close to their original size, while objects near the back of the volume shrink as they are projected to the front of the volume. This type of projection gives the most realism for simulation and 3D animation.
In this chapter we have introduced the very basics of 3D graphics. You’ve seen why you actually need two images of an object from different angles in order to perceive true three-dimensional space. You’ve also seen the illusion of depth created in a 2D drawing by means of perspective, hidden line removal, and coloring, shading, and lighting techniques. The Cartesian coordinate system was introduced for 2D and 3D drawing, and you learned about two methods used by OpenGL to project three-dimensional drawings onto a two-dimensional screen.
We purposely left out the details of how these effects are actually created by OpenGL. In the chapters that follow, you will find out how to employ these techniques and take maximum advantage of OpenGL’s power. On the Companion CD you’ll find one program for Chapter 2 (CUBE) that demonstrates the concepts covered in the first section of this chapter. In CUBE, pressing the spacebar will advance you from a wireframe cube to a fully lit cube complete with shadow. You won’t understand the code at this point, but it makes a powerful demonstration of what is to come. By the time you finish this book, you will be able to revisit this example and even be able to write it from scratch yourself.