Mathias
Mathias

Reputation: 334

Render to FBO not working, gDEBugger says otherwise

I'm trying to render to a texture using an FBO. When trying to do so, gDEBugger shows the correct texture, but when drawing it on a quad its just "white" / the glColor4f.

Here is the code to create the texture, fbo and renderbuffer:

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);

glGenRenderbuffers(1, &rb);
glBindRenderbuffer(GL_RENDERBUFFER, rb);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb); 

glBindFramebuffer(GL_FRAMEBUFFER, 0); 
glBindRenderbuffer(GL_RENDERBUFFER, 0);

Render to the texture:

glBindFramebuffer(GL_FRAMEBUFFER, fbo);     
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor4f(1.0f, 0.5f, 0.2f, 1.0f);

glBegin(GL_TRIANGLES);
    glVertex3f(10, 10, 0);
    glVertex3f(210, 30, 1);
    glVertex3f(50, 150, 1);
glEnd();

glBindFramebuffer(GL_FRAMEBUFFER, 0); 

And here is how I render the quad with the texture:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex2f(0, 0);
    glTexCoord2f(1.0f, 1.0f);
    glVertex2f(width, 0);
    glTexCoord2f(1.0f, 0.0f);
    glVertex2f(width, height);
    glTexCoord2f(0.0f, 0.0f);
    glVertex2f(0, height);
glEnd();

glDisable(GL_TEXTURE_2D);

When drawing with a loaded image as a texture it works, but not with the FBO bound textures. Anyone got an idea on what is wrong with my code?

Upvotes: 2

Views: 831

Answers (1)

Bahbar
Bahbar

Reputation: 18015

Your texture looks incomplete.

You don't have mipmaps for it, and you did not select a filtering mode that would work-around that.

Try:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

That said, you should still see the polygons, but without the proper texture, without this.

Upvotes: 4

Related Questions