I am trying to do some post processing on my 3d scene. When I render directly to the screen edges are sharp, however when I render my scene to an offscreen FBO then render that to a texture that is then drawn to the screen I get strange Z-fighting around object edges. Any Idea how this can be resolved?

The resolution of the FBO image seems to be worse in some places too, however the FBO size is set to the window width and height.


Direct Rendering enter image description here

FBO rendering enter image description here

Depth Buffer Code

private int createDepthBufferAttachment(){
    depthTexture = Game.textureManager.genTexture();//call to GL11.glGenTextures()
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, depthTexture);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT16, Game.WIDTH, Game.HEIGHT, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
    GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, depthTexture, 0);

    return depthTexture;
  • \$\begingroup\$ Can you share your code that you use to create the FBO, in particular, how you create the texture that you bind as GL_DEPTH_ATTACHMENT to your framebuffer object? \$\endgroup\$ – Bram Dec 22 '18 at 1:51
  • \$\begingroup\$ Another factor that can impact your depth precision is the positioning of your near & far planes. The greater the ratio of far:near distance, the worse your precision distribution. \$\endgroup\$ – DMGregory Dec 22 '18 at 21:28

Replace your GL_DEPTH_COMPONENT16 with GL_DEPTH_COMPONENT24 when creating the depth buffer texture.

  • \$\begingroup\$ Thank you so much! Was I just not allocating enough space for the depth buffer, so some pixels were essentially clipping? \$\endgroup\$ – bay jose Dec 22 '18 at 21:16
  • \$\begingroup\$ You were running out of numerical precision, so not "clipping" so much as "ending up in the same depth bin" for comparisons while rendering. \$\endgroup\$ – Patrick Hughes Dec 22 '18 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.