Section 4.1. Overview

4.1. Overview

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:

  • Ambient light simulates indirect lighting. It illuminates all geometry in your scene at the same intensity.

  • Diffuse light illuminates a surface based on its orientation to a light source. OpenGL diffuse lighting adheres to Lambert's law, in which the amount of illumination is proportional to the cosine of the angle between the surface normal and the light vector, and the diffused light reflects equally in all directions (Blinn 1977).

  • Specular light approximates the reflection of the light source on a shiny surface. OpenGL specular highlights adhere to the simple Phong illumination model (Phong 1975), in which the specular highlight peaks along the incident light reflection vector,[1] rather than more complex specular highlight models such as Torrance-Sparrow (Torrance 1967).

    [1] Phong also outlined a shading method for interpolating surface normals during rasterization to render "perfect" specular highlights. OpenGL lighting is per-vertex unless you use fragment shaders (see OpenGL® Shading Language), and uses the Gouraud shading method to interpolate color values during rasterization.

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:


It multiplies the current ambient material color by the ambient light in the scene.


It multiplies the current ambient material color by the ambient light color of each enabled light source.


For each enabled light, OpenGL multiplies the current diffuse material color by the diffuse light color and scales the result by the dot product of the current normal and a vector to the light.


For each enabled light, OpenGL multiplies the current specular material color by the specular light color. If the dot product of the current normal and the light's reflection vector is greater than zero, it scales the result by this dot product raised to the specular exponent.

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.

OpenGL Distilled
OpenGL Distilled
ISBN: 0321336798
EAN: 2147483647
Year: 2007
Pages: 123
Authors: Paul Martz

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: