Rendering a QImage on openGL



  • Hello,
    I am trying to render a QImage on an openGL surface. I have read/studied many tutorials but no luck so far. If someone could point me where my mistake is...

        QOpenGLContext context;
        context.create();
        QOffscreenSurface surface;
        surface.setFormat(QSurfaceFormat::defaultFormat());
        surface.create();
        context.makeCurrent(&surface);
        QOpenGLFramebufferObjectFormat fboFormat;
        fboFormat.setSamples(16);
        fboFormat.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil);
        const int w = 800;
        const int h = 600;
        QOpenGLFramebufferObject fbo(w, h, fboFormat);
        fbo.bind();
        QOpenGLPaintDevice device(QSize(w, h));
        QPainter pnt(&device);
        pnt.setRenderHint(QPainter::Antialiasing);
        pnt.setRenderHint(QPainter::TextAntialiasing);
        pnt.setRenderHint(QPainter::SmoothPixmapTransform);
        pnt.fillRect(QRect(0, 0, w, h), Qt::green);
        pnt.end();
        QImage pix(50, 100, QImage::Format_ARGB32_Premultiplied);
        pix.fill(Qt::red);
        glEnable(GL_TEXTURE_2D);
        QOpenGLTexture *texture = new QOpenGLTexture(pix.mirrored(), QOpenGLTexture::DontGenerateMipMaps);
        texture->bind();
        glEnable(GL_DEPTH_TEST);
        glPushMatrix();
        glTranslatef(10, 10, 0);
        glBegin(GL_QUADS);
        glTexCoord2i(0, 0); glVertex3f(0, 0, 0);
        glTexCoord2i(1, 0); glVertex3f(pix.width(), 0, 0);
        glTexCoord2i(1, 1); glVertex3f(pix.width(), pix.height(), 0);
        glTexCoord2i(0, 1); glVertex3f(0, pix.height(), 0);
        glEnd();
        glPopMatrix();
        glFlush();
        texture->release();
        glDisable(GL_TEXTURE_2D);
        delete texture;
        fbo.release();
        fbo.toImage().save("opengl.png");
    

    All I get is a plain green image... I have tried many other things and options, this version seems to me the more logical, yet it does not work.

    Thanks in advance for unlocking me...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.