443
back-to-front order (the so-called painter’s algorithm ). However, as shown in
Figure 10.37, this doesn’t work if the triangles are intersecting one another.
To implement triangle occlusion properly, independent of the order in
which the triangles are rendered, rendering engines use a technique known
as depth buff ering or z-buff ering. The depth buff er is a full-screen buff er that
typically contains 16- or 24-bit fl oating-point depth information for each pix-
el in the frame buff er. Every fragment has a z-coordinate that measures its
depth “into” the screen. (The depth of a fragment is found by interpolating
the depths of the triangle’s vertices.) When a fragment’s color is writt en into
the frame buff er, it depth is stored into the corresponding pixel of the depth
buff er. When another fragment (from another triangle) is drawn into the same
pixel, the engine compares the new fragment’s depth to the depth already
present in the depth buff er. If the fragment is closer to the camera (i.e., if it
has a smaller depth), it overwrites the pixel in the frame buff er. Otherwise the
fragment is discarded.
Z-Fighting and the W-Buffer
When rendering parallel surfaces that are very close to one another, it’s im-
portant that the rendering engine can distinguish between the depths of the
two planes. If our depth buff er had infi nite precision, this would never be
a problem. Unfortunately, a real depth buff er only has limited precision, so
the depth values of two planes can collapse into a single discrete value when
the planes are close enough together. When this happens, the more-distant
plane’s pixels start to “poke through” the nearer plane, resulting in a noisy
eff ect known as z-fi ghting.
To reduce z-fi ghting to a minimum across the entire scene, we would like
to have equal precision whether we’re rendering surfaces that are close to the
camera or far away. However, with z-buff ering this is not the case. The preci-
sion of clip-space z-depths (pHz) are not evenly distributed across the entire
range from the near plane to the far plane, because of the division by the view-
space z-coordinate. Because of the shape of the 1/z curve, most of the depth
buff er’s precision is concentrated near the camera.
The plot of the function pHz=1/pVz shown in Figure 10.38 demonstrates
this eff ect. Near the camera, the distance between two planes in view space
ΔpVz gets transformed into a reasonably large delta in clip space, ΔpHz. But
far from the camera, this same separation gets transformed into a tiny delta in
clip space. The result is z fi ghting, and it becomes rapidly more prevalent as
objects get farther away from the camera.
To circumvent this problem, we would like to store view-space z-coor-
dinates (pVz) in the depth buff er instead of clip-space z-coordinates (pHz).
View-space z-coordinates vary linearly with the distance from the camera, so
10.1. Foundations of Depth-Buffered Triangle Rasterization