[Hill: Chapter 13. Foley & van Dam: p. 649-651]
[Hill: 436-439. Foley & van Dam: p. 668-672]
for all i,j { Depth[i,j] = MAX_DEPTH Image[i,j] = BACKGROUND_COLOUR } for all polygons P { for all pixels in P { if (Z_pixel < Depth[i,j]) { Image[i,j] = C_pixel Depth[i,j] = Z_pixel } } }
Characteristics of the z-buffer algorithm:
When does the z-buffer perform poorly?
Scan conversion proceeds much like the 2D case, only now appropriate Z-values must be generated. In order to determine z = f(x,y), we begin with the plane equation of the polygon in device coordinates as given by
0 = A x + B y + C z + D,
We can solve for z as
z = ( - A x - B y - D ) /C.
The value of z at the adjacent pixel (x+1, y) is
z' = ( - A (x+1) - B y - D ) /C.
or
z' = z - A/C.
Generating z-values when travelling across a scanline is thus simple once the z-value is known at the left edge of the polygon. This can also be calculated in an incremental fashion, knowing that
x' = x + 1/m.
The new left edge of the polygon is (x+1/m, y+1) and we can determine the difference in z as being
z' = z - (A/m + B )/C.
The data for each surface includes:
[Hill: 707-711. Foley & van Dam: p. 675-680]
BSPtree *BSPmaketree(polygon list) {
choose a polygon as the tree root
for all other polygons
if polygon is in front, add to front list
if polygon is behind, add to behind list
else split polygon and add one part to each list
BSPtree = BSPcombinetree(BSPmaketree(front list),
root,
BSPmaketree(behind list) )
}
DrawTree(BSPtree) {
if (eye is in front of root) {
DrawTree(BSPtree->behind)
DrawPoly(BSPtree->root)
DrawPoly(BSPtree->front)
} else {
DrawTree(BSPtree->front)
DrawPoly(BSPtree->root)
DrawTree(BSPtree->behind)
}
}
[Hill: 706,711-713. Foley & van Dam: p. 672-675]
Ambiguities can be resolved in several ways.
If these fail, exchange the order of the surfaces and repeat. If this still fails, the polygons can be intersected to split one of the polygons if necessary. In the worst case, the algorithm can generate O(n^2) new polygons.
[Hill: 713-716. Foley & van Dam: p. 680-684]
for each scanline (row) in image for each pixel in scanline determine closest object calculate pixel colour, draw pixel end end
[Hill: Chapter 14. Foley & van Dam: p. 701-712]
for each pixel on screen determine ray from eye through pixel find closest intersection of ray with an object cast off reflected and refracted ray, recursively calculate pixel colour, draw pixel end