OpenGL study notes twelve (depth testing)

Reason to test the depth

When we render multiple objects, there is a relationship between each block of the plurality of objects obscured parts of the object will not be visible, it is farther away from the camera, in order to tell the computer does not need to be blocked by objects rendering, we it needs to be done to test the depth of the point on the object, to detect whether it needs to render.
To achieve the above detection, the depth buffer need, simply, is an array of points on the depth value stored in the object, the array value is 0 the start, when the depth value of the current object is rendered larger than when stored in the buffer, this value will be written into the buffer, while the depth test, if the contrary, the depth value is less than the depth values in the buffer, depth test is not passed, do not write buffer, eliminated this point (not rendered ).
Of course, you can define your own comparison function, will do next presentation.

Depth test function

There are two main steps depth testing,

  1. Comparison of the values ​​in the buffer, is determined by testing whether the (Z Test)
  2. Whether the value of the write buffer (Z Write)

Depth test function is disabled by default, depth testing methods open function:

glEnable(GL_DEPTH_TEST);

Depth write control logic function:

glDepthMask(GL_FALSE);
glDepthMask(GL_TRUE);

Comparison function

By comparison function, we can customize the refresh mode buffer

glDepthFunc(GL_ALWAYS);
function description
GL_ALWAYS Always by depth test
GL_NEVER Never test the depth
GL_LESS By testing a fragment depth value is less than the depth buffer value
GL_EQUAL When tested by the depth value equal to the depth value of the fragment buffer
GL_LEQUAL By testing a fragment depth equal to the depth value is smaller than the value of the buffer
GL_GREATER When tested by a depth greater than the depth value of the fragment buffer
GL_NOTEQUAL When tested by the depth value is not equal to segment the depth buffer value
GL_GEQUAL By testing a fragment depth greater than the depth buffer value is equal to

Clear the depth buffer

Every time we need to be rendered again after emptying the cache value, or screen error conditions may occur

Clear cache the same way and clear colors

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

The accuracy of the test depth and the depth of conflict

Function calculates depth values:

Here Insert Picture Description
a depth value often have 16-bit, 24-bit and 32-bit, its value bits 0-1.
Emerging issues:

  1. When large numbers of objects very far away from the camera, there will be a depth value of the error phenomenon, because too much focus on this at a value of 1
  2. When a large number of objects in the same plane, there will be the same depth value (because it is a float, it may render the results are different every time, causing jitter)

For more than two types of problems, it should set its rendering order, camera's near and far planes, to achieve the best results

Published 60 original articles · won praise 223 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_36696486/article/details/104378594