OpenGL framebuffer depth buffer not working

1.5k Views Asked by At

I am trying to create a framebuffer object with a colour and depth buffer. The colour buffer works fine but the depth buffer does not.

It is unlikely that the problem is in the scene drawing routine, as it works fine when using the default framebuffer. The scene draws something when depth testing is disabled, but when depth testing is enabled the scene does not draw. I can only conclude that there is a problem with the way that the depth buffer is being created and attached to the framebuffer. The frame buffer is created successfully, I have tried a number of different depth buffer formats and also a render buffer equivalent, but none of these changes seem to make a difference.

I did try reading the pixels of the depth buffer using glReadPixels. There seemed to be some changes to it, suggesting that the scene is writing to the depth buffer successfully.

The following is the code for creating the frame buffer. Is there anything else I can try to fix or help me debug the problem further? Perhaps something that needs to be set for custom framebuffers but not the default?

// Create and attach texture to framebuffer
_texture = (GLTexture)context.CreateTexture(width, height);

// Create depth texture
_depthBufferName = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D, _depthBufferName);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent, width, height, 0, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);

// Create depth render buffer
// _depthBufferName = GL.GenRenderbuffer();
// GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, _depthBufferName);
// GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, RenderbufferStorage.Depth24Stencil8, width, height);

// Create and bind frame buffer
GL.GenFramebuffers(1, out _name);
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _name);
GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, _texture.Id, 0);
GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, _depthBufferName, 0);
// GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, _depthBufferName);

GL.DrawBuffers(1, new[] { DrawBuffersEnum.ColorAttachment0 });

// Check if framebuffer has been setup successfully
if (GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferErrorCode.FramebufferComplete)
    throw new SDL2Exception("Unable to create framebuffer.");
0

There are 0 best solutions below