Friday, August 5, 2016

glDepthMask


Q
    What is Depth Buffer ?
    What is Color Buffer ?
A
     Theoretically, these buffers are really just big 2D-arrays, each the width plus height of the app screen..
     The Color-Buffer naturally is going to hold the final coloration of each pixel. there is one entry in the color buffer per screen pixel.
     The Depth-Buffer, is like the color buffer in that there is one entry per screen pixel, but it is used for something different. Entries in the Depth-Buffer are a measure of "how close" each colored pixel really is.

,,,
  • Depth Test
  • Stencil Test
  • Alpha Test
  • Blend Test

,,,


Name

glDepthMask
  enable or disable writing into the depth buffer..

C Specification

void glDepthMask(
   GLboolean flag
);

Parameters

  flag \ specifies whether the depth buffer is enabled for writing.if flag is GL_FALSE, depth buffer writing is disabled. otherwise, it is enabled. initially, depth buffer writing is enabled.

Description

  glDepthMask specifies whether the depth buffer is enabled for writing. if flag is GL_FALSE, depth buffer writing is disabled. Otherwise, it is enabled. initially, depth buffer writing is enabled.
,,,

How it looks like :-)

some steps
0)  Turn on the depth mask  glDepthMask(GL_TRUE);;
1)  Draw all opaque objects, in any order;;
2)  Turn off the Depth Mask glDepthMask(GL_FALSE);;
3)  Turn on a BLEND_MODE;
4)  Draw translucent objects sorted from furthest away to nearest;
Why do you do this?
There are 2 buffers you need to worry about: the depth buffer and the color buffer. These buffers are really just big 2d arrays, each the width x height of your screen.
The color buffer naturally is going to hold the final coloration of each pixel. There is one entry in the color buffer per screen pixel. The depth buffer, is like the color buffer in that there is one entry per screen pixel, but it is used for something different. Entries in the depth buffer are a measure of "how close" each colored pixel really is.
If you render 1 triangle, that is far away from the camera, it generates a set of colors and depth values for each pixel it "wants" to cover on the screen. Say you then render another poly that is closer, it also will generate a set of values for the depth and color buffers. Now, there is a sort of "contest" at pixel coloration time where the "further away" fragments (large depth buffer values) are discarded, and only the closest fragments are kept. The closer fragments end up coloring the pixel you had. (When two polygons are nearly overlapping, Z-fighting can occur)
Start by rendering the objects in your scene with the depth mask on. This means every shape you render, when its pixels get colored, the depth buffer gets updated with the "winner" of the contest.
Then, you 3) glDepthMask( GL_FALSE ) turns off the depth buffer for writing, 4) turn on blending, 5) render translucent shapes from furthest to nearest. Seems weird, huh?
When you turn off the depth mask, and render the translucent shapes, OpenGL will still read the depth buffer to determine which fragments to throw away (i.e. if your translucent shape is behind an already rendered solid shape, then you throw that translucent shape's fragments away). But it will not write to the depth buffer, so if a translucent shape is really really close to the eye (say like a translucent windshield), those windshield fragments do not prevent other fragments that are actually further away from being drawn. This is important, because if your windshield is right in front of you and you render it translucent, and you let the windshield fragments update the depth buffer then you will see nothing else in your scene except the windshield, even though there are shapes behind it, because OpenGL will think "Hey, that windshield is the only thing the user should see, due to these depth buffer readings, so I won't bother rendering anything further away than this windshield, then." Turning off the depth mask is a way of "tricking" OpenGL into "not knowing" there are very close, but translucent, fragments.

--By bobobobo

,,,

Another Insightment

Three-Dimensional Blending with the Depth Buffer
...the order in which polygons are drawn greatly affects the blended result. when drawing THREE-DIMENSIONAL translucent objects, you can get different appearance depending on whether you draw the polygons from back to front or from front to back. you also need to consider the effect of the depth buffer when determining the correct order...

...the depth buffer keeps track of the distance between the viewport and the portion of the object occupying a given pixel in a window on the screen; when another candidate color arrives for that pixel, it's drawn only if its object is closer to the viewport, in which case its depth value is stored in the depth buffer. with this method, obscured (or hidden) portions of surfaces aren't necessarily drawn and therefore aren't used for blending..

If you want to render both opaque and translucent objects in the same scene, then you want to use the depth buffer to perform hidden-surface removal for any objects that lie behind the opaque objects. If an opaque object hides either a translucent object or another opaque object, you want the depth buffer to eliminate the more distant object. If the translucent object is closer, however, you want to blend it with the opaque object. You can generally figure out the correct order to draw the polygons if everything in the scene is stationary, but the problem can quickly become too hard if either the viewpoint or the object is moving.

The solution is to enable depth buffering but make the depth buffer read-only while drawing the translucent objects. First you draw all the opaque objects, with the depth buffer in normal operation. Then you preserve these depth values by making the depth buffer read-only. When the translucent objects are drawn, their depth values are still compared to the values established by the opaque objects, so they aren't drawn if they're behind the opaque ones. If they're closer to the viewpoint, however, they don't eliminate the opaque objects, since the depth-buffer values can't change. Instead, they're blended with the opaque objects. To control whether the depth buffer is writable, use glDepthMask(); if you pass GL_FALSE as the argument, the buffer becomes read-only, whereas GL_TRUE restores the normal, writable operation.

Example 6-2 demonstrates how to use this method to draw opaque and translucent three-dimensional objects. In the program, typing `a' triggers an animation sequence in which a translucent cube moves through an opaque sphere. Pressing the `r' key resets the objects in the scene to their initial positions. To get the best results when transparent objects overlap, draw the objects from back to front.


No comments:

Post a Comment