JavaScript EditorFree JavaScript Editor     Ajax Editor 

Main Page
Previous Page
Next Page

9.4. Two-Sided Lighting

To mimic OpenGL's two-sided lighting behavior, you need to invert the surface normal and perform the same computations as defined in the preceding section, using the back-facing material properties. You can probably do it more cleverly than this, but it might look like Listing 9.13. The functions DirectionalLight, PointLight, and SpotLight that are referenced in this code segment are identical to the functions described in Section 9.2 except that the value glBackMaterial.shininess is used in the computations instead of the value glFrontMaterial.shininess.

Listing 9.13. Two-sided lighting computation

normal = -normal;

// Clear the light intensity accumulators
amb  = vec4(0.0);
diff = vec4(0.0);
spec = vec4(0.0);

// Loop through enabled lights, compute contribution from each
for (i = 0; i < NumEnabledLights; i++)
    if (gl_LightSource[i].position.w == 0.0)
        DirectionalLight(i, normal, amb, diff, spec);
    else if (gl_LightSource[i].spotCutoff == 180.0)
        PointLight(i, eye, ecPosition3, normal, amb, diff, spec);
        SpotLight(i, eye, ecPosition3, normal, amb, diff, spec);

color = gl_BackLightModelProduct.sceneColor +
        amb * gl_BackMaterial.ambient +
        diff * gl_BackMaterial.diffuse;

if (SeparateSpecular)
    gl_BackSecondaryColor = vec4(spec *
                                 gl_BackMaterial.specular, 1.0);
    color += spec * gl_BackMaterial.specular;

gl_BackColor = color;

There is no need to perform clamping on the values assigned to gl_BackSecondaryColor and gl_BackColor because these are automatically clamped by definition.

Previous Page
Next Page

JavaScript EditorAjax Editor     JavaScript Editor