Texture Environment

Textures provide color information, but so does glColor or lighting.

The texture environment controls how these colors interact.


A "GL_REPLACE" environment uses just the texture color, and ignores everything else.

A "GL_MODULATE" environment combines the texture and other color, multiplying them together.

Example: texenv.py






Texture Environment

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode)

mode can be one of GL_MODULATE, GL_REPLACE, GL_DECAL, GL_BLEND


If Ct is the texture color, and Cf is the "fragment" color:

  For GL_MODULATE, Color = Ct * Cf

  For GL_REPLACE, Color = Ct






Texture + Lighting

GL_MODULATE mode is used when objects are both textured and lighted

Example: texlight.py






Spotlights

Normally, local light sources shine equally in all directions.
Using spotlight options, you can have a light shine in a particular direction.

A spotlight's beam is a cone starting from the light position and pointing in the spotlight direction (GL_SPOT_DIRECTION).

The angle of the cone is defined by the GL_SPOT_CUTOFF value.

GL_SPOT_EXPONENT makes the brightness of the spotlight fall off toward the edges.


glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction)
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, angle)
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, exponent)





Spotlights

All lighting calculations are done only at the vertices.

Spotlights, like specular highlights, will not work well with large polygons.

The geometry must be finely tesselated for a good spotlight effect.






Emissive Material

A material can have an emission component.

This is a "flat" color that is not affected by orientation or any light sources.
It represents light emitted, rather than reflected, by the surface.

It's equivalent to adding in an ordinary glColor.


glMaterialfv(GL_FRONT, GL_EMISSION, [1, 1, 0, 1])





Other OpenGL Lighting Features

Attenuation
Light intensity falls off with distance
GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION options of glLight

Two-sided lighting
Allows backs of polygons to reflect light; can be different material than front
GL_LIGHT_MODEL_TWO_SIDE option of glLightModel; GL_BACK option of glMaterial

Color-material mode
Allows material values to be changed by glColor
glEnable(GL_COLOR_MATERIAL); glColorMaterial function

Secondary specular color
Specularity is added in after texture mapping
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR)

Rescaling normals
glScale can affect normals adversely
glEnable(GL_NORMALIZE) or glEnable(GL_RESCALE_NORMAL)





Lighting Math






Lighting Math

The lighted color at a point is a combination of the diffuse, specular, and ambient lighting contributions.

Each of these contributions is a product of the color of the light source, the color of the material, and the intensity of that type of lighting.

e.g.:

   diffuse_color = diffuse_intensity *
                   light_diffuse_color *
                   material_diffuse_color





Diffuse Lighting Math

Diffuse lighting is proportional to the cosine of the angle between the surface normal and the vector to the light source.

This is computed by the dot product of the two vectors.






Diffuse Lighting Math

diffuse_intensity = normal  dot  light_dir

                  = normal.x * light_dir.x
                    + normal.y * light_dir.y
                    + normal.z * light_dir.z



R  =  diffuse_intensity * light.red * material.red
G  =  diffuse_intensity * light.green * material.green
B  =  diffuse_intensity * light.blue * material.blue

(light.red is the red value of the GL_DIFFUSE color of the light source, etc.)






Specular Lighting Math

The intensity of specular lighting is maximum if the eye point is in the same direction as the light reflected off of the surface.
It falls off as the eye point moves away from this direction.

The intensity is raised to an exponent - the GL_SHININESS value - to control the tightness of the specular highlight.
A larger exponent yields a faster falloff, and a smaller highlight.






Specular Lighting Math

s_vector = normalize( light_dir + view_dir )

specular_intensity = (s_vector dot normal)shininess


R  =  specular_intensity * light.r * material.r
G  =  specular_intensity * light.g * material.g
B  =  specular_intensity * light.b * material.b

(light.r is the red value of the GL_SPECULAR color of the light source, etc.)






Ambient Lighting Math

Ambient lighting is independent of direction.

The intensity of the ambient illumination is simply the ambient value of the light source.

 

R = light.r * material.r
G = light.g * material.g
B = light.b * material.b

(light.r is the red value of the GL_AMBIENT color of the light source, etc.)






Lighting Math

The total color, from a single light, is:

color =  diffuse * material_diffuse * light_diffuse
       + specular * material_specular * light_specular
       + material_ambient * light_ambient

diffuse =  surface_normal dot light_direction

specular =  (normalize(light_direction + view_direction)
             dot normal)shininess





Lighting Math

The total color, from all lighting, is:

color =  color_from_light0
       + color_from_light1
       + color_from_light2
       + ...
       + material_ambient * lightmodel_ambient
       + material_emission


Creative Commons License
This document is by Dave Pape, and is released under a Creative Commons License.