OpenGL Distilled

4.3. Light Parameters

OpenGL supports eight or more light sources. Call glGetIntegerv() to query the number of supported light sources:

GLint maxLights; glGetIntegerv( GL_MAX_LIGHTS, &maxLights ).

Enable and disable individual light sources with glEnable( GL_LIGHTi ) or glDisable( GL_LIGHTi ), respectively, where i is zero to (GL_MAX_LIGHTS 1), inclusive. GL_LIGHTi follows the rule GL_LIGHTi = GL_LIGHT0 + i to simplify setting light parameters with iterative code.

To set the ambient, diffuse, and specular light colors, or the light position, call glLight*v().

void glLight[fd]v( GLenum light, GLenum pname, const TYPE* param )

Controls light-source parameters. light is the light to modify, for example, GL_LIGHT0. pname is the light parameter, and param is the new value for the parameter.

Valid values for pname include GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, and GL_POSITION. For ambient, diffuse, and specular parameters, param should point to an RGBA color value. For the position parameter, param should point to an xyzw homogenous location vector. For more information on setting the light position, see "Positional and Directional Lights" later in this chapter.

The following code configures GL_LIGHT1 to emit pale-yellow diffuse light and white specular light:

const GLfloat paleYellow[4] = { 1.f, 1.f, .75f, 1.f }; glLightfv( GL_LIGHT1, GL_DIFFUSE, paleYellow ); const GLfloat white[4] = { 1.f, 1.f, 1.f, 1.f }; glLightfv( GL_LIGHT1, GL_SPECULAR, white );

By default, all OpenGL lights emit zero-intensity ambient light; their ambient contribution to the primitive color is zero. All lights except GL_LIGHT0 also default to zero-intensity diffuse and specular light. GL_LIGHT0 is different; by default, it emits full-intensity (white) diffuse and specular light. This allows an application to obtain simple lighting effects quickly by calling

glEnable( GL_LIGHTING ); glEnable( GL_LIGHT0 );

4.3.1. Typical Usage

The default ambient color for all lights is zero intensity. OpenGL's light model feature, however, adds a dark-gray ambient light to the entire scene, which is adequate for many applications. For more information, see "glLightModel" in OpenGL® Reference Manual. Some applications also set a nonzero ambient intensity for each light, which increases the ambient scene illumination for each enabled light source.

Applications frequently set the diffuse and specular light parameters to full intensity (white). Keep in mind that this is the default value only for GL_LIGHT0; other lights default to zero intensity. Note that full-intensity light and material parameters could result in complete saturation. OpenGL handles such cases by clamping the result of light calculations to not exceed full intensity. To avoid clamping artifacts, consider that the sum of the effects from all enabled light sources should not exceed the RGB value (1, 1, 1).

Applications usually set the light position and direction. See "Positional and Directional Lights" later in this chapter for more information.

4.3.2. More Information

Other glLight*() pname parameters let you create spotlights and control attenuation. OpenGL spotlights restrict illumination to a cone of light. They're not commonly used because they tend to exaggerate per-vertex lighting artifacts. Light attenuation diminishes the illumination as a function of distance between geometry and the light source.

For more information, see Chapter 5, "Lighting," of OpenGL® Programming Guide; "glLight" in OpenGL® Reference Manual; and Section 2.14.1, "Lighting," of The OpenGL Graphics System.

Категории

© amp.flylib.com,