Open GL Super Bible

Previous Table of Contents Next

Put It All Together

No single light source is composed entirely of any of the three types of light just described. Rather, it is made up of varying intensities of each. For example, a red laser beam in a lab is composed of almost a pure-red specular component. However, smoke or dust particles scatter the beam, so it can be seen traveling across the room. This scattering represents the diffuse component of the light. If the beam is bright and no other light sources are present, you’d notice objects in the room taking on a red hue. This would be a very small ambient component of that light.

Thus a light source in a scene is said to be composed of three lighting components: ambient, diffuse, and specular. Just like the components of a color, each lighting component is defined with an RGBA value that describes the relative intensities of red, green, and blue light that make up that component. (We will ignore the alpha component until Chapter 15.) For example, our red laser light might be described by the component values in Table 9-1.

Table 9-1 Color and Light Distribution for a Red Laser Light Source

  Red Green Blue Alpha

Specular 0.99 0.0 0.0 1.0
Diffuse 0.10 0.0 0.0 1.0
Ambient 0.05 0.0 0.0 1.0

Note that the red laser beam has no green or blue light. Also, note that specular, diffuse, and ambient light can each range in intensity from 0.0 to 1.0. You could interpret this table as saying that the red laser light in some scenes has a very high specular component, a small diffuse component, and a very small ambient component. Wherever it shines, you are probably going to see a reddish spot. Also, because of conditions (smoke, dust, etc.) in the room, the diffuse component will allow the beam to be seen traveling through the air. Finally, the ambient component—likely due to smoke or dust particles, as well—will scatter a tiny bit of light all about the room. Ambient and diffuse components of light are frequently combined because they are so similar in nature.

Materials in the Real World

Light is only part of the equation, though. In the real world, objects do have a color of their own. In Chapter 8, we described the color of an object as being defined by its reflected wavelengths of light. A blue ball reflects mostly blue photons and absorbs most others. This assumes that the light shining on the ball has blue photons in it to be reflected and detected by the observer. Generally, most scenes in the real world are illuminated by a white light containing an even mixture of all the colors. Under white light, therefore, most objects appear in their proper or “natural” colors. However, this is not always so; put the blue ball in a dark room with only a yellow light, and the ball would appear black to the viewer, because all the yellow light would be absorbed and there would be no blue to be reflected.

Material Properties

When we use lighting, we do not describe polygons as having a particular color, but rather as being made up of materials that have certain reflective properties. Instead of saying that a polygon is red, we say that the polygon is made of a material that reflects mostly red light. We are still saying that the surface is red, but now we must also specify the material’s reflective properties for ambient, diffuse, and specular light sources. A material may be shiny and reflect specular light very well, while absorbing most of the ambient or diffuse light. Conversely, a flat colored object may absorb all specular light and won’t be shiny under any circumstances. Another property to be specified is the emission property for objects that emit their own light, such as taillights or glow-in-the-dark watches.

Adding Light to Materials

Setting lighting and material properties to achieve the desired effect takes some practice. There are no color cubes or rules of thumb to give you quick and easy answers. This is where analysis gives way to art, and science yields to magic. The CD subdirectory for this chapter contains a supplementary sample program called MATLIGHT (for Materials and Lighting Studio). This program allows you to change material and lighting properties on the fly for a scene composed of some simple objects. You can use MATLIGHT to get a feel for the various lighting and material property settings. In addition, because the source is included, you can also substitute your own objects in MATLIGHT and work out the lighting and material details before committing your scene to code.

When drawing an object, OpenGL decides which color to use for each pixel in the object. That object has reflective “colors,” and the light source has “colors” of its own. How does OpenGL determine which colors to use? Understanding this is not difficult, but it does take some simple grade-school multiplication. (See, that teacher told you you’d need it one day!)

Each vertex of your primitives is assigned an RGB color value based on the net effect of the ambient, diffuse, and specular illumination multiplied by the ambient, diffuse, and specular reflectance of the material properties. By making use of smooth shading between the vertices, the illusion of illumination is achieved!

Calculating Ambient Light Effects

First you need to put away the notion of color and instead think only in terms of red, green, and blue intensities. For an ambient light source of half-intensity red, green, and blue components, you’d have an RGB value for that source of (0.5, 0.5, 0.5). If this ambient light illuminates an object with ambient reflective properties specified in RGB terms of (.50, 1.0, .50), then the net “color” component from the ambient light would be

(0.50 * .50, 0.5 * 1.0, 0.50 * .50) = (0.25, 0.5, 0.25)

which would be the result of multiplying each of the ambient light source terms by each of the ambient material property terms. See Figure 9-5.

Figure 9-5  Calculating the ambient color component of an object

Thus, the material color components actually determine the percentage of incident light that is reflected. In our example, the ambient light had a red component that was at one-half intensity, and the material ambient property of .5 specified that one-half of that half-intensity light was reflected. Half of a half is a fourth, or 0.25.

Previous Table of Contents Next