开发者

Render to FBO not working, gDEBugger says otherwise

开发者 https://www.devze.com 2023-01-11 01:13 出处:网络
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.

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);
   开发者_Go百科 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消