OpenSceneGraph integration into Qt Quick

旧街凉风 提交于 2019-11-28 11:29:22

问题


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 osgViewer::Viewer::frame() inside QQuickFramebufferObject::Renderer::render(). I've tried to use https://bitbucket.org/leon_manukyan/qtquick2osgitem/overview.

However, it seems this approach doesn't work correctly in all cases. For example, in Android platform this code renders only the first frame.

I think the problem is that QQuickFramebufferObject uses the same OpenGL context both for Qt Quick Scene Graph and code called within QQuickFramebufferObject::Renderer::render().

So I'm wondering, 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 such as https://github.com/podsvirov/osgqtquick?


回答1:


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.



来源:https://stackoverflow.com/questions/46187024/openscenegraph-integration-into-qt-quick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!