Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

can't read values from QOpenGLFramebuffer 2nd color attachment



  • I have a QOpenGLFramebufferObject that I've been writing to and reading from using texture() in another part of the application. I've added a second color attachment to include some additional data, but now I'm having trouble retrieving it.

        _drawFbo = new QOpenGLFramebufferObject(PAINT_FBO_WIDTH, PAINT_FBO_WIDTH, QOpenGLFramebufferObject::Depth);
    
        // I've also tried without the format (uses GL_RGBA)
        _drawFbo->addColorAttachment(PAINT_FBO_WIDTH, PAINT_FBO_WIDTH, GL_RGBA32F);
    

    And then in my shader I write to both places when the shader is bound:

    layout(location=0) out vec4 meshWithPaintColor;
    layout(location=1) out vec4 primitiveId;
    
    void main() {
        ...
        meshWithPaintColor = vec4(finalColor, 0);
        primitiveId = vec4(1,1,1,1);
    

    When I try reading from this 2nd attachment using the textures()[1] value bound to a shader sampler the values always seem to be zero.

    Do I need to do anything with the QOpenGLFramebufferObject to allow drawing to the second color attachment?



  • I did in fact have to call glDrawBuffers myself. I assumed this was handled by the FBO binding, but apparently not.

    QOpenGLExtraFunctions* f = QOpenGLContext::currentContext()->extraFunctions();
    GLenum bufs[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
    f->glDrawBuffers(2, bufs);
    

    This seems strange to me that the FBO abstraction supports color attachments, but requires extra functions to use them.



  • I did in fact have to call glDrawBuffers myself. I assumed this was handled by the FBO binding, but apparently not.

    QOpenGLExtraFunctions* f = QOpenGLContext::currentContext()->extraFunctions();
    GLenum bufs[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
    f->glDrawBuffers(2, bufs);
    

    This seems strange to me that the FBO abstraction supports color attachments, but requires extra functions to use them.