5 introduce FBO

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值