Z-buffering

The depth is stored as a height map of the scene, the values representing a distance to camera, with 0 being the closest.

The encoding scheme may be flipped with the highest number being the value closest to camera.

Z-buffering was first described in 1974 by Wolfgang Straßer in his PhD thesis on fast algorithms for rendering occluded objects.

[1] A similar solution to determining overlapping polygons is the painter's algorithm, which is capable of handling non-opaque scene elements, though at the cost of efficiency and incorrect results.

In a 3D-rendering pipeline, when an object is projected on the screen, the depth (z-value) of a generated fragment in the projected screen image is compared to the value already stored in the buffer (depth test), and replaces it if the new value is closer.

In the end, the z-buffer will allow correct reproduction of the usual depth perception: a close object hides one further away.

The z-buffer has the same internal data structure as an image, namely a 2D-array, with the only difference being that it stores a single value for each screen pixel instead of color images that use 3 values to create color.

Primary visibility tests (such as back-face culling) and secondary visibility tests (such as overlap checks and screen clipping) are usually performed on objects' polygons in order to skip specific polygons that are unnecessary to render.

The granularity of a z-buffer has a great influence on the scene quality: the traditional 16-bit z-buffer can result in artifacts (called "z-fighting" or stitching) when two objects are very close to each other.

A more modern 24-bit or 32-bit z-buffer behaves much better, although the problem cannot be eliminated without additional algorithms.

The primary use now is for video games, which require fast and accurate processing of 3D scenes.

Z-buffering is also used (implemented as software as opposed to hardware) for producing computer-generated special effects for films.

[citation needed] Furthermore, Z-buffer data obtained from rendering a surface from a light's point-of-view permits the creation of shadows by the shadow mapping technique.

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.

[a] On more 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 performance cost of z-buffering, 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).

Super Smash Bros. and F-Zero X are two N64 games that minimized Z buffering to increase framerates.

[3] Mechwarrior 2 on PC supported resolutions up to 800x600[4] on the original 4 MB 3DFX Voodoo due to not using Z Buffering.

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.

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 if not combined with z-buffering it suffers from severe problems such as: 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.

The range of depth values in camera space to be rendered is often defined between a

Values outside of this range correspond to points which are not in the viewing frustum, and shouldn't be rendered.

Typically, these values are stored in the z-buffer of the hardware graphics accelerator in fixed point format.

where d is the depth of the z-buffer (usually 16, 24 or 32 bits) and rounding the result to an integer:[5] This formula can be inverted and derived in order to calculate the z-buffer resolution (the 'granularity' mentioned earlier).

plane, and much more sparsely farther away, resulting in better precision closer to the camera.

plane set too closely is a common cause of undesirable rendering artifacts in more distant objects.

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.

Z-buffer data