现在的位置: 首页 > 综合 > 正文

5 introduce FBO

2013年10月01日 ⁄ 综合 ⁄ 共 2305字 ⁄ 字号 评论关闭

FBO: framebuffer object.
framebuffer is the final rendering destination of OpenGL pipeline. by default it's created and managed by window system. FBO is non-displayable created by OpenGL.

OpenGL app can redirect the rendering output to the FBO.

FBO contains a collection of rendering destinations: color, depth and stencil buffer. (only default framebuffer has a accumulation buffer.)  these are called framebuffer-attachable images.

there are two types of framebuffer-attachable images: texture images and renderbuffer images.

details: OpenGL Frame Buffer Object (FBO)

CreateFBO()
{
    // Get the currently bound frame buffer object. On most platforms this just gives 0.
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &m_i32OriginalFbo);
   
    // Generate and bind a render buffer which will become a depth buffer shared between our two FBOs

   /* Note that not only a texture image is attached to the FBO, but also, a renderbuffer image is attached to the depth attachment point of     the FBO. We do not actually use this depth buffer, however, the FBO itself needs it for depth test. If we don't attach this depth renderable     image to the FBO, then the rendering output will be corrupted because of missing depth test. If stencil test is also required during FBO     rendering, then additional renderbuffer image should be attached to GL_STENCIL_ATTACHMENT_EXT.
    */

    glGenRenderbuffers(1, &m_uDepthBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, m_uDepthBuffer);

    // Create a texture for rendering to
    glGenTextures(1, &m_uiTextureToRenderTo);
    glBindTexture(GL_TEXTURE_2D, m_uiTextureToRenderTo);
   
    // Create the object that will allow us to render to the aforementioned texture
    glGenFramebuffers(1, &m_uFBO);
    glBindFramebuffer(GL_FRAMEBUFFER, m_uFBO);

    // Attach the texture to the FBO
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_uiTextureToRenderTo, 0);

    // Attach the depth buffer we created earlier to our FBO.
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_uDepthBuffer);
}

Render()
{
    // step 1: render the object to "FBO": m_uiTextureToRenderTo
    {
        // Bind our FBO
        glBindFramebuffer(GL_FRAMEBUFFER, m_uFBO);
       
        DrawMesh()
       
        // We are done with rendering to our FBO so switch back to the back buffer.
        glBindFramebuffer(GL_FRAMEBUFFER, m_i32OriginalFbo);
    }
   
    // step 2: use the texture generated in step 1 as a texture.
    {
        // Bind our texture that we have rendered to
        glBindTexture(GL_TEXTURE_2D, m_uiTextureToRenderTo);

        // Draw our textured cube
        DrawMesh()
    }
}

抱歉!评论已关闭.