What you’ll learn in this chapter:
This chapter discusses lighting: in our opinion, the honey spot of OpenGL. You’ve been learning OpenGL from the ground up—how to put programs together, then how to assemble objects from primitives and manipulate them in 3D space. In Chapter 8 we showed you how to add color to your objects and do smooth shading. All well and good, but let’s face it—any good summer co-op student with a good book on computer graphics could have put this much together themselves building only on the Windows GDI. To recoin a phrase, “Where’s the Beef?”
To put it succinctly, the beef starts here. For most of the rest of this book, science takes a back seat and magic rules. According to Arthur C. Clarke, “Any sufficiently advanced technology is indistinguishable from magic.” Of course there is no real magic involved in lighting, but it sure can seem that way at times. (If you want to dig into the mathematics, see Appendix B.)
Another name for this chapter might be “Adding Realism to Your Scenes.” You see, there is more to an object’s color in the real world than what we explained in Chapter 8. In addition to having a color, objects can appear shiny or dull or may even glow with their own light. An object’s apparent color will vary with bright or dim lighting, and even the color of the light hitting an object will make a difference. An illuminated object can even be shaded across its surface when lit or viewed from an angle.
Most of the rest of Parts II and III are concerned with techniques that allow you to add more and more realism to your scenes. So put away your calculators (if you want), bring out your wizard’s cap, and take a deep breath… The magic show starts here!
Real objects don’t appear in a solid or shaded color based solely on their RGB value. Figure 9-1 shows the output from the program JET from the CD. It’s a simple jet airplane, hand plotted with triangles using only the methods covered so far in this book. As usual, JET and the other programs in this chapter allow you to spin the object around by using the arrow keys to better see the effects.
The selection of colors is meant to highlight the three-dimensional structure of the jet. Aside from the crude assemblage of triangles, however, you can see that it looks hardly anything like a real object. Suppose you constructed a model of this airplane and painted each flat surface the colors represented. The model would still appear glossy or flat depending on the kind of paint used, and the color of each flat surface would vary with the angle of your view and any sources of light.
OpenGL does a very good job of approximating the real world in terms of lighting conditions. Unless an object emits its own light, it is illuminated by three different kinds of light: ambient, diffuse, and specular.
Ambient light is light that doesn’t come from any particular direction. It has a source, but the rays of light have bounced around the room or scene and become directionless. Objects illuminated by ambient light are evenly lit on all surfaces in all directions. You can think of all previous examples in this book as being lit by a bright ambient light, because the objects were always visible and evenly colored (or shaded) regardless of their rotation or viewing angle. Figure 9-2 shows an object illuminated by ambient light.
Diffuse light comes from a particular direction but is reflected evenly off a surface. Even though the light is reflected evenly, the object surface is brighter if the light is pointed directly at the surface than if the light grazes the surface from an angle. A good example of a diffuse light source is fluorescent lighting, or sunlight streaming in a side window at noon. In Figure 9-3 the object is illuminated by a diffuse light source.
Like diffuse light, specular light is directional, but it is reflected sharply and in a particular direction. A highly specular light tends to cause a bright spot on the surface it shines upon, which is called the specular highlight. A spotlight and the Sun are examples of specular light. Figure 9-4 shows an object illuminated by a purely specular light source.