Z-buffering is a three-dimensional computer graphics method of accounting for the remoteness of an image element. It is one of the solutions to the "visibility problem". It is very effective and practically has no shortcomings, if implemented in hardware . Programmatically, there are other methods that can compete with it: Z-sorting (“artist's algorithm”) and binary space partitioning (BSP), but they also have their advantages and disadvantages. The main disadvantage of Z-buffering is the consumption of a large amount of memory : the so-called depth buffer or Z-buffer is used in the work.
Z-buffer is a two-dimensional array , each element of which corresponds to a pixel on the screen. When the video card draws a pixel, its remoteness is calculated and written to the Z-buffer cell. If the pixels of two objects to be drawn overlap, then their depth values are compared, and the one that is closer is drawn, and its distance value is stored in the buffer. The resulting graphic image is called a z-depth map , which is a grayscale graphic image , each pixel of which can take up to 256 gray values. They determine the distance from the viewer of an object of a three-dimensional scene. The map is widely used in post-processing to give volume and realism and creates effects such as depth of field , atmospheric haze , etc. The map is also used in 3D-packages for texturing , making the surface embossed.
- Card Examples
Below is the result of using two cards together. Here the second map is shot from a scene in which the first acted as a texture extruding the surface.
| Map 1 | Map 2 | Result |
Bit depth buffer has a strong impact on the quality of visualization: the use of a 16- bit buffer can lead to geometric distortions, for example, the effect of "fight" if two objects are close to each other. 24, 32-bit buffers do their job well. 8-bit is almost never used due to low accuracy.
Edwin Kathmull is usually considered the inventor of the z-buffer, although Wolfgang Strasser described this idea in his dissertation (1974).
Options
In the Z-buffer in its classic form, the bitmap of the buffer is not accurate enough at short distances. To solve this problem, a w-buffer is used , in which not the remoteness is used, but the reciprocal of it ( ) What is better to use - z-buffer or w-buffer - depends on the program.
On modern video adapters, working with the z-buffer consumes a considerable part of the RAM bandwidth of the video adapter. To combat this, lossless compression is used: compression / recovery consumes less resources than accessing memory.
At the beginning of the frame, the buffer is filled with a certain number (for example, 1.0). It also consumes a fraction of the machine time, and therefore often do this: the first frame is buffered so that the depth of nearby objects is 0.0, and distant objects - 0.5. The second frame is from 1.0 to 0.5. This reduces accuracy by 1 bit, but eliminates the need to clear the buffer.
Z-buffer and sorting
Although the Z-buffer is designed specifically to do without sorting visible faces, the speed of the Z-buffer depends heavily on the sorting of objects.
Z-conflict
If two objects have a close Z-coordinate, sometimes, depending on the viewpoint, one or the other is shown, then both with a striped pattern. This is called the Z-conflict . Most often, conflicts are inherent in special effects (decals) superimposed on the main texture, for example, bullet holes.
Z-conflicts are solved by shifting one object relative to another by an amount exceeding the error of the Z-buffer.