OpenSceneGraph integration into Qt Quick

后端 未结 1 1807
情书的邮戳
情书的邮戳 2020-12-22 01:18

I want to integrate OSG scene into my Qt Quick application.

It seems that the proper way to do it is to use QQuickFramebufferObject class and call

相关标签:
1条回答
  • 2020-12-22 01:34

    Is it possible to integrate OpenSceneGraph into Qt Quick using QQuickFramebufferObject correctly or it is better to use implementation that uses QQuickItem and separate OpenGL context?

    The easiest way would be using QQuickPaintedItem which is derived from QQuickItem. While it is by default offering raster-image type of drawing you can switch its render target to OpenGL FramebufferObject:

    QPainter paints into a QOpenGLFramebufferObject using the GL paint engine. Painting can be faster as no texture upload is required, but anti-aliasing quality is not as good as if using an image. This render target allows faster rendering in some cases, but you should avoid using it if the item is resized often.

    MyQQuickItem::MyQQuickItem(QQuickItem* parent) : QQuickPaintedItem(parent)
    {
        // unless we set the below the render target would be slow rastering
        // but we can definitely use the GL paint engine just by doing this:
        this->setRenderTarget(QQuickPaintedItem::FramebufferObject);
    }
    

    How do we render with this OpenGL target then? The answer can be still good old QPainter filled with the image called on update/paint:

    void MyQQuickItem::presentImage(const QImage& img)
    {
        m_image = img;
        update();
    }
    
    // must implement
    // virtual void QQuickPaintedItem::paint(QPainter *painter) = 0
    void MyQQuickItem::paint(QPainter* painter)
    {
        // or we can precalculate the required output rect
        painter->drawImage(this->boundingRect(), m_image);
    }
    

    While QOpenGLFramebufferObject used behind the scenes here is not QQuickFramebufferObject the semantics of it is pretty much what the question is about and we've confirmed with the question author that we can use QImage as a source to render in OpenGL.

    P.S. I successfully use this technique since Qt 5.7 on PC desktop and singleboard touchscreen Linux device. Just a bit unsure of Android.

    0 讨论(0)
提交回复
热议问题