Z-buffering
From Wikipedia, the free encyclopedia
In computer graphics, z-buffering is the management of image depth coordinates in three-dimensional (3-D) graphics, usually done in hardware, sometimes in software. It is one solution to the visibility problem, which is the problem of deciding which elements of a rendered scene are visible, and which are hidden. The painter's algorithm is another common solution which, though less efficient, can also handle non-opaque scene elements. Z-buffering is also known as depth buffering.
When an object is rendered by a 3D graphics card, the depth of a generated pixel (z coordinate) is stored in a buffer (the z-buffer or depth buffer). This buffer is usually arranged as a two-dimensional array (x-y) with one element for each screen pixel. If another object of the scene must be rendered in the same pixel, the graphics card compares the two depths and chooses the one closer to the observer. The chosen depth is then saved to the z-buffer, replacing the old one. In the end, the z-buffer will allow the graphics card to correctly reproduce the usual depth perception: a close object hides a farther one. This is called z-culling.
The granularity of a z-buffer has a great influence on the scene quality: a 16-bit z-buffer can result in artifacts (called "z-fighting") when two objects are very close to each other. A 24-bit or 32-bit z-buffer behaves much better, although the problem cannot be entirely eliminated without additional algorithms. An 8-bit z-buffer is almost never used since it has too little precision.
Contents |
[edit] Developments
Even with enough granularity, quality problems may arise when precision in the z-buffer's distance values is not spread evenly over distance. Nearer values are much more precise (and hence can display closer objects better) than values which are farther away. Generally, this is desirable, but sometimes it will cause artifacts to appear as objects become more distant. A variation on z-buffering which results in more evenly distributed precision is called w-buffering (see below).
At the start of a new scene, the z-buffer must be cleared to a defined value, usually 1.0, because this value is the upper limit (on a scale of 0 to 1) of depth, meaning that no object is present at this point through the viewing frustum.
The invention of the z-buffer concept is most often attributed to Edwin Catmull, although Wolfgang Straßer also described this idea in his 1974 Ph.D. thesis1.
On recent PC graphics cards (1999-2005), z-buffer management uses a significant chunk of the available memory bandwidth. Various methods have been employed to reduce the impact of z-buffer, such as lossless compression (computer resources to compress/decompress are cheaper than bandwidth) and ultra fast hardware z-clear that makes obsolete the "one frame positive, one frame negative" trick (skipping inter-frame clear altogether using signed numbers to cleverly check depths).
[edit] Z-culling
In rendering, z-culling is early pixel elimination based on depth, a method that provides an increase in performance when rendering of hidden surfaces is costly. It is a direct consequence of z-buffering, where the depth of each pixel candidate is compared to the depth of existing geometry behind which it might be hidden.
When using a z-buffer, a pixel can be culled (discarded) as soon as its depth is known, which makes it possible to skip the entire process of lighting and texturing a pixel that would not be visible anyway. Also, time-consuming pixel shaders will generally not be executed for the culled pixels. This makes z-culling a good optimization candidate in situations where fillrate, lighting, texturing or pixel shaders are the main bottlenecks.
While z-buffering allows the geometry to be unsorted, sorting polygons by increasing depth (thus using a reverse painter's algorithm) allows each screen pixel to be rendered fewer times. This can increase performance in fillrate-limited scenes with large amounts of overdraw, but suffers from severe problems such as: 1) polygons might occlude one another in a cycle (e.g. : triangle A occludes B occludes C occludes A), and 2) there is no canonical to pick a point on a triangle (e.g.: nomatter whether one sorts triangles by their centroid or closest point or furthest point, one can always find two triangles A and B such that A is "closer" but in reality A should be drawn first). As such, a reverse painter's algorithm cannot be used as an alternative to Z-culling (without strenuous re-engineering), except as an optimization to Z-culling. For example, an optimization might be to keep polygons sorted according to x/y-location and z-depth to provide bounds, in an effort to quickly determine if two polygons might possibly have an occlusion interaction.
[edit] Mathematics
The range of depth values in camera space (see 3D projection) to be rendered is often defined between a near and far value of z. After a perspective transformation, the new value of z, or z', is defined by:
where z is the old value of z in camera space, and is sometimes called w or w'.
The resulting values of z' are normalized between the values of -1 and 1, where the near plane is at -1 and the far plane is at 1. Values outside of this range correspond to points which are not in the viewing frustum, and shoudn't be rendered.
In order to convert the normalised values of z' back into camera space, the inverse of the above formula can be used:
This allows measurement of distances (between objects and the camera) in the camera/object space, after a depth buffer calculation has been performed. This formula assumes that the value of z' has been normalised to the range [-1,1] as described above (this may not be the case, however, when reading a depth buffer from a hardware graphics accelerator). The resolution of values of z calculated from z' will depend on the selected near and far planes, as described below.
To implement a z-buffer, the values of z' are linearly interpolated across screen space between the vertices of the current polygon, and these intermediate values are generally stored in the z-buffer in fixed point format. The values of z' are grouped much more densely near the near plane, and much more sparsely farther away, resulting in better precision closer to the camera. The closer the near plane is set to the camera, the less precision there is far away—having the near plane set too closely is a common cause of undesirable rendering artifacts in more distant objects.
[edit] W-buffer
To implement a w-buffer, the old values of z in camera space, or w, are stored in the buffer, generally in floating point format. However, these values cannot be linearly interpolated across screen space from the vertices—they usually have to be inverted, interpolated, and then inverted again. The resulting values of w, as opposed to z', are spaced evenly between near and far. There are implementations of the w-buffer that avoid the inversions altogether.
Whether a z-buffer or w-buffer results in a better image depends on the application.
[edit] See also
- Edwin Catmull – inventor of the z-buffer concept.
- 3D computer graphics
- z-fighting
- Irregular Z-buffer
- Z-order
- Hierarchical Z-buffer
[edit] External links
[edit] Notes
Note 1: see W.K. Giloi, J.L. Encarnação, W. Straßer. "The Giloi’s School of Computer Graphics". Computer Graphics 35 4:12–16.