0

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.

Ranking
  • 11
  • 4
  • 1
    Did you set the viewport to the size of a tile? – Robinson Aug 02 '13 at 13:50
  • in your first code block your assigning to `FBOTexId`, is this a custom function? it should be `glGenTextures(1, &FBOTexId)`. Same with `glGenFramebufferEXT`? – tamato Aug 02 '13 at 22:43
  • There is no reason to pass in info into `glTexImage2d` last argument, you will be filling that in by drawing to the FBO – tamato Aug 02 '13 at 22:45
  • What is your clear color? Can you show the code for when you are drawing to the framebuffer – tamato Aug 02 '13 at 22:47
  • You may want to use `GL_RGBA` as the 7th argument to `glTexImage2D` just to things consistent with the 3rd argument. – tamato Aug 02 '13 at 22:50
  • @tamato thx for all the replies^^ wasn't able to get to my computer for some time now due to some problems but here the answers to your comments: 1. I'm using the lwjgl which added the function glGenTextures() which gives back an new texture id so its basically the same (atleast I think so) 2. But it isn't wrong also right? 3. the clear color is 0.0, 0.0, 0.0, 0.0; I'm drawing to the framebuffer in the render() function. the fbotex itself is then drawn in the draw() function. 4. Will change that. – Ranking Aug 08 '13 at 10:14

1 Answers1

0

Well first thanks for all the comments. I havent realy found the error nor do I understand why this is necessary but this is how it works:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 

By adding these two lines of code after the glBindTexture(GL_TEXTURE_2D, FBOTextId) in the setupFBO() function everything works fine. If u replace the GL_NEAREST in above lines with GL_LINEAR works too, but it has some strange artifacts when scaling the image.

By the way just a short question which has nothing to do with this thread: Does someone know a way of making parts of a texture (by drawing to it with an fbo) transparant by evaluating the colors from another texture?

Ranking
  • 11
  • 4