i
i
i
i
i
i
i
i
8.2. Operations Before and After Rasterization 175
efficient flow of data that makes object-order rendering so fast. Figure 8.10 shows
the result of this process when the objects are not sorted by depth.
Figure 8.10. The result
of drawing two spheres of
identical size using the min-
imal pipeline. The sphere
that appears smaller is far-
ther away but is drawn last,
so it incorrectly overwrites
the nearer one.
8.2.3 Using a z-Buffer for Hidden Surfaces
In practice the painter’s algorithm is rarely used; instead a simple and effective
hidden surface removal algorithm known as the z-buffer algorithm is used. The
method is very simple: at each pixel we keep track of the distance to the closest
surface that has been drawn so far, and we throw away fragments that are farther
away than that distance. The closest distance is stored by allocating an extra value
for each pixel, in addition to the red, green, and blue color values, which is known
as the depth, or z-value. The depth buffer, or z-buffer, is the name for the grid of
depth values.
The z-buffer algorithm is implemented in the fragment blending phase, by
comparing the depth of each fragment with the current value stored in the z-buffer.
If the fragment’s depth is closer, both its color and its depth value overwrite the
values currently in the color and depth buffers. If the fragment’s depth is farther
Of course there can be ties
in the depth test, in which
case the order may well
matter.
away, it is discarded. To ensure that the first fragment will pass the depth test, the z
buffer is initialized to the maximum depth (the depth of the far plane). Irrespective
of the order in which surfaces are drawn, the same fragment will win the depth
test, and the image will be the same.
The z-buffer algorithm requires each fragment to carry a depth. This is done
simply by interpolating the z-coordinate as a vertex attribute, in the same way that
color or other attributes are interpolated.
The z-buffer is such a simple and practical way to deal with hidden surfaces in
object-order rendering that it is by far the dominant approach. It is much simpler
than geometric methods that cut surfaces into pieces that can be sorted by depth,
because it avoids solving any problems that don’t need to be solved. The depth
Figure 8.11. The result
of drawing the same two
spheres using the z-buffer.
order only needs to be determined at the locations of the pixels, and that is all
that the z-buffer does. It is universally supported by hardware graphics pipelines
and is also the most commonly used method for software pipelines. Figure 8.11
shows an example result.
Precision Issues
In practice, the z-values stored in the buffer are non-negative integers. This is
preferable to true floats because the fast memory needed for the z-buffer is some-
what expensive and is worth keeping to a minimum.
The use of integers can cause some precision problems. If we use an integer
range having B values {0, 1,...,B−1}, we can map 0 to the near clipping plane