渲染到FBO不工作,gDEBugger说不然

时间:2010-08-12 10:33:05

标签: c++ opengl

我正在尝试使用FBO渲染到纹理。当试图这样做时,gDEBugger显示正确的纹理,但是当它在四边形上绘制时它只是“白色”/ glColor4f。

以下是创建纹理,fbo和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);

渲染到纹理:

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); 

以下是我如何使用纹理渲染四边形:

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);

使用加载的图像作为纹理进行绘制时,它可以正常工作,但不能使用FBO绑定纹理。有人知道我的代码出了什么问题吗?

1 个答案:

答案 0 :(得分:4)

你的纹理看起来不完整。

您没有mipmap,并且没有选择可以解决此问题的过滤模式。

尝试:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

那就是说,你应该仍然可以看到多边形,但没有合适的纹理,没有它。