i
i
i
i
i
i
i
i
158 7. The Rendering Pipeline
trated. Imagine adding a matching array of similar dimension that is capable
of recording a real number for each fragment. Let us call this the Z-buffer.
The main action of the Z-buffer algorithm is to record the depth coordinate
(or the x-coordinate) associated with the 3D point P into the buffer at the
address (i, j). This action is repeated for all fragments inside the projection of
polygon k.
When it comes to drawing another polygon, say l,ittoowillpaintintoa
set of fragments, some of which may overlap with those previously filled with
data from polygon k. Now the Z-buffer comes into play. Before informa-
tion for polygon l is written to the fragment at location (i, j), the Z-buffer is
checked to see whether polygon l appears to be in front of polygon k.Ifl is in
front of k then the data from l is placed in the frame buffer and the Z-buffer
depth at location (i, j) is updated to take account of l’s depth.
The algorithm is known as the Z-buffer algorithm because the first pro-
grams to use it arranged their frame of reference with the viewpoint at the
origin and the direction of view aligned along the z-axis. In these programs,
the distance to any point on the polygon from the viewpoint was simply the
point’s z-coordinate. (OpenGL and Direct3D use this direction for their di-
rection of view.) The Z-buffer algorithm is summarized in Figure 7.6.
To find P, and hence its x-coordinate, we call on the ideas of Section 6.4.1
for the intersection of line and plane. Initially, we n eed to transform the 2D
Fill the depth buffer at Z (i, j)witha“far away” depth;
i.e., set Z(i, j) = ∞ for all i, j.
Repeat for all polygons k {
For polygon k find pixels (i, j) covered by it. Fill
thesewiththecolorortextureofpolygonk.
With each pixel (i, j) covered by k repeat {
Calculate the depth (Δ)ofP from V (see Figure 7.5)
If Δ < Z(i, j) {
Set pixel (i, j) to color of polygon k
Update the depth buffer Z(i, j) = Δ
}
}
}
Figure 7.6. The b asic ideas of the Z-buffer rendering algorithm.