qtopenglglslfbo

can't draw to second color attachment of QOpenGLFramebufferObject


I have a QOpenGLFramebufferObject that I've been writing to and reading from using texture() in my application. I've added a second color attachment to include some additional data, but it seems like no data is being written to it.

// creating the FBO (this has been working)
_drawFbo = new QOpenGLFramebufferObject(PAINT_FBO_WIDTH, PAINT_FBO_WIDTH, QOpenGLFramebufferObject::Depth);

// now I'm adding another color attachment
_drawFbo->addColorAttachment(PAINT_FBO_WIDTH, PAINT_FBO_WIDTH);

And then in my shader I write to both attachments 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?


Solution

  • 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.