What is the depth, depth cache, and depth test in OpenGL?

Source: Internet
Author: User

Original from http://blog.csdn.net/xiaoquanhuang/article/details/6613705

1) Intuitive understanding

Depth is actually the distance of the pixel in the 3d world, and the depth cache stores the depth value of each pixel point (drawn on the screen)! The depth test determines whether to draw a farther pixel point (or a closer pixel point), which is usually relatively close, and the farther priority can achieve the effect of perspective!!!

2) z Value (depth value), Z buffer (depth cache)

Let's talk about Z coordinates. The z coordinate is the same as the x and Y coordinates. After transformation, clipping, and perspective division, the range of Z is -1.0~1.0. The Depthrange map specifies the transformation of the z-coordinate, which is similar to the viewport transformation used to map X and Y to window coordinates, but the depthrange mapping differs from the viewport mapping, because the hardware scheme of the deep cache is hidden from the application. The parameter calling Depthrange is [0.0,1.0], and the z-value (depth value) associated with a break is the distance to the eye. By default, a fragment closest to the eye (on the near section) is mapped to 0.0, and the fragment farthest from the eye (on the far section) is mapped to 1.0. Fragments can be mapped to a subset of the depth cache range (by specifying a smaller value in Depthrange). The mapping can also be reversed, so that the most distant piece of the eye is at 0.0, the most recent piece in 1.0 (called Depthrange (1.0,0.0)), although such a reverse mapping is possible, but does little to the practical application.
To understand why the rendering quality is inconsistent, it is important to understand the characteristics of the z-coordinate of the screen. The Z-value specifies the distance from the fragment to the eye. The relationship between distance and Z-values in orthographic projections is linear, but not in perspective projections. In perspective projections, this relationship is non-linear, and the degree of nonlinearity is proportional to the far/near (or zfar/znear in the Gluperspective function) in the Frustum function. This nonlinearity increases the accuracy of Z-values near the near-cross-section, and increases the efficiency of the depth cache, but reduces the accuracy of the depth cache in other parts of the viewport. According to experience, the far/near ratio of more than 1000 will have this bad effect. So the general far/near ratio should be less than 1000. The simplest way to solve this problem is to reduce the far/near ratio by nearly cross-section away from the eye, whose only side effect is that objects close to the eye may be cut off, but this is rarely a problem in a particular application, where the near-section position has no effect on the projection of the x and Y coordinates. So this has a small effect on the image.
There are some other aspects of OpenGL rasterization and deep caching that are worth mentioning. One big problem is that the rasterization process uses imprecise algorithms, so it is difficult to work with coplanar entities unless they share the same plane equation. This problem is more serious in the finite-precision deep cache implementation. These issues include decals (decaling), hidden line elimination, contour polygons, and shadows. However, there are many ways to solve these problems, such as Polygonoffset technology.
The number of bits in the depth cache is the parameter that measures the depth cache precision. The higher the depth cache bit, the higher the accuracy, the current video card generally can support the 16-bit z buffer, some advanced graphics cards can already support the 32-bit z buffer, but generally with 24-bit z buffer is enough.

3) Depth Test

The depth test in OpenGL is to use the deep buffer algorithm to eliminate the non-meeting in the scene. By default, the depth value in the depth cache ranges from 0.0 to 1.0, and this range value can be passed through the function:
Gldepthrange (nearnormdepth, farnormaldepth);
The range of depth values is changed from nearnormdepth to Farnormaldepth. Here nearnormdepth and farnormaldepth can take any value from 0.0 to 1.0, even for nearnormdepth > farnormaldepth. In this way, the Gldepthrange function allows for deep testing in any area of the perspective projection finite observation space.
Another very useful function is:
Glcleardepth (maxDepth);
The parameter maxdepth can be any value in the range of 0.0 to 1.0. Glcleardepth initializes the depth cache with MaxDepth, and by default, the depth cache is initialized with 1.0. Because the polygons that are larger than the depth cache initial values are not drawn in the depth test, the glcleardepth function can be used to speed up the deep test processing. It is important to note that after you specify the initialization value for the depth cache, you should call:
Glclear (Gl_depth_buffer_bit);
Completes the initialization of the deep cache.
In the depth test, the default is to compare the Z-value of the new pixel that needs to be drawn to the z-value of the corresponding position in the depth buffer, and if it is smaller than the value in the depth cache, update the color value of the corresponding pixel in the frame cache with the color value of the new pixel. This comparison test can be done by using a function:
Gldepthfunc (func);
Be modified. The value of the parameter Func can be gl_never (no processing), gl_always (processing All), gl_less (less than), gl_lequal (less than equals), gl_equal (equals), gl_gequal (greater than or equal), Gl_ GREATER (greater than) or gl_notequal (not equal to), where the default value is gl_less. These tests can reduce the computation of deep cache processing in a variety of applications.

What is the depth, depth cache, and depth test in OpenGL?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.