Lighting in Color-index Mode (OpenGL Programming)

In color-index mode, the parameters comprising RGBA values either have no effect or have a special interpretation. Since it’s much harder to achieve certain effects in color-index mode, you should use RGBA whenever possible. In fact, the only light-source, lighting-model, or material parameters in an RGBA form that are used in color-index mode are the light-source parameters GL_DIFFUSE and GL_SPECULAR and the material parameter GL_SHININESS. GL_DIFFUSE and GL .SPECULARtmp10a085_thumb respectively) are used to compute color-index diffuse and specular light intensitiestmp10a086_thumbas    follows:

tmp10a089_thumb


wheretmp10a090_thumbrefer    to    the    red,    green, and blue components, respectively, of color x. The weighting values 0.30, 0.59, and 0.11 reflect the "perceptual" weights that red, green, and blue have for your eye—your eye is most sensitive to green and least sensitive to blue.

To specify material colors in color-index mode, use glMaterial*() with the special parameter GL_COLOR_INDEXES, as follows:

tmp10a092_thumb

The three numbers supplied for GL_COLOR_INDEXES specify the color indices for the ambient, diffuse, and specular material colors, respectively. In other words, OpenGL regards the color associated with the first index (16.0 in this example) as the pure ambient color, with the second index (47.0) as the pure diffuse color, and with the third index (79.0) as the pure specular color. (By default, the ambient color index is 0.0, and the diffuse and specular color indices are both 1.0. Note that glColorMaterial() has no effect on color-index lighting.)

As it draws a scene, OpenGL uses colors associated with indices between these numbers to shade objects in the scene. Therefore, you must build a color ramp between the indicated indices (in this example, between indices 16 and 47, and then between indices 47 and 79). Often, the color ramp is built smoothly, but you might want to use other formulations to achieve different effects. Here’s an example of a smooth color ramp that starts with a black ambient color and goes through a magenta diffuse color to a white specular color:

tmp10a093_thumb

The GLUT library command glutSetColor() takes four arguments. It associates the color index indicated by the first argument with the RGB triplet specified by the last three arguments. When i = 0, the color index 16 is assigned the RGB value (0.0, 0.0, 0.0), or black. The color ramp builds smoothly up to the diffuse material color at index 47 (when i = 31), which is assigned the pure magenta RGB value (1.0, 0.0, 1.0). The second loop builds the ramp between the magenta diffuse color and the white (1.0, 1.0, 1.0) specular color (index 79). Plate 15 shows the result of using this color ramp with a single lit sphere.

The Mathematics of Color-index Mode Lighting

Advanced

As you might expect, since the allowable parameters are different for color-index mode than for RGBA mode, the calculations are different as well. Since there’s no material emission and no ambient light, the only terms of interest from the RGBA equations are the diffuse and specular contributions from the light sources and the shininess. Even these terms need to be modified, however, as explained next.

Begin with the diffuse and specular terms from the RGBA equations. In the diffuse term, instead of diffuse)^ * diffusemateriai, substitute dc, as defined in the preceding section for color-index mode. Similarly, in the specular term, instead of specularlight * specularmaterial, use sci as defined in the preceding section. (Calculate the attenuation, the spotlight effect, and all other components of these terms as before.) Call these modified diffuse and specular terms d and 5, respectively. Now, let s’ = min{ s, 1 ), and then compute

tmp10a094_thumb

wheretmp10a095_thumbare    the    ambient,    diffuse,    and    specular    material    indices specified using GL_COLOR_INDEXES. The final color index is

tmp10a097_thumb

After lighting calculations are performed, the color-index values are converted to fixed-point (with an unspecified number of bits to the right of the binary point). Then the integer portion is masked (bitwise ANDed) with 2" – 1, where n is the number of bits in a color in the color-index buffer.

Next post:

Previous post: