# Cg Programming/Unity/Glossy Textures

This tutorial covers per-pixel lighting of partially glossy, textured surfaces.
It combines the shader code of Section “Textured Spheres” and Section “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 those sections, this would be a very good opportunity to read them.

## Gloss Mapping

In Section “Lighting Textured Surfaces”, the material constant for the diffuse reflection was determined by the RGB components of a texture image. Here we extend this technique and determine the strength of the 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 Section “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 Section “Smooth Specular Highlights” and the texturing from Section “Textured Spheres”. Similarly to Section “Lighting Textured Surfaces”, the RGB components of the texture color in textureColor is multiplied to the diffuse material color _Color.
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 (1.0 - textureColor.a). On the other hand, usual gloss maps would require a multiplication with textureColor.a. (Note how easy it is to make this kind of changes to a shader program.)
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 Section “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 Section “Lighting Textured Surfaces”, the fragment shaders of both passes should be replaced with this code:
Note that a usual gloss map would require a multiplication with textureColor.a instead of (1.0 - textureColor.a).

## 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.

## Further reading

If you still want to learn more
* about per-pixel lighting (without texturing), you should read Section “Smooth Specular Highlights”.
* about texturing, you should read Section “Textured Spheres”.
* about per-vertex lighting with texturing, you should read Section “Lighting Textured Surfaces”.
< Cg Programming/Unity