CHAI3DCHAI3D

unanswered Incorrect OpenGL state restoration in cFrameBuffer

More
08 Jul 2018 19:09 #1

Hello!

There is an error in chai3d::cFrameBuffer regarding the restoration of the OpenGL settings.

The method chai3d::cFrameBuffer::renderFinalize() restores OpenGL settings assuming that the rendering is performed in default frame buffer with id 0:

bool cFrameBuffer::renderFinalize() {
//...
    glBindFramebuffer (GL_FRAMEBUFFER, 0);
//...
}

This breaks the rendering that uses aframe buffer object as its target. This is the case of the modern QOpenGLWidget, for example.

To fix this problem it is required to store the current frame buffer id in the chai3d::cFrameBuffer::renderInitialize() function:
bool cFrameBuffer::renderInitialize()
{
    //...
    if (glIsFramebuffer(m_fbo) == false)
    {
        glGenFramebuffers(1, &m_fbo);
    }

    glGetIntegerv (GL_FRAMEBUFFER_BINDING, &m_originFrameBuffer);

    // to perform any operations on a FBO you need to bind it, much like you 
    // would a VBO or texture, so that the operations can be performed on it, 
    // this is done via the following code
    glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);    
    //...
}

And to bind the stored buffer id in the chai3d::cFrameBuffer::renderFinalize() function:
bool cFrameBuffer::renderFinalize() {
//...
    glBindFramebuffer (GL_FRAMEBUFFER, m_originFrameBuffer);
//...
}

Please Log in or Create an account to join the conversation.