Silicon Graphics

[Prev] [TOC] [Next]



(-) OpenGL Programming Guide
(-) Chapter 6Lighting

Creating Light Sources

Light sources have a number of properties, such as color, position, and direction. The following sections explain how to control these properties and what the resulting light looks like. The command used to specify all properties of lights is glLight*(); it takes three arguments: to identify the light whose property is being specified, the property, and the desired value for that property. void glLight{if}[v](GLenum light, GLenum pname, TYPEparam);

Creates the light specified by light, which can be GL_LIGHT0, GL_LIGHT1, ... , or GL_LIGHT7. The characteristic of the light being set is defined by pname, which specifies a named parameter (see Table 6-1 ). The param argument indicates the values to which the pname characteristic is set; it's a pointer to a group of values if the vector version is used, or the value itself if the nonvector version is used. The nonvector version can be used to set only single-valued light characteristics.

Parameter NameDefault ValueMeaning

GL_AMBIENT

(0.0, 0.0, 0.0, 1.0)

ambient RGBA intensity of light

GL_DIFFUSE

(1.0, 1.0, 1.0, 1.0)

diffuse RGBA intensity of light

GL_SPECULAR

(1.0, 1.0, 1.0, 1.0)

specular RGBA intensity of light

GL_POSITION

(0.0, 0.0, 1.0, 0.0)

(x, y, z, w) position of light

GL_SPOT_DIRECTION

(0.0, 0.0, -1.0)

(x, y, z) direction of spotlight

GL_SPOT_EXPONENT

0.0

spotlight exponent

GL_SPOT_CUTOFF

180.0

spotlight cutoff angle

GL_CONSTANT_ATTENUATION

1.0

constant attenuation factor

GL_LINEAR_ATTENUATION

0.0

linear attenuation factor

GL_QUADRATIC_ATTENUATION

0.0

quadratic attenuation factor

Table 6-1 : Default Values for pname Parameter of glLight*()


The default values listed for GL_DIFFUSE and GL_SPECULAR in Table 6-1 apply only to GL_LIGHT0. For other lights, the default value is (0.0, 0.0, 0.0, 1.0) for both GL_DIFFUSE and GL_SPECULAR.

Here's an example of using glLight*():

GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

As you can see, arrays are defined for the parameter values, and glLightfv() is called repeatedly to set the various parameters. In this example, the first three calls to glLightfv() are superfluous, since they're being used to specify the default values for the GL_AMBIENT, GL_DIFFUSE, and GL_SPECULAR parameters.

Remember to turn on each light with glEnable(); see "Enabling Lighting" for more information about how to do this.

All the parameters for glLight*() and their possible values are explained in the following sections. These parameters interact with those that define the overall lighting model for a particular scene and an object's material properties. See "Selecting a Lighting Model" and "Defining Material Properties" for more information about these two topics. "The Mathematics of Lighting" explains how all these parameters interact mathematically.


(+) Color
(+) Position and Attenuation
(+) Spotlights
(+) Multiple Lights
(+) Controlling a Light's Position and Direction

[Prev] [TOC] [Next]

OpenGL Programming Guide


Maintained by Maintainer: Send e-mail to .htm">name@address.