0
votes

I am trying to render to an fbo and then use that texture as an input to my second render pass for post processing, but it seems that glClear and glClearColor affect the texture that has been rendered to. How can I make them only affect the display buffer?

My code looks something like this:

UseRenderingShaderProgram();
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

renderWorld();

// render to screen
UsePostProcessingShaderProgram();
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glClearColor(0.0, 0.0, 0.0, 1.0); // <== texture appears to get cleared in this two lines.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

renderWorld();

glfwSwapBuffers();
1
"texture appears to get cleared in this two lines." - That's not the case, your error is elsewhere.Christian Rau

1 Answers

1
votes

If I had to make an educated guess you're defined your texture to use mipmap minification filtering. After rendering to a texture using a FBO only one mipmap level is defined. But without all the mipmap levels selected being created the texture is incomplete and will not deliver data. The easiest solution would be disabling mipmapping for this texture by setting its minification filter parameter

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

Also you must make sure that you're correctly unbinding and binding the texture being attached to the FBO.

  • Unbind the texture before binding the FBO (you can have it attached to it the whole time safely).

  • Unbind the FBO before binding the texture as image source for rendering.

Adding those two changes (binding order and mipmap levels) should make your texture appear.