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;
}
}
