GLSL Programming/GLUT/Glossy Textures

This tutorial covers per-pixel lighting of partially glossy, textured surfaces.

It combines the shader code of the tutorial on textured spheres and the tutorial on smooth specular highlights to compute per-pixel lighting with a material color for diffuse reflection that is determined by the RGB components of a texture and an intensity of the specular reflection that is determined by the A component of the same texture. If you haven't read the tutorial on textured spheres or the tutorial on smooth specular highlights, this would be a very good opportunity to read them.

Gloss Mapping
The tutorial on lighting textured surfaces introduced the concept of determining the material constant for the diffuse reflection by the RGB components of a texture image. Here we extend this technique and determine the strength of thea specular reflection by the A (alpha) component of the same texture image. Using only one texture offers a significant performance advantage, in particular because an RGBA texture lookup is under certain circumstances just as expensive as an RGB texture lookup.

If the “gloss” of a texture image (i.e. the strength of the specular reflection) is encoded in the A (alpha) component of an RGBA texture image, we can simply multiply the material constant for the specular reflection $$k_\text{specular}$$ with the alpha component of the texture image. $$k_\text{specular}$$ was introduced in the tutorial on specular highlights and appears in the specular reflection term of the Phong reflection model:

$$I_\text{specular} = I_\text{incoming}\,k_\text{specular} \max(0, \mathbf{R}\cdot \mathbf{V})^{n_\text{shininess}}$$

If multiplied with the alpha component of the texture image, this term reaches its maximum (i.e. the surface is glossy) where alpha is 1, and it is 0 (i.e. the surface is not glossy at all) where alpha is 0.



Shader Code for Per-Pixel Lighting
The shader code is a combination of the per-pixel lighting from the tutorial on smooth specular highlights and the texturing from the tutorial on textured spheres. Similarly to the tutorial on lighting textured surfaces, the RGB components of the texture color in  is multiplied to the ambient and diffuse lighting.

In the particular texture image to the left, the alpha component is 0 for water and 1 for land. However, it should be the water that is glossy and the land that isn't. Thus, with this particular image, we should multiply the specular material color with. On the other hand, usual gloss maps would require a multiplication with. (Note how easy it is to make this kind of changes to a shader program.)

The vertex shader is then: And the fragment shader becomes: The texture and sphere have to be set up as described in the tutorial on textured spheres.

A useful modification of this shader for the particular texture image above, would be to set the diffuse material color to a dark blue where the alpha component is 0.

Shader Code for Per-Vertex Lighting
As discussed in the tutorial on smooth specular highlights, specular highlights are usually not rendered very well with per-vertex lighting. Sometimes, however, there is no choice because of performance limitations. In order to include gloss mapping in the shader code of the tutorial on lighting textured surfaces, the fragment shader should be replaced with this code:

Note that a usual gloss map would require a multiplication with  instead of.

Summary
Congratulations! You finished an important tutorial about gloss mapping. We have looked at:
 * What gloss mapping is.
 * How to implement it for per-pixel lighting.
 * How to implement it for per-vertex lighting.