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
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
GL_MODULATE mode is used when objects are both textured and lighted
Example: texlight.py
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)
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.
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])
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 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_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.)
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.
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 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.)
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
The total color, from all lighting, is:
color = color_from_light0 + color_from_light1 + color_from_light2 + ... + material_ambient * lightmodel_ambient + material_emission