1
votes

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.

1
I'm seeing a distinct lack of framebuffer clearing code.Nicol Bolas
What do you mean? And by the way, the FBO returns complete.Sebastian Mendez
Calls to glClear or glClearBuffer. Clearing the framebuffer before using it. Where are you doing that?Nicol Bolas
At the start of the drawStuff function, I have glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); I dont have any glClearBuffer, what does it do?Sebastian Mendez
It turns out Nicol was right: Although I did clear the buffer for writing to the textures, i forgot to clear the default buffer just before drawing from the texture, and that's why it didn't refresh. Thanks again.Sebastian Mendez

1 Answers

1
votes

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.