Rendering the depth buffer to screen

Started by
4 comments, last by AbandonedAccount 10 years, 3 months ago

I'm using OpenGl 4.3 deferred renderer and I've setup my depth buffer for my FBO as following:

GLCALL(glGenRenderbuffers(1, &mDepthbuffer));
GLCALL(glBindRenderbuffer(GL_RENDERBUFFER, mDepthbuffer));
GLCALL(glRenderbufferStorageMultisample(GL_RENDERBUFFER, 0, GL_DEPTH32F_STENCIL8, windowWidth, windowHeight));
GLCALL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mDepthbuffer));
GLCALL(glBindRenderbuffer(GL_RENDERBUFFER, 0));

When I debug normals, positions, etc I can just set that color attachments using glReadBuffer and then glBlitFramebuffer to the default FBO. But the depth buffer is a DEPTH_STENCIL Renderbuffer - what is the best way of getting its contents to the screen?

Advertisement

http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffer-in-glsl/ seems like what you want.

If you need generic access, you can read depth buffer with glReadPixels. You can also do PBO juggling if you need better performance than glReadPixels.

And just to make sure you're not mixing the two, GL_DEPTH_COMPONENT is depth buffer while GL_DEPTH_STENCIL is both the depth and stencil buffer.

I use both, but I only want the depth component from the combined buffer, is that not possible to get?

It's fine to use both. You'll most likely deal with GL_DEPTH24_STENCIL8 format. You can access the depth component with .x (xyzw) or .r (rgba). Nothing real complicated, so I'm sure you'll be fine. Just remember it's 0..1 and not -1..1.

Just curious, the stencil component is then accessed using .y?

This answer is going to assume you're going to be using textures, which appears to be the preferred method going forward (OpenGL now requires support in 4.4).

You can't access both depth and stencil information from the same sampler since stencil is 8-bit unsigned int, and depth is 24-bit float. A texture can't have 2 formats like that. For stencil information, you'll be using a usampler* (unsigned) and GL_STENCIL_INDEX. For depth, you'll use a sampler* and GL_DEPTH_COMPONENT. The stencil data is in the last 8-bit component, so the A of RGBA.

You can use ARB_stencil_texturing if you don't have OpenGL 4.4. Unless absolutely necessary, I wouldn't bother learning older methods. Be sure to make 2 copies of the texture (or 2 views) since you can't have both formats on the same texture.

This topic is closed to new replies.

Advertisement