经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
OSG嵌入QT的简明总结
来源:cnblogs  作者:charlee44  时间:2019/6/17 9:06:23  对本文有异议

1.解决方案

不得不说关于OSG的资料实在太零散了,搜索了很多关于OSG在QT下的解决方案,都是各有各的说法,有的说的不是很清楚,有的已经过时了。这里提供一下自己的解决方案吧。
在远古的OSG里面,应该是提供对QT的支持的;不过应该是由于QT版本变动比较大,现在的OSG版本应该已经没有了。但是在GitHub上又有了新的osgQt项目(地址:https://github.com/openscenegraph/osgQt) 用来解决这个问题。
osgQt是个简单的小项目,其实没有必要额外的编译,最核心的是个名为GraphicsWindowQt的类,只需要复制GraphicsWindowQt.h和GraphicsWindowQt.cpp到QT工程里面就可以使用了。同时osgQt给出了一个名为osgviewerQt的样例,也仅仅只是个cpp文件。结合两者,一个简单的示例就出来了。
QT工程如下:

编译运行后如下:

2.存在问题

1) 警告提示

上述项目直接运行,会出现诸如“QOpenGLContext::swapBuffers() called with non-exposed window, behavior is undefined”的警告。查阅网上的英文资料,大意说是因为OpenGL环境未初始化产生的。在osgviewerQt中绑定了一个定时器,每隔10ms就调用frame()来绘制一帧,而这个定时器是在构造函数的时候就开始调用了,没有等待QT中OpenGL环境的生成。在这里我把定时器的部分给改进了一下,等待OSG的环境初始化完成在启动定时器,这个警告就没有了。osgviewerQt改进后的代码如下:

  1. #include <QTimer>
  2. #include <QApplication>
  3. #include <QGridLayout>
  4. #include <osgViewer/CompositeViewer>
  5. #include <osgViewer/ViewerEventHandlers>
  6. #include <osgGA/MultiTouchTrackballManipulator>
  7. #include <osgDB/ReadFile>
  8. #include "GraphicsWindowQt"
  9. #include <iostream>
  10. class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
  11. {
  12. public:
  13. ViewerWidget(QWidget* parent = 0, Qt::WindowFlags f = 0, osgViewer::ViewerBase::ThreadingModel threadingModel=osgViewer::CompositeViewer::SingleThreaded) : QWidget(parent, f)
  14. {
  15. setThreadingModel(threadingModel);
  16. // disable the default setting of viewer.done() by pressing Escape.
  17. setKeyEventSetsDone(0);
  18. QWidget* widget1 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/cow.osgt") );
  19. QWidget* widget2 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/glider.osgt") );
  20. QWidget* widget3 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/axes.osgt") );
  21. QWidget* widget4 = addViewWidget( createGraphicsWindow(0,0,100,100), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/fountain.osgt") );
  22. // QWidget* popupWidget = addViewWidget( createGraphicsWindow(900,100,320,240,"Popup window",true), osgDB::readRefNodeFile("D:/Work/OSGBuild/OpenSceneGraph-Data/dumptruck.osgt") );
  23. // popupWidget->show();
  24. QGridLayout* grid = new QGridLayout;
  25. grid->addWidget( widget1, 0, 0 );
  26. grid->addWidget( widget2, 0, 1 );
  27. grid->addWidget( widget3, 1, 0 );
  28. grid->addWidget( widget4, 1, 1 );
  29. setLayout( grid );
  30. //connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
  31. //_timer.start( 10 );
  32. }
  33. QWidget* addViewWidget( osgQt::GraphicsWindowQt* gw, osg::ref_ptr<osg::Node> scene )
  34. {
  35. osgViewer::View* view = new osgViewer::View;
  36. addView( view );
  37. osg::Camera* camera = view->getCamera();
  38. camera->setGraphicsContext( gw );
  39. const osg::GraphicsContext::Traits* traits = gw->getTraits();
  40. camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
  41. camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
  42. // set the draw and read buffers up for a double buffered window with rendering going to back buffer
  43. camera->setDrawBuffer(GL_BACK);
  44. camera->setReadBuffer(GL_BACK);
  45. camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
  46. view->setSceneData( scene );
  47. view->addEventHandler( new osgViewer::StatsHandler );
  48. view->setCameraManipulator( new osgGA::MultiTouchTrackballManipulator );
  49. gw->setTouchEventsEnabled( true );
  50. return gw->getGLWidget();
  51. }
  52. osgQt::GraphicsWindowQt* createGraphicsWindow( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
  53. {
  54. osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
  55. osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
  56. traits->windowName = name;
  57. traits->windowDecoration = windowDecoration;
  58. traits->x = x;
  59. traits->y = y;
  60. traits->width = w;
  61. traits->height = h;
  62. traits->doubleBuffer = true;
  63. traits->alpha = ds->getMinimumNumAlphaBits();
  64. traits->stencil = ds->getMinimumNumStencilBits();
  65. traits->sampleBuffers = ds->getMultiSamples();
  66. traits->samples = ds->getNumMultiSamples();
  67. return new osgQt::GraphicsWindowQt(traits.get());
  68. }
  69. // virtual void paintEvent( QPaintEvent* /*event*/ )
  70. // { frame(); }
  71. //定时器事件
  72. void timerEvent(QTimerEvent* )
  73. {
  74. frame();
  75. }
  76. //启动定时器绘制
  77. void show()
  78. {
  79. QWidget::show();
  80. _timerID = startTimer(10);
  81. }
  82. protected:
  83. //QTimer _timer;
  84. int _timerID; //定时器ID
  85. };
  86. int main( int argc, char** argv )
  87. {
  88. osg::ArgumentParser arguments(&argc, argv);
  89. #if QT_VERSION >= 0x050000
  90. // Qt5 is currently crashing and reporting "Cannot make QOpenGLContext current in a different thread" when the viewer is run multi-threaded, this is regression from Qt4
  91. osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::ViewerBase::SingleThreaded;
  92. #else
  93. osgViewer::ViewerBase::ThreadingModel threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
  94. #endif
  95. while (arguments.read("--SingleThreaded")) threadingModel = osgViewer::ViewerBase::SingleThreaded;
  96. while (arguments.read("--CullDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullDrawThreadPerContext;
  97. while (arguments.read("--DrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::DrawThreadPerContext;
  98. while (arguments.read("--CullThreadPerCameraDrawThreadPerContext")) threadingModel = osgViewer::ViewerBase::CullThreadPerCameraDrawThreadPerContext;
  99. #if QT_VERSION >= 0x040800
  100. // Required for multithreaded QGLWidget on Linux/X11, see http://blog.qt.io/blog/2011/06/03/threaded-opengl-in-4-8/
  101. if (threadingModel != osgViewer::ViewerBase::SingleThreaded)
  102. QApplication::setAttribute(Qt::AA_X11InitThreads);
  103. #endif
  104. QApplication app(argc, argv);
  105. ViewerWidget* viewWidget = new ViewerWidget(0, Qt::Widget, threadingModel);
  106. viewWidget->setGeometry( 100, 100, 800, 600 );
  107. viewWidget->show();
  108. return app.exec();
  109. }

2) 多线程问题

在OSG中提供了诸如CullDrawThreadPerContext等多线程模式,但是在这里是没办法支持这些多线程模式的,只能支持单线程。在网上查阅了一些解决方案,但是最后都不是很完美,有空再把其解决方案写出来。

3) 其他

GraphicsWindowQt最终继承的还是QT中的QGLWidget类,提供OpenGL功能。而在新版本的QT中,这个类已经被废弃了,取而代之的是一个叫做QOpenGLWidget的类。所以这里面问题还是不少的,好在内容相对较少,以后有空可以自己改进。

原文链接:http://www.cnblogs.com/charlee44/p/11031757.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号