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

Artifacts when rendring QOpenGLWidget to QImage



  • Hello,

    I have a QOpenGLWidget that I want to render to an png image.

    This is my code (in a method of class derived from QOpenGLWIdget):

    QSize size = QSize(scale*this->width(), scale*this->height());
    QImage img = QImage(size, QImage::Format_RGB32);
    QPainter painter(&img);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    this->render(&painter);
    img.save(filename);
    

    Now the thing that I want to render e.g. looks like this.
    As the screenshot taken from the GUI shows it looks like it should.
    ogl_good.jpeg

    However when I want to save the exact same thing (using the code above) I get following:
    ogl_bad.png
    So it seems that the regions where there is lots of specular reflections first get yellow, and if its even more then black.

    Why is that?



  • @gde23 Where is the drawing code? What is rendered to img through QPainter?



  • @nagesh : thanks for the answer however I dont thinkt it has to do we the opengl drawing code. Nothing special is happening there, just some triangles.

    I found a solution/workaround myself:
    Instead of rendering via QPainter there is a method in QOpenGLWidget that I didn't know existed:

    QOpenGLWIdget::grabFramebuffer()
    

    This woks for me and I can obtain the image without the artifacts. I still don't understand what went wrong in the first place, but anyway, that's fine.



  • @gde23 good that you got the solution

    Problem in earlier code might be because of
    1)img is unintialized // fill with transparency
    2)in order to take the screen shot , you are doing again drawing in img. rather than direct screen grab of window


Log in to reply