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

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

问题描述

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

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

似乎正确的方法是使用QQuickFramebufferObject类并在QQuickFramebufferObject::Renderer::render()中调用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快速场景图和在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上下文的实现,例如

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

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 引擎.绘画可以更快,因为不需要上传纹理,但是 抗锯齿质量不如使用图像好.这个渲染 目标可以在某些情况下加快渲染速度,但您应避免 如果经常调整尺寸,则使用它.

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.

P.S.自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天全站免登陆