Sunday, 13 April 2014

What is z-buffering?

Z-buffering, also known as depth buffering, is a computer graphics technique used to determine what objects should ultimately appear on the screen and be visible to the user in the final render.

In a typical 3D scene, some objects are closer to the point of view, some are further, and some are obstructed. When rendering this to a 2D computer screen, it is necessary to decide which objects should be visible and which objects are hidden (or partially hidden).

A simple and intuitive solution to this is to render the objects in order, starting from furthest to nearest. This is known as the painter's algorithm. Objects that are rendered last will "cover" the previously rendered objects, thereby achieving the effect of having nearer objects obstruct the further objects. Unfortunately, this approach requires sorting the objects by their distance from the point of view, which may not be possible in some cases (e.g. cyclically overlapping polygons). The simple method may also be inefficient because for every object, there can easily be pixels which are rendered but are not visible to the user anyway, thus wasting computational resources.

The z-buffering is an alternative that attempts to address this issue. The concept is simple; every pixel on the screen has an additional variable which holds the "depth" (i.e. how far from the POV) of the object occupying the pixel. When an object is drawn, there are two possibilities; either the pixel it is attempting to draw on is empty, or it has been occupied by some other object rendered earlier. If it is empty, then obviously this object will occupy it. Otherwise, a comparison is made between the z value of the pixel and the z value of this new object. If the new object is determined to be further behind the existing object, then rendering is skipped; otherwise, the new object "wins" the pixel and overwrites it.

Pretty simple, isn't it? Now, read about the mathematics.

No comments:

Post a Comment