In which you learn how to describe the direction a surface faces.
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 the lighting that you see in nature, you must come up with a scheme for measuring how much a surface is facing a light source.
In computer graphics, you describe which direction a surface faces with a normal vector. 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.
Altering a triangle's appearance based on its relationship to light sources is called shading. The work of shading is performed in a shader, which explains its name.
For simple shapes, you can reason out what the normals should be. For example, the normals on the right face of a cube point right. For more complex shapes, like a two-headed Tyrannosaurus Rex, you will want to compute normals with an algorithm. You will write that algorithm in a future chapter. For the time being, you'll stick to simple shapes.
Normals should always have a magnitude of 1. This will simplify later calculations. If you have a normal stored in a
Vector3, you can normalize it with the method you wrote earlier:
normal = normal.normalize();
The term "normal" is overloaded. A normalized vector has unit length. A normal vector is a vector perpendicular to a surface—and it also happens to be normalized.