OpenGL Depth Buffer Z Buffer Introduction __opengl

Source: Internet
Author: User
First Article

" Color buffer "

The color buffer (color_buffer) is the frame buffer (frame_buffer), and the scene you need to render will eventually be written to the buffer for each pixel, which is then displayed on the render to the screen.

" Depth Buffer "

The depth buffer (depth_buffer) corresponds to the frame buffer, which is used to record the depth value of each pixel above, through the depth buffer, we can carry on the depth test, thus determines the pixel occlusion relation, guarantees the rendering correct.

" Template buffers "

The template buffer (Stencil_buffer) is the same as the depth buffer, and by setting the template to buffer the values of each pixel, we can specify that only certain pixels are rendered when rendering, which can achieve some special effects.


The effect of the depth buffer is to distinguish the color of the layer, to prevent the color of the blocked display.

when a pixel is drawn for the second time-for example, when an object is drawn after another object-the depth buffer either retains the previous depth value or replaces the current depth value with the depth value of the second pixel. Which depth is left in depth is dependent on the depth function you choose. For example, if the current depth function is comparefunction.lessequal, only values less than or equal to the current depth value will be retained, and values greater than the current depth value will be discarded. This is called the depth test, which is tested in depth every time the pixel is drawn. When a depth test is performed on a pixel, its color is written to the render target, and the depth is written to the depth buffer

Depth buffer principle and why use it

----------------Resources "OpenGL Programming Guide"

1. Learn about hidden surface elimination before you begin to introduce depth caching.
Hidden surface elimination (Hidden-surface removal) is the removal of a solid object from being obscured by other objects. The easiest way to implement hidden surface elimination is to use a depth buffer (also called a Z-buffer).
2. Depth buffer principle
The depth buffer principle is to correlate the depth value (or distance) of a distance-viewing plane (near-cropped surface) with each pixel in the window.
First, use Glclear (gl_depth_buffer_bit) to set the depth value of all pixels to the maximum (typically a far trimmed surface).
Then, all objects are drawn in any order in the scene. The graphics calculation performed by hardware or software converts each drawing surface into a collection of pixels on the window, regardless of whether it is obscured by other objects.
Second, OpenGL calculates the distance between these surfaces and the viewing plane. If a depth buffer is enabled, OpenGL compares its depth value to the depth value already stored in the pixel before each pixel is drawn. New pixel depth Value < original pixel depth value, the new pixel value replaces the original; instead, the new pixel value is obscured and his color value and depth are discarded.
In order to start a depth buffer, you must first start it, that is, glenable (gl_depth_test). Before each scene is drawn, you need to clear the depth buffer, that is, Glclear (gl_depth_buffer_bit), and then draw the objects in the scene in any order.




Second article


OpenGL often appears in depth testing, has not been clear. Let's find out today.

(1) What is the depth.

The depth is actually the distance (drawing coordinates) of the pixel point in the 3d world from the camera, and the depth cache stores the depth values of each pixel point (drawn on the screen).

The greater the depth value (z-value), the farther away from the camera.

The depth value is stored in the depth cache, and we measure the precision of the depth cache with the number of bits in depth cache. The higher the depth cache bit, the higher the accuracy, the current graphics card can generally support 16-bit z-buffer, some advanced graphics cards can support 32-bit z-buffer, but generally with 24-bit z-buffer is enough.

(2) Why the need for depth.

When we do not use the depth test, if we first draw a close object, and then draw a distant object, the distance of the object because of drawing, will be close to the object cover, this effect is not what we want. With a deep buffer, the order of the objects is less important and can be shown in terms of near and far (Z values), which is critical.

In fact, as long as there is a depth buffer, whether or not the depth test is enabled, OpenGL attempts to write the depth data into the buffer when the pixel is drawn, unless Gldepthmask (Gl_false) is invoked to prevent writing. These depth data can also have interesting uses, such as shading and so on, in addition to regular testing.

(2) Enable depth testing

Use Glenable (gl_depth_test);

The default is to compare the z value of the new pixel to be drawn with the z value of the corresponding position in the depth buffer, and to update the color value of the corresponding pixel in the frame cache with the color value of the new pixel if it is smaller than the value in the depth cache.

However, you can use Gldepthfunc (func) to modify this default test mode.

The value of the parameter Func can be gl_never (not processed), gl_always (handle All), gl_less (less than), gl_lequal (less than equal), gl_equal (equals), gl_gequal (greater than or equal), Gl_ GREATER (greater than) or gl_notequal (not equal), where the default value is gl_less.

Generally, the Gldepthfunc (gl_lequal) is used to express the occlusion relationship between the general objects.

(3) When depth testing is enabled, this does not apply to drawing opaque objects at the same time.


Note:

When drawing translucent objects, note that you need to use Gldepthmask (Gl_false) to set the depth buffer to read-only before you draw a Semitransparent object, or you may see a picture error. Why, because when you draw a transparent object, you use the process color, you cannot continue using the depth mode, but instead use the process color function to mix. This allows you to draw translucent objects using a mixed function.

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.