OpenSceneGraph 集成到 Qt Quick [英] OpenSceneGraph integration into Qt Quick

查看:19
本文介绍了OpenSceneGraph 集成到 Qt Quick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 OSG 场景集成到我的 Qt Quick 应用程序中.

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

似乎正确的方法是使用 QQuickFramebufferObject 类并在 QQuickFramebufferObject::Renderer 中调用 osgViewer::Viewer::frame()::渲染().我尝试使用 https://bitbucket.org/leon_manukyan/qtquick2osgitem/overview.

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.

但是,这种方法似乎并非在所有情况下都能正常工作.例如,在 Android 平台中,此代码仅呈现第一帧.

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

我认为问题在于 QQuickFramebufferObject 对 Qt Quick Scene Graph 和在 QQuickFramebufferObject::Renderer::render() 中调用的代码使用相同的 OpenGL 上下文.

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 将 OpenSceneGraph 集成到 Qt Quick 中,或者最好使用使用 QQuickItem 的实现并分离 OpenGL 上下文等作为 https://github.com/podsvirov/osgqtquick?

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?

推荐答案

是否可以将 OpenSceneGraph 集成到 Qt Quick 中使用QQuickFramebufferObject 正确或最好使用使用 QQuickItem 和单独的 OpenGL 上下文的实现?

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?

最简单的方法是使用派生自 QQuickItem 的 QQuickPaintedItem.虽然它默认提供光栅图像类型的绘图,但您可以将其渲染目标切换为 OpenGL FramebufferObject:

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 使用 GL 绘制绘制到 QOpenGLFramebufferObject引擎.绘画可以更快,因为不需要上传纹理,但是抗锯齿质量不如使用图像.这个渲染target 在某些情况下允许更快的渲染,但你应该避免如果项目经常调整大小,则使用它.

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);
}

那么我们如何使用这个 OpenGL 目标进行渲染呢?答案仍然可以是旧的 QPainter,在 update/paint 上填充了调用的图像:

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);
}

虽然这里使用的 QOpenGLFramebufferObject 不是 QQuickFramebufferObject 它的语义几乎就是问题所在,我们已经确认问题作者,我们可以使用 QImage 作为源在 OpenGL 中进行渲染.

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.

附:自 Qt 5.7 以来,我在 PC 桌面和单板触摸屏 Linux 设备上成功使用了这种技术.只是有点不确定Android.

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.

这篇关于OpenSceneGraph 集成到 Qt Quick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆