Z-Buffer or Depth-Buffer method in C++

The z-buffer also known as depth-buffer is a method that is used for hidden surface detection.

Hidden surface detection

For any picture that has objects and surfaces that are transparent. In this case, objects that are behind other objects are hidden. For proper visual of the image, we need to remove these hidden surfaces is required. The identification is called hidden surface problem.

In z-buffer, we will compare surfaces in the z-axis as depths.


Step 1: initialize the depth of all pixel max.
   d(i,j) = infinity
Step 2: Initialize color for all pixels.
   c(i,j) = background-color
Step 3: for each pixel in polygon projection do,
   Step 3.1: find z i.e. depth of projection’s (i,j) pixel.
   Step 3.2: if z < d(i,j) do
      Step 3.2.1 : d(i,j) = z and c(i,j) = color of polygon.

Using this method pixel by pixel, we will find the depth of each pixel of the plane. The smallest surface determines the color of the buffer. Generally, all z values are close to the range[0,1].

z=0, back clipping plane and z= 1, front clipping plane.

Some important point of a z-buffer

  • No initial sorting of polygons(planes) is required to apply the z-buffer method.

  • Z-buffer provides fast results even if the number of planes is large.

  • Object comparison is not required in the z-buffer method.

  • Non-polygonal hidden objects can also be detected using this method.

  • No extra data-structure for storing and resolving is required.

  • Hardware implementation is possible to speed up the process and is actively used in solve graphic workstations.

  • The method is applicable only when the objects under consideration are opaque i.e. transparent objects might give an error.

  • Drawing of the hidden surface might be a time-consuming process.