Computer Graphics - Depth Buffer Test

Depth Buffer (Z-buffer) Test

Algorithm

The following algorithm is taken from here.

We start with setting the depth of each pixel to infinity.
d(i, j) = infinity
Initialize the color value for each pixel 
c(i, j) = background color
for each polygon, do the following steps :

for (each pixel in polygon's projection)
{
    z (depth) of polygon at (x, y) corresponding to pixel (i, j)
    
    if (z < d(i, j))
    {
        d(i, j) = z;
        c(i, j) = color;
    }
}

Example Figure 

An example taken from METU CENG Slides

Z-Fighting

Z-fighting occurs when two planes have the same depth (have the same value in the z-axis) and z-buffer cannot decide which plane is near which one is far. Then the following situation occurs:


Image result for z-fighting"
Z-fighting in the intersection 
This situation may occur also when the depth range [nearest, furthest] is scaled to [0,1]. This is range [0,1] scaling from [near=10, far=50] after projection and viewport transformations.

Taken from METU Ceng Slides
This is range [0,1] scaling from [near=10, far=200] after projection and viewport transformations.
Taken from METU Ceng Slides
As you can see the difference when the range between nearest and furthest increases, the stability of the [0-1] range decreases. The scaled z-value of the z= 180 and z= 200 are almost the same, and this similarity may cause z-fighting. In order to prevent this, you must choose the range [nearest, furthest] as small as possible.

More Resources:
https://www.geeksforgeeks.org/z-buffer-depth-buffer-method/
https://en.wikipedia.org/wiki/Z-buffering

Comments