Chapter Objectives
After reading this chapter, you'll be able to do the following:
Understand how real-world lighting conditions are approximated by OpenGL
Render illuminated objects by defining the desired light sources and lighting model
Define the material properties of the objects being illuminated
Manipulate the matrix stack to control the position of light sources
As you saw in Chapter 5 , OpenGL computes the color of each pixel in a final, displayed scene that's held in the framebuffer. Part of this computation depends on what lighting is used in the scene and on how objects in the scene reflect or absorb that light. As an example of this, recall that the ocean has a different color on a bright, sunny day than it does on a gray, cloudy day. The presence of sunlight or clouds determines whether you see the ocean as bright turquoise or murky gray-green. In fact, most objects don't even look three-dimensional until they're lit. Figure 6-1 shows two versions of the exact same scene (a single sphere), one with lighting and one without.
![[IMAGE]](figures/Fig6-1.gif)
Figure 6-1 : A Lit and an Unlit Sphere
As you can see, an unlit sphere looks no different from a two-dimensional disk. This demonstrates how critical the interaction between objects and light is in creating a three-dimensional scene.
With OpenGL, you can manipulate the lighting and objects in a scene to create many different kinds of effects. This chapter explains how to control the lighting in a scene. It discusses OpenGL's conceptual model of lighting, and it describes in detail how to set the numerous illumination parameters to achieve certain effects. Toward the end of the chapter, the mathematical computations that determine how lighting affects color are presented.
This chapter contains the following major sections:
"Real-World and OpenGL Lighting" explains in general terms how light behaves in the world and how OpenGL models this behavior.
"A Simple Example: Rendering a Lit Sphere" introduces OpenGL's lighting facility by presenting a short program that renders a lit sphere.
"Creating Light Sources" explains how to define and position light sources.
"Selecting a Lighting Model" discusses the elements of a lighting model and how to specify them.
"Defining Material Properties" explains how to describe the properties of objects so that they interact with light in a desired way.
"The Mathematics of Lighting" presents the mathematical calculations used by OpenGL to determine the effect of lights in a scene.
"Lighting in Color-Index Mode" discusses the differences between using RGBA mode and color-index mode for lighting.
Real-World and OpenGL Lighting
A Simple Example: Rendering a Lit Sphere
Creating Light Sources
Selecting a Lighting Model
Defining Material Properties
The Mathematics of Lighting
Lighting in Color-Index Mode
OpenGL Programming Guide