i
i
i
i
i
i
i
i
178 8. The Graphics Pipeline
into the output image. For some applications this is sufficient, but in many cases
we want 3D objects to be drawn with shading, using the same illumination equa-
tions that we used for image-order rendering in Chapter 4. Recall that these equa-
tions require a light direction, an eye direction, and a surface normal to compute
the color of a surface.
One way to handle shading computations is to perform them in the vertex
stage. The application provides normal vectors at the vertices, and the positions
and colors of the lights are provided separately (they don’t vary across the surface,
so they don’t need to be specified for each vertex). For each vertex, the direction
to the viewer and the direction to each light are computed based on the positions
of the camera, the lights, and the vertex. The desired shading equation is evaluated
tocomputeacolor,whichisthenpassedtothe rasterizer as the vertex color. Per-
vertex shading is sometimes called Gouraud shading.
One decision to be made is the coordinate system in which shading com-
putations are done. World space or eye space are good choices. It is impor-
tant to choose a coordinate system that is orthonormal when viewed in world
space, because shading equations depend on angles between vectors, which are
not preserved by operations like nonuniform scale that are often used in the mod-
eling transformation, or perspective projection, often used in the projection to the
canonical view volume. Shading in eye space has the advantage that we don’t
need to keep track of the camera position, because the camera is always at the
origin in eye space, in perspective projection, or the view direction is always +z
in orthographic projection.
Per-vertex shading has the disadvantage that it cannot produce any details in
the shading that are smaller than the primitives used to draw the surface, because
it only computes shading once for each vertex and never in between vertices.
Figure 8.13. Two
spheres drawn using per-
pixel (Gouraud) shading.
Because the triangles are
large, interpolation artifacts
are visible.
For instance, in a room with a floor that is drawn using two large triangles and
illuminated by a light source in the middle of the room, shading will be evaluated
only at the corners of the room, and the interpolated value will likely be much too
dark in the center. Also, curved surfaces that are shaded with specular highlights
must be drawn using primitives small enough that the highlights can be resolved.
Figure 8.13 shows our two spheres drawn with per-vertex shading.
8.2.5 Per-fragment Shading
Per-fragment shading is
sometimes called Phong
shading, which is confusing
because the same name
is attached to the Phong
illumination model.
To avoid the interpolation artifacts associated with per-vertex shading, we can
avoid interpolating colors by performing the shading computations after the in-
terpolation, in the fragment stage. In per-fragment shading, the same shading
equations are evaluated, but they are evaluated for each fragment using interpo-
lated vectors, rather than for each vertex using the vectors from the application.