If you have OpenGL lighting enabled, you will need to send an additional piece of information for primitives called a normal, which is a unit vector (a vector of length 1.0) that faces outward from a 3D shape. This vector is necessary for calculating the shading from lights in the scene. For example, if you have a cube in the center of the screen aligned along the axis, the normal for the front face is (0, 0, 1) because it is facing along the positive z axis, and the normal for the right face is (1, 0, 0) because it faces along the x axis (see Figure 9-8).

To send a normal to OpenGL, use the glNormal3d function, which takes three values for the normal vector, or the glNormal3dv function, which takes a sequence of three values. For example, if the square in Listing 9-6 was the front face of a cube you would set the normal with glNormal3d(0, 0, 1) or glNormal3dv(front_vector). The latter is useful because it can be used with Vector3 objects. If you are using flat shading (glShadeModel(GL_FLAT)), you will need one normal per face. For smooth shading (glShadeModel(GL_SMOOTH)), you will need to supply a normal per vertex.

Was this article helpful?

0 0

Post a comment