Basic Shading
Pick up a book and hold it under a nearby light source. Rotate the book around. When does the cover of the book appear brightest? When does the cover appear darkest?
When the cover is facing exactly toward the light source, its surface is brightest. When the cover is facing to the side or away from the light source, its surface is not illuminated by the light source at all—at least not directly. To recreate in a renderer the lighting that you see in the physical world, we must come up with a scheme to measure how much a surface is facing a light source.
In computer graphics, we describe which direction a surface faces with a normal vector, which is perpendicular to the surface. Explore how this triangle's illumination changes with respect to its normal and the yellow light source as you rotate the triangle:
Click and drag on the triangle to rotate just it. Click and drag on the background to rotate the whole scene. If you see red, you are looking at the unilluminated back face of the triangle. Normally, models are solid and back faces are not rendered.
Altering a triangle's coloring based on its relationship to light sources is called shading. The work of shading is performed in a shader; thus its name. In this chapter, we examine one common shading algorithm. By its end, you'll be able to answer the following questions:
- How are normals defined for a surface?
- How are effects like matte finishes, shiny highlights, and global illumination approximated by the ambient, diffuse, and specular terms of the basic shading algorithm?
- In what ways does color alter a surface's shading?
- What light reflects off a surface when it is illuminated by multiple light sources?
Many modern renderers use shading algorithms that are more physically accurate than the one we'll discuss. They are also more complex and expensive. Our basic shading algorithm is a reasonable place to start.