Question

Here is my code for creating a new FBO. Right now, I am using a render buffer.

/**
 * Creates a new FBO.
 * @param width The width of the FBO to create.
 * @param height The height of the FBO to create.
 * @return an int[] array containing the buffer IDs in the 
 * following order: {frameBufferID, colorBufferID (texture), depthBufferID}.
 */
public static int[] newFBO(int width, int height) {
    int[] out = new int[3];
    out[0] = glGenFramebuffersEXT();
    out[1] = glGenTextures();
    out[2] = glGenRenderbuffersEXT();

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, out[0]);

    glBindTexture(GL_TEXTURE_2D, out[1]);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, org.lwjgl.opengl.GL12.GL_TEXTURE_MAX_LEVEL,20);
    glTexParameteri(GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP,GL_TRUE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,GL_RGBA, GL_INT, (java.nio.ByteBuffer) null);

    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,GL_COLOR_ATTACHMENT0_EXT,GL_TEXTURE_2D, out[1], 0);

    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, out[2]);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL14.GL_DEPTH_COMPONENT24, width, height);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT,GL_RENDERBUFFER_EXT, out[2]);

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

    return out;
}

I want to switch the render/depthbuffer to a depth texture. What would I change to do this? (I am using LWJGL)

Was it helpful?

Solution

Firstly, generate a texture instead of a render buffer:

out[2] = glGenRenderbuffersEXT();

becomes:

out[2] = glGenTextures();

Next, create storage for it (instead of the render buffer):

glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, out[2]);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL14.GL_DEPTH_COMPONENT24,
                         width, height);

becomes:

glBindTexture(GL_TEXTURE_2D, out[2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT24, width, height,
             0, GL_DEPTH_COMPONENT, GL_INT, (java.nio.ByteBuffer) null);

Lastly, bind this texture to your frame buffer instead of the renderbuffer:

glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
                             GL_RENDERBUFFER_EXT, out[2]);

becomes:

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
                          GL_TEXTURE_2D, out[2], 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top