简体   繁体   English

延迟渲染:在默认帧缓冲区中使用Gbuffer中的深度缓冲区

[英]Deferred Rendering: Use depth buffer from Gbuffer in default framebuffer

I am making a deferred rendering application with C++ and OpenGL. 我正在使用C ++和OpenGL开发延迟渲染应用程序。 After doing the geometry and light passes I want to render some particles. 完成几何和光通过之后,我想渲染一些粒子。 But I need some depth testing on those. 但是我需要对此进行一些深度测试。 So what I want to do is to use the depth buffer binded to my GBuffer to do some depth testing when drawing to the default OpenGL framebuffer. 因此,我想做的就是在绘制到默认的OpenGL帧缓冲区时,使用绑定到我的GBuffer的深度缓冲区进行一些深度测试。

I make and bind the depth buffer/texture to the framebuffer/GBuffer like this 我像这样制作深度缓冲区/纹理并将其绑定到帧缓冲区/ GBuffer

glGenTextures(1, &_depthTextureID);
glBindTexture(GL_TEXTURE_2D, _depthTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, _width, _height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, _depthTextureID, 0);

How do I bind the Gbuffer depth buffer to the default framebuffer for depth testing on the geometry pass? 如何将Gbuffer深度缓冲区绑定到默认的帧缓冲区,以便在几何传递上进行深度测试?

You can't bind created render buffers or textures to the default framebuffer. 您不能将创建的渲染缓冲区或纹理绑定到默认的帧缓冲区。 The default framebuffer is fixed. 默认的帧缓冲区是固定的。

But you can copy the depth texture to the default framebuffer when rendering the GBuffer to it by setting gl_FragDepth in your fragment shader. 但是,可以通过在片段着色器中设置gl_FragDepth来将深度纹理渲染到默认的帧缓冲区时,将深度纹理复制到默认的帧缓冲区。

You can't directly use the depth texture as depth buffer of your default framebuffer. 您不能直接将深度纹理用作默认帧缓冲区的深度缓冲区。 There's two main options I can think of: 我可以想到两个主要选项:

  1. You can use glBlitFramebuffer() to copy the depth buffer from the FBO to the default framebuffer. 您可以使用glBlitFramebuffer()将深度缓冲区从FBO复制到默认帧缓冲区。 The main calls will look something like this: 主要电话如下所示:

     glBindFramebuffer(GL_READ_FRAMEBUFFER, fboId); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBlitFramebuffer(0, 0, _width, _height, 0, 0, _width, _height, GL_DEPTH_BUFFER_BIT, GL_NEAREST); 
  2. If you only need depth testing, but no update of the depth buffer, during the pass that renders to the default framebuffer, you can simply bind the depth texture, and sample it in your fragment shader. 如果只需要深度测试,而无需更新深度缓冲区,则在渲染为默认帧缓冲区的过程中,您可以简单地绑定深度纹理,并在片段着色器中对其进行采样。 Then you compare the sampled depth value with the incoming depth value of your fragments, and discard the pixels that do not pass the depth test. 然后,将采样的深度值与片段的传入深度值进行比较,并丢弃未通过深度测试的像素。 Or you could sample it with a sampler2DShadow type sampler, and get the depth comparison result slightly more directly. 或者,您可以使用sampler2DShadow类型的采样器对其进行采样,然后更直接地获得深度比较结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM