GLSL Programming/Blender/Lighting Textured Surfaces

This tutorial covers per-vertex lighting of textured surfaces.

It combines the shader code of the tutorial on textured spheres and the tutorial on specular highlights to compute lighting with a diffuse material color determined by a texture. If you haven't read the tutorial on textured spheres or the tutorial on specular highlights, this would be a very good opportunity to read them.

Texturing and Diffuse Per-Vertex Lighting
In the tutorial on textured spheres, the texture color was used as output of the fragment shader. However, it is also possible to use the texture color as any of the parameters in lighting computations, in particular the material constant $$k_\text{diffuse}$$ for diffuse reflection, which was introduced in the tutorial on diffuse reflection. It appears in the diffuse part of the Phong reflection model:

$$I_\text{diffuse} = I_\text{incoming}\,k_\text{diffuse} \max(0,\mathbf{N}\cdot \mathbf{L})$$

where this equation is used with different material constants for the three color components red, green, and blue. By using a texture to determine these material constants, they can vary over the surface.

Shader Code
In comparison to the per-vertex lighting in the tutorial on specular highlights, the vertex shader here computes two varying colors:  is multiplied with the texture color in the fragment shader and   is just the specular term, which shouldn't be multiplied with the texture color. This makes perfect sense but for historically reasons (i.e. older graphics hardware that was less capable) this is sometimes referred to as “separate specular color”.

And the fragment shader modulates the  with the texture color and adds the  :

In order to assign a texture image to this shader, you should follow the steps discussed in the tutorial on textured spheres; in particular, the Python script has to set the value of the uniform, e.g. with:

Summary
Congratulations, you have reached the end. We have looked at:
 * How texturing and per-vertex lighting are usually combined.
 * What a “separate specular color” is.