Opengl 在OpenSceneGraph场景中显示Qt Quick内容
我想在OpenSceneGraph场景中的虚拟屏幕上显示我的Qt Quick内容 我现在使用的方法效率很低:Opengl 在OpenSceneGraph场景中显示Qt Quick内容,opengl,qtquick2,openscenegraph,Opengl,Qtquick2,Openscenegraph,我想在OpenSceneGraph场景中的虚拟屏幕上显示我的Qt Quick内容 我现在使用的方法效率很低: 使用FBO(FrameBufferObject)将Qt快速渲染到屏幕外曲面 使用QOpenGLFramebufferObject::toImage()下载像素 将像素上传到OSG 这就是GPU-CPU-GPU传输 一个合适的解决方案应该以某种方式利用现有的FBO,并且能够仅在GPU内部传输数据 有两种选择: 在Qt侧创建FBO,并在OSG侧使用其纹理 在OSG侧创建FBO并将其馈送到Q
setupviewerambeddedinwindow
代码:
请记住,一次只能有一个活动上下文
你的计划是:
0. create osg::Texture out of QOpenGLFrameBufferObject::texture()
1. make QtQuick context active
2. render QtQuick to texture
3. make primary (OSG) context active
4. render OSG
5. goto 1
制作合适的osg::纹理
由于OSG和Qt API不兼容,您几乎无法按原样将QOpenGLFrameBufferObject
链接到OSG::Texture2D
QOpenGLFrameBufferObject具有返回opengl纹理id的QOpenGLFrameBufferObject::texture()
方法,但osg::texture自己管理所有opengl内容
类似于osg::Texture2D(uint-textureId)代码>可以帮助我们,但它根本不存在
让我们自己做一个
osg::Texture
由存储OpenGL纹理id和一些其他数据的osg::TextureObject
支持。如果我们使用给定的纹理id构造osg::TextureObject
,并将其传递给osg::texture
,后者将使用它作为自己的纹理
代码:
完成演示项目
void Widget::initializeGL()
{
QOpenGLContext* qmlGLContext = new QOpenGLContext(this);
// ...
qmlGLContext->setShareContext(context());
qmlGLContext->create();
}
0. create osg::Texture out of QOpenGLFrameBufferObject::texture()
1. make QtQuick context active
2. render QtQuick to texture
3. make primary (OSG) context active
4. render OSG
5. goto 1
void Widget::createOsgTextureFromId(osg::Texture2D* texture, int textureId)
{
osg::Texture::TextureObject* textureObject = new osg::Texture::TextureObject(texture, textureId, GL_TEXTURE_2D);
textureObject->setAllocated();
osg::State* state = m_osgGraphicsContext->getState();
texture->setTextureObject(state->getContextID(), textureObject);
}