Render to texture (FBO) - result is upside down

I’ve managed to render to texture, and also render the resulting texture on a quad. My problem is that the result is upside down and I cannot figure out why.

What I basically do is creating an instance of the following class with the screen size as width and height parameters. Then I activate it when I want to start rendering to texture, and deactivate it when I’m done. Then I fetch the result as a texture id, which I then bind to GL_TEXTURE_2D later on.

This works fine, but as I said from the beginning: it is upside down!

LWJGL code:


import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.*;

import java.nio.ByteBuffer;

public class FBORenderer {
    private int fbo;
    private int texture;
    private int height;
    private int width;
    private ByteBuffer byteBuffer;

    public FBORenderer(int w, int h) {
        fbo = ARBFramebufferObject.glGenFramebuffers();
        texture = GL11.glGenTextures();
        width = w;
        height = h;

        activate();

        byteBuffer = BufferUtils.createByteBuffer(4 * width * height);

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_BYTE, byteBuffer);


        ARBFramebufferObject.glFramebufferTexture2D(ARBFramebufferObject.GL_DRAW_FRAMEBUFFER,
                ARBFramebufferObject.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, texture, 0);

        deactivate();
    }

    public void activate(){
        ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_DRAW_FRAMEBUFFER, fbo);
    }

    public void deactivate() {
        ARBFramebufferObject.glBindFramebuffer(ARBFramebufferObject.GL_DRAW_FRAMEBUFFER, 0);
    }

    public int getTexture() {
        return texture;
    }
}

Maybe your texture coordinates on the quad that renders the FBO texture are upside down? Texture origins and window origins are the bottom left corner in OpenGL.

Hmm. Could be! I forgot to mention that I’m using slick2d as well. I’m unsure if they use different corners for (0,0). Will report back when I find out.

Yes, seems like that was the problem.

I went from this


 private void drawFullscreenQuad() {
    glBegin(GL_QUADS);
    glNormal3d(0, 0, 1);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);
    glTexCoord2f(1, 0);
    glVertex2f(width, 0);
    glTexCoord2f(1, 1);
    glVertex2f(width, height);
    glTexCoord2f(0, 1);
    glVertex2f(0, height);
    glEnd();
}

to this


private void drawFullscreenQuad() {
    glBegin(GL_QUADS);
    glTexCoord2f(0, 1);
    glVertex2f(0, 0);
    glTexCoord2f(1, 1);
    glVertex2f(width, 0);
    glTexCoord2f(1, 0);
    glVertex2f(width, height);
    glTexCoord2f(0, 0);
    glVertex2f(0, height);
    glEnd();
}

and now it works fine. I think I was confused by the fact that in slick2d (0,0) is upper left corner.

Thanks for helping me sorting it out! :slight_smile: