1

我无法从片段着色器写入 FBO 中的颜色缓冲区。这个想法是绘制到这个纹理,然后把这个纹理绘制成一个四边形。问题是,着色器在绘制到默认窗口缓冲区时起作用,因为如果我删除 FBO,则会显示场景。并且四边形的纹理也有效,我用导入的纹理尝试了它,它显示了。我得到的只是黑屏。可能出了什么问题?:(

创建 FBO 的代码:

glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

glGenRenderbuffers(1, &depthbuff); //Add a depth renderbuffer for depth testing
glBindRenderbuffer(GL_RENDERBUFFER, depthbuff);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuff);

//Add the buffer to be drawn to and the buffer for the picking information
glGenTextures(1, &drawbuff);
glBindTexture(GL_TEXTURE_2D, drawbuff);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, drawbuff, 0);

glGenTextures(1, &pickingbuff);
glBindTexture(GL_TEXTURE_2D, pickingbuff);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, pickingbuff, 0);

渲染代码:

glBindFramebuffer(GL_FRAMEBUFFER, fbo);
GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
glDrawBuffers(2, buffers);

shader->bind(); //If the three lines above are removed, it draws correctly to the window
drawStuff();

四边形代码:

quadshader->bind();
glEnable(GL_TEXTURE_2D);
int drawTexID = quadshader->uniformLocation("drawTex");

glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, anotherTexture); //This works. A texture imported from a TGA
glBindTexture(GL_TEXTURE_2D, drawbuff);
glUniform1i(drawTexID, 0);

drawQuad();

最后,片段着色器,我只是将其设置为将所有内容绘制为纯色:

void main(void) {
    gl_FragData[0] = vec4(1.0, 1.0, 0.0, 1.0);
    gl_FragData[1] = vec4(0.0, 1.0, 1.0, 1.0);
}

编辑:如果有任何帮助,整个事情都在 Qt GLWidget 中。

4

1 回答 1

1

您是否小心确保您的纹理不会同时绑定到采样器(glBindTexture)和帧缓冲区?我相信如果您在绑定到活动采样器时尝试写入纹理,或者如果您尝试在纹理附加到活动帧缓冲区时对其进行采样,则该行为是错误或未定义的。

于 2012-05-14T20:23:11.337 回答