0
\$\begingroup\$

I have written a Java class for a FrameBuffer and the DepthTexture is simply black or white depending on the order of the creation of the DepthTexture and DepthRenderbuffer. If the createDepthBufferAttachment is called first and then the createDepthTextureAttachment my DepthTexture is a solid white (and by that I really mean 1.0 for every coordinate, I checked that). If its the opposite way around it's a solid black. I am sure I am missing something obvious and would be very happy if someone could save me another week fiddling around with this problem.

By the way to get the texture id i use getDepthTexture() or getTexture() depending on what I want. getTexture() works like it should, the texture gathered through getDepthTexture() doesn't.

I also checked the FrameBuffer by doing glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE but it was complete so there is no issue.

If you have any questions concerning the Code feel free to ask

package render;

import java.nio.ByteBuffer;

import org.lwjgl.opengl.*;

public class FrameBuffer
{
    private int frameBuffer;
    private int texture;
    private int depthTexture;
    private int depthBuffer;
    private int width;
    private int height;

    public FrameBuffer(int width, int height)
    {
        this.width = width;
        this.height = height;
        initialiseResultFrameBuffer();
        Loader.registerFrameBuffer(this);
    }

    public void initialiseResultFrameBuffer()
    {
        frameBuffer = createFrameBuffer();
        texture = createTextureAttachment();
        depthBuffer = createDepthBufferAttachment();
        depthTexture = createDepthTextureAttachment();
        unbindCurrentFrameBuffer();
    }

    public int createFrameBuffer()
    {
        int frameBuffer = GL30.glGenFramebuffers();
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
        GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
        return frameBuffer;
    }

    public void bindFrameBuffer()
    {
        //GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); // I don't ever need this right?
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
        GL11.glViewport(0, 0, width, height);
    }

    public static void unbindCurrentFrameBuffer()
    {
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
        GL11.glViewport(0, 0, DisplayManager.getWidth(), DisplayManager.getHeight());
    }

    private int createTextureAttachment()
    {
        int texture = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, texture, 0);
        return texture;
    }

    private int createDepthTextureAttachment()
    {
        int texture = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT32, width, height, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
        GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, texture, 0);
        return texture;
    }

    private int createDepthBufferAttachment()
    {
        int depthBuffer = GL30.glGenRenderbuffers();
        GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
        GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, width, height);
        GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, depthBuffer);
        return depthBuffer;
    }

    public int getFrameBuffer()
    {
        return frameBuffer;
    }
    public int getTexture()
    {
        return texture;
    }
    public int getDepthTexture()
    {
        return depthTexture;
    }

    public void cleanUp()
    {
        GL30.glDeleteFramebuffers(frameBuffer);
        GL11.glDeleteTextures(texture);
        GL11.glDeleteTextures(depthTexture);
        GL30.glDeleteRenderbuffers(depthBuffer);
    }
}
\$\endgroup\$
  • \$\begingroup\$ this Problem is still unsolved and I need a solution for it \$\endgroup\$ – HenrikD Feb 15 '18 at 22:02
0
\$\begingroup\$

Finally! I found the issue. I will post it here so everyone who has the same issue can find this post. Hope this will be useful.

As I said I really was missing something really obvious. The Depthtexture must be configured to be rendered to. In my code there was a call of GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0) which activates the drawing to the COLOR_ATTACHMENT0. If I try to call GL11.glDrawBuffer(GL30.GL_DEPTH_ATTACHMENT) after that it won't render anymore to the colorattachment.

The Solution was this piece of Code here:

IntBuffer buffers = BufferUtils.createIntBuffer(2);
buffers.put(GL30.GL_COLOR_ATTACHMENT0);
buffers.put(GL30.GL_DEPTH_ATTACHMENT);
buffers.flip();
GL20.glDrawBuffers(buffers);
\$\endgroup\$

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.