Open GL Super Bible

Previous Table of Contents Next

Chapter 2
3D Graphics Fundamentals

What you’ll learn in this chapter:

How the eyes perceive three dimensions
How a 2D image can have the appearance of 3D
How Cartesian coordinates specify object positions
What a clipping volume is
How viewports affect image dimensions
How 3D objects are built from 2D primitives
How to work with orthographic and perspective projections

Before getting into the specifics of using OpenGL to create 3D graphics, we’ll take some time out to establish some 3D vocabulary. In doing so, we will orient you to the fundamental concepts of 3D graphics and coordinate systems. You’ll find out why we can get away with calling 2D images on a flat computer screen 3D graphics. Readers experienced in 3D graphics who are ready to get started using OpenGL may want to just skim this chapter.

3D Perception

“3D computer graphics” are actually two-dimensional images on a flat computer screen that provide an illusion of depth, or a third “dimension.” In order to truly see in 3D, you need to actually view the object with both eyes, or supply each eye with separate and unique images of the object. Take a look at Figure 2-1. Each eye receives a two-dimensional image that is much like a temporary photograph on the retina (the back part of your eye). These two images are slightly different because they are received at two different angles (your eyes are spaced apart on purpose). The brain then combines these slightly different images to produce a single, composite 3D picture in your head, as shown in Figure 2-1.

Figure 2-1  How the eyes “see” three dimensions

In Figure 2-1, the angle [theta] between the images gets smaller as the object goes farther away. This 3D effect can be amplified by increasing the angle between the two images. Viewmasters (those hand-held stereoscopic viewers you probably had as a kid) and 3D movies capitalize on this effect by placing each of your eyes on a separate lens, or by providing color-filtered glasses that separate two superimposed images. These images are overenhanced for dramatic or cinematic purposes.

So what happens when you cover one eye? You may think you are still seeing in 3D, but try this experiment: Place a glass or some other object just out of arm’s reach, off to your left side. Cover your right eye with your right hand and reach for the glass. (Maybe you should use an empty plastic one!) Notice that you have a more difficult time estimating how much farther you need to reach (if at all) before you touch the glass. Now uncover your right eye and reach for the glass, and you can easily discern how far you need to lean to reach the glass. This is why people who have lost one eye often have difficulty with distance perception.

2D + Perspective = 3D

The reason the world doesn’t become suddenly flat when you cover one eye is that many of a 3D world’s effects are also present in a 2D world. This is just enough to trigger your brain’s ability to discern depth. The most obvious cue is that nearby objects appear larger than distant objects. This effect is called perspective. And perspective plus color changes, textures, lighting, shading, and variations of color intensities (due to lighting) together add up to our perception of a three-dimensional image.

Perspective alone is enough to lend the appearance of three dimensions. Figure 2-2 presents a simple wireframe cube. Even without coloring or shading, the cube still has the appearance of a three-dimensional object. Stare at the cube for long enough, however, and the front and back of the cube will switch places. This is because your brain is confused by the lack of any surface in the drawing.

Figure 2-2  This simple wireframe cube demonstrates perspective

Hidden Line Removal

Figure 2-2 contains just enough information to lend the appearance of three dimensions, but not enough to let you discern the front of the cube from the back. When viewing a real object, how do you tell the front from the back? Simple—the back is obscured by the front. If the cube in Figure 2-2 were a solid, you wouldn’t be able to see the corners in the back of the cube, and thus you wouldn’t confuse them for the corners in the front of the cube. Even if the cube were made of wire, parts of the wires in front would obscure parts of the wires in the back. To simulate this in a two-dimensional drawing, lines that would be obscured by surfaces in front of them must be removed. This is called hidden line removal and it has been done to the cube in Figure 2-3.

Figure 2-3  The cube after hidden lines are removed

Colors and Shading

Figure 2-3 still doesn’t look much like a real-world object. The faces of the cube are exactly the same color as the background, and all you can see are the front edges of the object. A real cube would have some color and/or texture; in a wooden cube, for example, the color and grain of the wood would show. On a computer (or on paper), if all we did was color the cube and draw it in two dimensions, we would have something similar to Figure 2-4.

Figure 2-4  The cube with color, but no shading

Now we are back to an object that appears two-dimensional, and unless we specifically draw the edges in a different color, there is no perception of three dimensions at all. In order to regain our perspective of a solid object (without drawing the edges a different color), we need to either make each of the three visible sides a different color, or make them the same color with shading to produce the illusion of lighting. In Figure 2-5, the faces of the cube all have a different color or shade.

Figure 2-5  The cube with its visible faces in three different shades

Lights and Shadows

One last element we must not neglect is lighting. Lighting has two important effects on objects viewed in three dimensions. First, it causes a surface of a uniform color to appear shaded when viewed or illuminated from an angle. Second, objects that do not transmit light (most solid objects) cast a shadow when they obstruct the path of a ray of light. See Figure 2-6.

Figure 2-6  A solid cube illuminated by a single light

Two sources of light can influence our three-dimensional objects. Ambient light, which is undirected light, is simply a uniform illumination that can cause shading effects on objects of a solid color; ambient light causes distant edges to appear dimmer. Another source of light is from a light source, called a lamp. Lamps can be used to change the shading of solid objects and for shadow effects.

Previous Table of Contents Next