1
votes

I have encountered some problem with QGLFrameBufferObject, when I change my OS from win7 to Ubuntu 12.04 LTS. After recompiling the code in the new ubuntu system, the color goes wrong. So I made a minimal test. I just clear the color buffer with color (1.0, 0.0, 0.0, 0.8), and that's it.

I get the correct result when I render directly to the screen, with all pixels being (1.0, 0.0, 0.0, 0.8).

However, the result from offline rendering (with QGLFrameBufferObject) is wrong, as given by offline.png. This color is (0.3, 0.0, 0.0, 0.8).

This problem happens when alpha>0.0 and <1.0. For 0.0 and 1.0 it works fine.

I've compiled this test code on another computer (computer A) with win7 OS, everything works fine. Also tried on one more computer (computer B) with ubuntu OS, everything works fine.

I'm using a NVidia GeForce GTX 570 GPU, and have tried the latest stable driver (304.60) and an older version (290.10, used for computer B) for linux-64bit, but nothing changes.

Anyone knows why? Below is the code.

mywidget::mywidget(QWidget * parent) :
    QGLWidget(parent)
{
}

void mywidget::resizeGL(int width, int height )
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 1.0, 0.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void mywidget::paintGL()
{
    glClearColor(1.0, 0.0, 0.0, 0.8);

    // DRAW ON THE SCREEN
    {
        glClear(GL_COLOR_BUFFER_BIT);
    }

    QGLFramebufferObject fbo(width(), height());
    fbo.bind();

    // DRAW ON THE FBO USING THE SAME CODE AND THE SAME CONTEXT
    {
        glClearColor(1.0, 0.0, 0.0, 0.8);
        glClear(GL_COLOR_BUFFER_BIT);
    }

    fbo.release();
    fbo.toImage().save("offline.png");
}

I have noticed that there are two similar posts, but with no answer or detailed answer: Wrong alpha blending when rendering on a QGLFramebufferObject; Alpha compositing wrong when rendering to QGLFrameBufferObject vs screen

2
"fbo.toImage().save("offline.png");" I would be much more concerned that this is going wrong. What does the FBO look like if you blit it to the screen? - Nicol Bolas
Yes! It is the problem of toImage function. When I directly bind the texture of the fbo, I can see from the screen that it works fine. Thank you. Then I have to check what is the problem in toImage. - blueteaxk

2 Answers

0
votes

The question you linked give a hint: Nowhere in your code you enable blending. Try adding

 glEnable(GL_BLEND​​);

before doing any rendering.

0
votes

As @Nicol Bolas pointed out, the problem is in fbo.toImage().save("offline.png");, in the toImage() function. The returned image is created in 'QImage::Format_ARGB32_Premultiplied' format, but the pixel color data is set as if it is in QImage::Format_ARGB32 format. Perhaps this is a bug in Qt 4.8. I think by changing QImage::Format_ARGB32_Premultiplied to QImage::Format_ARGB32 in QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format, bool include_alpha) function will solve the problem. This code is in qgl.cpp. Still I haven't tried it.