Game Development Reference
In-Depth Information
than the current depth value stored in the z-buffer, that pixel is drawn, and the new
depth value is written into the z-buffer. A sample representation of the z-buffer is
shown in Figure 4.17 .
Figure 4.17 A sample scene and its corresponding z-buffer.
So the first object that's drawn every frame will always have all of its pixels' color
and depth information written into the color and z-buffers, respectively. But when
the second object is drawn, if it has any pixels that are further away from the
existing pixels, those pixels will not be drawn. Pseudocode for this algorithm is
provided in Listing 4.2 .
Listing 4.2 Z-Buffering
Click here to view code image
// zBuffer[x][y] grabs depth at that pixel
foreach Object o in scene
foreach Pixel p in o
float depth = calculate depth at p
if zBuffer[p.x][p.y] > depth
draw p
zBuffer[p.x][p.y] = depth
end
end
end
Search WWH ::




Custom Search