Monday, May 2, 2016

Depth Testing


[State]
    To Be Continued....
...
[Conception]
   Depth Testing
...
[URL]
    http://www.learnopengl.com/#!Advanced-OpenGL/Depth-testing
...
[Contents]
...
The depth-buffer is a buffer that, just like the color buffer (that stores all the fragment colors: the visual output), stores information per fragment and (usually) has the same width and height as the color buffer. The depth buffer is automatically created by the windowing system and stores its depth values as 16, 24 or 32 bit floats. In most systems you'll see a depth buffer with a precision of 24 bits.
When depth testing is enabled OpenGL tests the depth value of a fragment against the content of the depth buffer. OpenGL performs a depth test and if this test passes, the depth buffer is updated with the new depth value. If the depth test fails, the fragment is discarded.
Depth testing is done in screen space after the fragment shader has run (and after stencil testing has run which we'll discuss in the next tutorial). The screen space coordinates relate directly to the viewport defined by OpenGL's glViewport function and can be accessed via GLSL's built-in gl_FragCoord variable in the fragment shader. The x and y components ofgl_FragCoord represent the fragment's screen-space coordinates (with (0,0) being the bottom-left corner). Thegl_FragCoord also contains a z-component which contains the actual depth value of the fragment. This z value is the value that is compared to the depth buffer's content.
...
[]
...
Depth testing is disabled by default so to enable depth testing we need to enable it with the GL_DEPTH_TEST option:
glEnable(GL_DEPTH_TEST);  
Once enabled OpenGL automatically stores fragments their z-values in the depth buffer if they passed the depth test and discards fragments if they failed the depth test accordingly. If you have depth testing enabled you should also clear the depth buffer before each render iteration using the GL_DEPTH_BUFFER_BIT, otherwise you're stuck with the written depth values from last render iteration:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
There are certain scenarios imaginable where you want to perform the depth test on all fragments and discard them accordingly, but not update the depth buffer. Basically, you're using a read-only depth buffer. OpenGL allows us to disable writing to the depth buffer by setting its depth mask to GL_FALSE:
glDepthMask(GL_FALSE); 
Note that this only has effect if depth testing is enabled.

...
[]

...
[]

...

No comments:

Post a Comment