OpenGL fragment shader not writing to fbo color buffer

I'm having trouble writing from my fragment shader to a color buffer in the FBO. The idea is to draw to this texture, and then draw this texture into a quad. The thing is, the shader works when drawing to the default window buffer, since if I remove the FBO, the scene is displayed. And the texturing of the quad works too, I tried it with an imported texture, and it showed. All I'm getting is a black screen. What could have gone wrong? :(

Code for creating the 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);

Code for rendering:

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

Code for the quad:

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

And lastly, the frag shader, I just set it to draw everything to a solid color:

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

EDIT: If it is of any help, the whole thing is in a Qt GLWidget.


Are you careful to make sure that you're texture is not simultaneously bound to a sampler (glBindTexture), and a framebuffer? I believe the behavior is either an error or undefined if you try to write to a texture while it's bound to an active sampler, or if you try to sample a texture while it's attached to an active framebuffer.

链接地址: http://www.djcxy.com/p/33922.html

上一篇: 关于渲染到纹理

下一篇: OpenGL片段着色器不写入fbo颜色缓冲区