i
i
i
i
i
i
i
i
18.1. Buffers and Buffering 833
18.1.2 Z-Buffering
Depth resolution is important because it helps avoid rendering errors. For
example, say you modeled a sheet of paper and placed it on a desk, ever
so slightly above the desk’s surface.
3
With precision limits of the z-depths
computed for the desk and paper, the desk can poke through the paper at
variousspots.Thisproblemissometimescalledz-fighting.
AswesawinSection2.4,theZ-buffer (also called the depth buffer)
can be used to resolve visibility. This kind of buffer typically has 24 bits
per pixel. For orthographic viewing, the corresponding world space dis-
tance values are proportional to the z-value, and so the depth resolution is
uniform. For example, say the world space distance between the near and
far planes is 100 meters, and the Z-buffer stores 16 bits per pixel. This
means that the distance between any adjacent depth values is the same,
100 meters/2
16
, i.e., about 1.5 millimeters.
However, for perspective viewing, this is not the case; the distribution
is non-uniform. After applying the perspective transform (Equations 4.68–
4.70), a point v =(v
x
,v
y
,v
z
,v
w
) is obtained. Next, division by v
w
is
done, giving (v
x
/v
w
,v
y
/v
w
,v
z
/v
w
, 1). The value v
z
/v
w
is mapped from its
valid range (e.g., [0, 1] for DirectX) to the range [0, 2
b
− 1] and is stored
in the Z-buffer, where b is the number of bits. The characteristics of the
perspective transform matrix result in greater precision for objects closer
to the viewer (see page 97). This is especially true when there is a large
difference between the near and the far values. When the near value is
close to the far value, the precision tends to be uniform over the depth.
To return to the paper and desk example, this change in precision can
mean that as the viewer moves farther away from the paper, the desk
poke-through problem occurs more frequently.
18.1.3 Single, Double, and Triple Buffering
In Section 2.4, we mentioned that double buffering is used in order for
the viewer not to see the actual rendering of the primitives. Here, we will
describe single, double, and even triple buffering.
Assume that we have only a single buffer. This buffer has to be the
one that is currently shown to the viewer. As polygons for a frame are
drawn, we will see more and more of them as the monitor refreshes—an
unconvincing effect. Even if our frame rate is equal to the monitor’s update
rate, single buffering has problems. If we decide to clear the buffer or draw
a large polygon, then we will briefly be able to see the actual partial changes
3
If the paper were placed exactly at the same height as the desk, i.e., the paper
and desk were made coplanar, then there would be no right answer without additional
information about their relationship. This problem is due to poor modeling and cannot
be resolved by better z precision.