Search code examples
openglframebufferperspectivecameraprojection-matrix

OpenGL: Loading rendered texture from Framebuffer objects


I am doing a two-pass blur into a framebuffer object. To make sure that in the FBO, the whole scene is covered with the image I am trying to blur. Here is the process.

  1. I have setup an FBO with the dimensions of the image I need to blur.
  2. I am setting up an Orthographic Projection using the following function (called as setupOrtho(FBO's dimensions) ):

    glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0,1,0,1,-1,1) glMatrixMode(GL_MODELVIEW) glLoadIdentity()

  3. Draw to the FBO, using this projection. Unbind the FBO (back to the screen).
  4. Switch to the Perspective View by calling setupPerspective(window's dimensions) and replacing the glOrtho above with glFrustum.

If I draw simple white quads, the view switching works as expected. One quad is drawing in Orthographic Projection and other in a Perspective View. Now take the rendered texture from the FBO (let id be RENDEREDTEXTURE).

If I bind the RENDEREDTEXTURE in an Orthographic Projection in my main scene, it shows the blurred Image. If I bind it into the Perspective View in my main scene, the previously visible white quad (which was drawn in the Perspective View) disappears.

SOLUTION: Posted as an answer.

NOTE : Never forget MIPMAP generation when using textures.

What could be the issue here?

Please suggest any alternative means such that in the FBO, only the image I want to process on is visible (for which I did the orthographic projection), and in the main scene, the processed image is just like any other texture loaded from an image file.


Solution

  • For the framebuffer textures, Mipmaps are not generated automatically for the textures. They have to be manually triggered, after your framebuffer has rendered the scene you want.

    Use the following snippet, right after you switch back to default framebuffer.

        glBindTexture(GL_TEXTURE_2D, YOUR_FINAL_RENDERED_TEXTURE_ID)
        glGenerateMipmap(GL_TEXTURE_2D)
        glBindTexture(GL_TEXTURE_2D, 0)
    

    Then you can use YOUR_FINAL_RENDERED_TEXTURE_ID as you would normally to bind this texture anywhere.