I'm trying to use FBO's for performance improvements and for the gained knowledge by trying out something new, but I have run in following problem: When I render some texture to the back buffer it works perfect (just a bit slow because of multiple layers of textures on top of each other) But when I try to first draw the textures onto another texture using an FBO the new texture always keeps white.
Now to the Code This is my FBOsetup function (called at the beginning (after creating the Opengl context)):
int FBOId;
int FBOTexId;
void setupFBO(){
IntBuffer buffer = ByteBuffer.allocateDirect(1 * 4).order(ByteOrder.nativeOrder()).asIntBuffer();
glGenFramebuffersEXT(buffer);
FBOId= buffer.get();
FBOTexId=glGenTextures();
glBindTexture(GL_TEXTURE_2D, FBOTexId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, tileSize, tileSize, 0, GL_RGB, GL_UNSIGNED_BYTE, ByteBuffer.allocateDirect(4*tileSize*tileSize));
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBOId);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, FBOTexId, 0);
}
Now the render function:
void redraw(){
glPushMatrix();
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FBOId);
glLoadIdentity();
glOrtho(0, tileSize, tileSize, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
Main.drawTexture(0, 0, tileSize, tileSize, textureRotation, textureId);
glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
glPopAttrib();
glPopMatrix();
}
And the final draw function:
void draw(){
Main.drawTexture(x*tileSize, y*tileSize, tileSize, tileSize, FBOTexId);
}
And for all who want it my drawTexture function:
public static void drawTexture(float x, float y, float xSize, float ySize, float rot, int textureId){
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
GL11.glMatrixMode(GL11.GL_TEXTURE);
GL11.glLoadIdentity();
GL11.glTranslatef(0.5f,0.5f,0.0f);
GL11.glRotatef(rot,0.0f,0.0f,1.0f);
GL11.glTranslatef(-0.5f,-0.5f,0.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(x,y);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(x+xSize,y);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(x+xSize,y+ySize);
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(x,y+ySize);
GL11.glEnd();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
If I replace the Main.draw line in the draw() function with this Main.drawTexture(x*tileSize, y*tileSize, tileSize, tileSize, textureRotation, textureId);
it directly draws the texture to the correct position on the screen, but the problem is that I would always have to redraw all layers of textures and with the FBO's I draw all the layers everytime something changes and then only draw the texture from the FBO.
FBOTexId
, is this a custom function? it should beglGenTextures(1, &FBOTexId)
. Same withglGenFramebufferEXT
? – tamatoglTexImage2d
last argument, you will be filling that in by drawing to the FBO – tamatoGL_RGBA
as the 7th argument toglTexImage2D
just to things consistent with the 3rd argument. – tamato