Important: Please read the Qt Code of Conduct -

Multiple Render Targets with QOpenGLFramebufferObject

  • I have a problem with multiple render target while I'm using QOpenGLFramebufferObject.

    As a test, I'm currently trying to clear 3 buffers with different colors. If my program follows my intention,
    COLOR_ATTACHMENT0 will be cleared with RED = { 1.0, 0.0, 0.0, 1.0 }.
    COLOR_ATTACHMENT1 will be cleared with GREEN = { 0.0, 1.0, 0.0, 1.0 }.
    Default render buffer will be cleared with BLUE = { 0.0, 0.0, 1.0, 1.0 }.

    However, both the COLOR_ATTACHMENT 0 and 1 are cleared with RED.
    The default render buffer is properly cleared with BLUE.

    Maybe, I misunderstood the usage of QOpenGLFramebufferObject, but I currently have no idea about the problem.

    If some knows the correct usage, I would like to ask some advice to revise my code.
    Following is my current code.

    Thank you.

    // GLWidget class inherits QOpenGLWidget
    GLWidget::GLWidget(QWidget* parent)
        : QOpenGLWidget{ parent } {
    GLWidget::~GLWidget() {
    void GLWidget::initializeGL() {
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    void GLWidget::paintGL() {
        // Initialize FBO.
        QOpenGLFramebufferObjectFormat format;
        QOpenGLFramebufferObject fbo(width(), height(), format);
        fbo.addColorAttachment(width(), height());
        // Clear buffers.
        QOpenGLExtraFunctions* f = QOpenGLContext::currentContext()->extraFunctions();
        GLenum bufs[2] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
        f->glDrawBuffers(2, bufs);
        GLfloat red[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
        f->glClearBufferfv(GL_COLOR, 0, red);
        GLfloat green[4] = { 0.0f, 1.0f, 0.0f, 1.0f };
        f->glClearBufferfv(GL_COLOR, 1, green);
        // Save images.
        QImage img1 = fbo.toImage(false, 0);"red.png");
        QImage img2 = fbo.toImage(false, 1);"green.png");
        // Clear default render buffer.
    void GLWidget::resizeGL(int width, int height) {
        glViewport(0, 0, width, height);

  • I was ashamed of my carelessness but the problem is caused by the GPU driver which is not the latest one.

    My graphics card is NVIDIA GeForce 960 GTX that supports OpenGL 4.5. However, some of the modern functions in OpenGL 4.x did not work properly on the old graphics drivers.

    If someone has the similar problem, updating the version of your GPU driver can solve the problem.

    Thank you.

Log in to reply