In reality, light is electromagnetic radiation in the visible spectrum. The apparent color of an object is a function of the wavelengths it reflects or absorbs, the wavelengths emitted by light source(s) shining on the object and their intensities, whether the object surface is glossy or dull, the position of the object relative to the light source(s) and viewer, and the presence of other objects that might contribute reflected (indirect) light.
Like most real-time 3D graphics APIs, OpenGL features a simpler lighting model that is powerful enough to approximate real-world lighting adequately.
4.1.1. Ambient, Diffuse, and Specular Light
OpenGL features three types of light, as shown in Figure 4-1:
Figure 4-1. An example of ambient, diffuse, and specular light reflection.
4.1.2. Controlling OpenGL Lighting
Enable OpenGL lighting by calling glEnable( GL_LIGHTING ), and disable it by calling glDisable( GL_LIGHTING ).
By default, lighting is disabled, and OpenGL uses the current primary color, as set by your application with glColor3f(), to color rendered primitives.
When lighting is enabled, however, OpenGL doesn't use the current primary color. Instead, it uses the current material colors, as set by your application with calls to glMaterial*(). Material colors determine the amount of ambient, diffuse, and specular light reflected by geometry. For information on controlling these values, see "Material Parameters" later in this chapter.
OpenGL supports at least eight active light sources at any given time. Each light source can be individually enabled or disabled.
Your application will need to enable individual OpenGL light sources, as well as set light parameters to control the colors and intensities of the ambient, diffuse, and specular light they emit. For information on controlling these values, see "Light Parameters" later in this chapter. To control light-source position and direction, see "Positional and Directional Lights" later in this chapter.
OpenGL computes both diffuse and specular light based on the orientation of the geometry with respect to the light (and to the eye for specular light).
OpenGL uses the current vertex normal to determine orientation. For information on specifying unit length normals, see the section "Normals" later in this chapter.
4.1.3. Minimal Lighting Code
At first glance, controlling OpenGL lighting might appear complex. The amount of code required to obtain simple lighting effects, however, is actually quite small. The minimal code for enabling lighting effects is
// Enable OpenGL lighting glEnable( GL_LIGHTING ); // Enable a single light source glEnable( GL_LIGHT0 ); // Specify geometry with unit length normals // ...
This code enables lighting with a single light source, GL_LIGHT0, and otherwise uses default lighting parameters. This results in a white directional light shining directly forward, illuminating a nonglossy white surface. The SimpleLighting code example at the OpenGL® Distilled Web site demonstrates this simple technique.
4.1.4. Internal Lighting Computation
When lighting is enabled, OpenGL computes lighting values as part of the vertex processing render stage. For each vertex in your geometry, OpenGL replaces the vertex primary color with the computed lighting value.
OpenGL computes lighting values using the following steps:
OpenGL sums the color values computed in steps 1 through 4 to arrive at the final lit color for the current vertex. (This is a brief and incomplete summary of the OpenGL lighting equations. For a complete discussion, see Section 2.14.1, "Lighting," of The OpenGL Graphics System and Chapter 5, "Lighting," of OpenGL® Programming Guide.