简体   繁体   English

OpenSceneGraph集成到Qt Quick中

[英]OpenSceneGraph integration into Qt Quick

I want to integrate OSG scene into my Qt Quick application. 我想将OSG场景集成到我的Qt Quick应用程序中。

It seems that the proper way to do it is to use QQuickFramebufferObject class and call osgViewer::Viewer::frame() inside QQuickFramebufferObject::Renderer::render() . 似乎正确的方法是使用QQuickFramebufferObject类并在QQuickFramebufferObject::Renderer::render()调用osgViewer::Viewer::frame() QQuickFramebufferObject::Renderer::render() I've tried to use https://bitbucket.org/leon_manukyan/qtquick2osgitem/overview . 我试过使用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. 例如,在Android平台中,此代码仅呈现第一帧。

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() . 我认为问题是QQuickFramebufferObject对Qt快速场景图和QQuickFramebufferObject::Renderer::render()调用的代码使用相同的OpenGL上下文。

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 ? 所以我想知道,是否有可能使用QQuickFramebufferObject正确地将OpenSceneGraph集成到Qt Quick中,或者最好使用使用QQuickItem实现和单独的OpenGL上下文,例如https://github.com/podsvirov/osgqtquick

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? 是否可以正确使用QQuickFramebufferObject将OpenSceneGraph集成到Qt Quick中,或者最好使用使用QQuickItem和单独的OpenGL上下文的实现?

The easiest way would be using QQuickPaintedItem which is derived from QQuickItem. 最简单的方法是使用QQuickItem派生的QQuickPaintedItem。 While it is by default offering raster-image type of drawing you can switch its render target to OpenGL FramebufferObject : 虽然它默认提供光栅图像类型的绘图,但您可以将其渲染目标切换为OpenGL FramebufferObject

QPainter paints into a QOpenGLFramebufferObject using the GL paint engine. QPainter使用GL绘制引擎绘制到QOpenGLFramebufferObject中。 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? 那么我们如何用这个OpenGL目标渲染呢? The answer can be still good old QPainter filled with the image called on update/paint: 答案仍然可以很好旧QPainter充满了更新/ 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. 虽然QOpenGLFramebufferObject使用的幕后这里就不QQuickFramebufferObject它的语义是非常的问题是关于什么的,我们已经与这个问题笔者,我们可以使用的QImage作为在OpenGL渲染源证实。

PS I successfully use this technique since Qt 5.7 on PC desktop and singleboard touchscreen Linux device. PS我从Qt 5.7成功使用这种技术在PC桌面和单板触摸屏Linux设备上。 Just a bit unsure of Android. 对Android有点不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM