LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: Depth Buffer  (Read 333 times)

pdid

  • Newbie
  • *
  • Offline Offline
  • Posts: 21
Depth Buffer
« on: January 23, 2017, 23:01:04 »

What is the difference between glEnable(GL_DEPTH_TEST) and glDepthMask(true)?
Logged

spasi

  • Administrator
  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 1805
Re: Depth Buffer
« Reply #1 on: January 24, 2017, 01:31:54 »

When GL_DEPTH_TEST is enabled, the depth of incoming fragments will be tested against the current depth in the framebuffer, using the function specified with glDepthFunc. Fragments will be discard or accepted based on that test.

When glDepthMask is set to true, any fragments that make it to the end of the rendering pipeline will update the framebuffer with their depth.

This is useful when doing a Z pre-pass. In the depth pass, GL_DEPTH_TEST is enabled, glDepthFunc is set to GL_LEQUAL (or GL_GEQUAL, depending on the setup) and glDepthMask is set to true. In the shading pass(es), GL_DEPTH_TEST is again enabled, glDepthFunc is set to GL_EQUAL and glDepthMask is set to false. Doing no depth writes in the shading pass(es) saves framebuffer bandwidth.
Logged

Kai

  • Nerdus Imperius
  • *****
  • Offline Offline
  • Posts: 900
Re: Depth Buffer
« Reply #2 on: January 24, 2017, 07:40:53 »

Just read the documentation of glEnable (search for GL_DEPTH_TEST) and of glDepthMask.
You can have the following constellations:
1. GL_DEPTH_TEST enabled and depth mask = true:
   - fragments are depth-tested and the depth is written to the depth buffer
2. GL_DEPTH_TEST enabled and depth mask = false:
   - fragments are depth-tested but the depth is NOT written to the depth buffer
   - this is the mode you would render transparent objects in after you've rendered opaque ones
3. GL_DEPTH_TEST disabled and depth mask = true:
   - Fragments are NOT being depth-tested and NEITHER written to the depth buffer
4. GL_DEPTH_TEST disabled and depth mask = false:
   - Same as 3.
Logged