0

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.");
genpfault
  • 51,148
  • 11
  • 85
  • 139
IntelOrca
  • 108
  • 2
  • 8
  • I have to say, this sounds a lot like you are not clearing the depth buffer. It could very well be that your drawing code is to blame, for instance if you call `glClear (...)` at some point while your FBO is not bound - that will clear the default framebuffer only. – Andon M. Coleman Jun 08 '14 at 19:51
  • @AndonM.Coleman I wish it was, but I have doubled checked and I bind the frame buffer right before clearing the depth buffer. And glReadBytes proved that it was clearing it. – IntelOrca Jun 08 '14 at 21:14
  • Really? How exactly are you using `glReadPixels (...)` when your depth buffer is only the source of ***drawing*** and not ***reading***? Your FBO needs to be bound to ***both*** Draw and Read, the target `GL_FRAMEBUFFER` does this (in OpenTK terminology that would be `FramebufferTarget.Framebuffer`, `FramebufferTarget.DrawFramebuffer` is equivalent to `GL_DRAW_FRAMEBUFFER`). – Andon M. Coleman Jun 08 '14 at 21:18
  • Oh right, when I did this, the code was FramebufferTarget.Framebuffer. I only recently changed it to FramebufferTarget.DrawFramebuffer to see if it made a difference. I will change it back. – IntelOrca Jun 08 '14 at 21:31
  • Hey man, I know this question is a bit old, but did you solve this issue? I have EXACTLY the same problem and I fear that its an opentk bug :/ – Greg K. Dec 11 '16 at 23:53

0 Answers0