2.4 Lighting

Lighting is a bit more complicated than the primitive color and vertex color processes, since the following two changes are required:

Defining the light will be covered in the next section. Here we will explain how to add normal vector information to the model.

List 2-3

  static Vtx  cube_vtx[] = {
    /*
     *  Vertex information when lighting is ON.
     *  Normal vectors are stored here, rather than vertex color.
     *  This is for smooth shading.
     */
    {-10, -10, -10, 0, 0, 0, -73, -73, -73, 255},
    { 10, -10, -10, 0, 0, 0,  73, -73, -73, 255},
    { 10,  10, -10, 0, 0, 0,  73,  73, -73, 255},
    {-10,  10, -10, 0, 0, 0, -73,  73, -73, 255},
    {-10, -10,  10, 0, 0, 0, -73, -73,  73, 255},
    { 10, -10,  10, 0, 0, 0,  73, -73,  73, 255},
    { 10,  10,  10, 0, 0, 0,  73,  73,  73, 255},
    {-10,  10,  10, 0, 0, 0, -73,  73,  73, 255},
  };

The Vtx union for storing vertex information is used as type Vtx_t when setting vertex color, and as type Vtx_tn when setting normal vectors.

The only difference between type Vtx_t and type Vtx_tn is that one holds "the vertex color RGB components" and the other holds "the normal vector x,y,z components." All other elements are common to both. The thing you need to be aware of is that when treating them as normal vector component values, each element becomes a signed 8bit number. Also, the normal vectors need to be normalized.

In other words, you must scale so that either of the following conditions is met:

sqrt(x * x + y * y + z * z) == 127

or

x * x + y * y + z * z == 127 * 127

Since lighting is one of the N64 shading methods, you can choose to perform either flat shading or smooth shading.

For flat shading, the lighting is calculated using the vertex normal information specified in the last argument of the gSP1Triangle command, and the resulting color is applied to the entire surface. The surface color will vary depending on the direction of the surface and the light settings, but any one surface will be filled with only one color.