Jump to content
  • Advertisement
Sign in to follow this  
lilljohan

OpenGL FBO depth wont clear

This topic is 2080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This has been solved now: I had to call glDepthMask(GL_TRUE) before clearing.

Hello,
I am having trouble with the depth buffer and my fbo. Everything renders correctly if I disable depth test or if I render directly to the frame buffer instead of the fbo. It looks like the depth buffer that is attached to the fbo is not cleared correctly, I can also see some flickering of the rendered object when I move the camera. My box is rendered correctly in the first frame when I do frame by frame stepping in CodeXL but it is not visible in the following frames.

I get no errors from CheckFrameBufferStatus or GetError and the fbo looks ok when I check it in CodeXL (1 texture and 1 render buffer, fromat = GL_DEPTH_COMPONENT24, type = GL_FLOAT).

Creation of the fbo:
GL.GenFramebuffers(1, out Handles[index].FrameBufferObject);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handles[index].FrameBufferObject);

Handles[index].DrawBuffers = new DrawBuffersEnum[textureHandles.Length];

for (var i = 0; i < textureHandles.Length; i++)
{
    Handles[index].DrawBuffers[i] = DrawBuffersEnum.ColorAttachment0 + i;
    GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + i, OGL.TextureTarget.Texture2D, textureHandles[i], 0);
}

GL.GenRenderbuffers(1, out Handles[index].DepthBufferObject);
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, Handles[index].DepthBufferObject);
GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.DepthComponent24, width, height);

GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, Handles[index].DepthBufferObject);
GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0);

var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
if (status != FramebufferErrorCode.FramebufferComplete)
{
    // Log error and clean up if neccecary
}

GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
Below are all the draw calls for a complete frame, extracted while debugging with CodeXL.
// Render the crate to the texture
glBindFramebuffer(GL_FRAMEBUFFER, 1)
glDrawBuffers(1, {GL_COLOR_ATTACHMENT0})
glViewport(0, 0, 512, 512)
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glUseProgram(33) [Context 3 - program 33: shader 31, shader 32]
glDisable(GL_BLEND)
glEnable(GL_DEPTH_TEST)
glDepthMask(TRUE)
glEnable(GL_CULL_FACE)
glBlendFunc(0, 1)
glCullFace(GL_BACK)
glDepthFunc(GL_LESS)
glUniformMatrix4fv(0, 1, FALSE, {0.47494337, -1.4067751, 0.32057661, 0.32045144} {5.5997848e-008, 0.6356858, 0.93057901, 0.9302156} {0.85067475, 0.78542173, -0.17898236, -0.17891248} {1.7013494, 0.80802053, -1.6746986, -1.4740837})
glBindVertexArray(4)
glDrawElements(GL_TRIANGLES, 612, GL_UNSIGNED_INT, 0x000000)

// Render texture to fullscreen quad
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glDrawBuffer(GL_BACK)
glViewport(0, 0, 1280, 720)
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
glUseProgram(6) [Context 3 - program 6: shader 4, shader 5]
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, 2) [Context 3 - Texture 2: Context 3 - Texture 2]
glDisable(GL_BLEND)
glDisable(GL_DEPTH_TEST)
glDepthMask(FALSE)
glEnable(GL_CULL_FACE)
glBlendFunc(0, 1)
glCullFace(GL_BACK)
glDepthFunc(GL_LESS)
glUniform1i(0, 0)
glBindVertexArray(5)
glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, 0x000000)
wglGetCurrentDC()
wglSwapBuffers(0xE40123A5)
Anyone got any ideas? I have searched on google but have not found anything that helps so far. It feels like I'm missing something obvious in my opengl calls.
 
Update: Actual fbo setup as reported by CodeXL, everything looks ok to me.
// Texture setup
glGenTextures(1, {0})
glBindTexture(GL_TEXTURE_2D, 2) [Context 4 - Texture 2: No preview available]
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 512, 512, 0, GL_RGBA, GL_FLOAT, 0x000000) [Context 4 - Texture 2: No preview available]
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) [Context 4 - Texture 2: Context 4 - Texture 2]
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) [Context 4 - Texture 2: Context 4 - Texture 2]
glFinish()
glGetError()

// Frame buffer setup
glGenFramebuffers(1, 0x29A83C4)
glBindFramebuffer(GL_FRAMEBUFFER, 1)

// Attach texture
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 2, 0)

// Create and attach depth render buffer
glGenRenderbuffers(1, 0x29A83C8)
glBindRenderbuffer(GL_RENDERBUFFER, 1)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 512, 512)

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 1)
glBindRenderbuffer(GL_RENDERBUFFER, 0)

glCheckFramebufferStatus(GL_FRAMEBUFFER)
glBindFramebuffer(GL_FRAMEBUFFER, 0)
Edited by Johan Gustafsson

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!