Search code examples
openglfbo

How to retrieve number of attachments to FBO


How can I get number of color attachments to currently bind FBO? I checked glGetInteger and glGetFramebufferAttachmentParameteriv but they don't have enum to get these values.


Solution

  • Untested, but this should do it:

    GLint maxAtt = 0;
    glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAtt);
    int nAtt = 0;
    for (int iAtt = 0; iAtt < maxAtt; ++iAtt) {
        GLint objType = GL_NONE;
        glGetFramebufferAttachmentParameteriv(
            GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + iAtt,
            GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &objType);
        if (objType != GL_NONE) {
            ++nAtt;
        }
    }
    // nAtt is the number of color attachments.
    

    You could do something similar using GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME instead of GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, and comparing the value to 0.