Question

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.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top