之前写过关于OSG的一些文章,主要是以基础为主,但是结合QT来显示还没有写过。
OSG和QT结合显示有单独的类:GraphicsWindowQt。如果编译过程中没有把这个类编译成库文件,可以直接在源码中拷贝出来,把头文件和cpp文件修改一下文件名就可以了,然后把修改好的.h和.cpp加入到工程中。
环境:VS2015+QT5.10+64位+OSG3.5.1
- 把环境设置好,头文件目录、链接文件目录在VS2015中配置好。
- 把链接文件添加上:
#pragma once
#ifdef _DEBUG
#pragma comment(lib, "osgd.lib")
#pragma comment(lib, "osgDBd.lib")
#pragma comment(lib, "osgFXd.lib")
#pragma comment(lib, "osgViewerd.lib")
#pragma comment(lib, "OpenThreadsd.lib")
#pragma comment(lib, "osgGAd.lib")
#pragma comment(lib, "osgUtild.lib")
#pragma comment(lib, "osgTextd.lib")
#pragma comment(lib, "osgWidgetd.lib")
#pragma comment(lib, "osgShadowd.lib")
#pragma comment(lib, "osgAnimationd.lib")
#pragma comment(lib, "osgParticled.lib")
#pragma comment(lib, "osgManipulatord.lib")
#pragma comment(lib, "osgSim.lib")
#pragma comment(lib, "osgEarthd.lib")
#pragma comment(lib, "osgEarthUtild.lib")
#pragma comment(lib, "osgEarthFeaturesd.lib")
#pragma comment(lib, "osgEarthSymbologyd.lib")
#pragma comment(lib, "CEGUIBase_d.lib")
#pragma comment(lib, "CEGUIOpenGLRenderer_d.lib")
#pragma comment(lib, "CEGUILuaScriptModule_d.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#else
#pragma comment(lib, "osg.lib")
#pragma comment(lib, "osgDB.lib")
#pragma comment(lib, "osgFX.lib")
#pragma comment(lib, "osgViewer.lib")
#pragma comment(lib, "OpenThreads.lib")
#pragma comment(lib, "osgGA.lib")
#pragma comment(lib, "osgUtil.lib")
#pragma comment(lib, "osgText.lib")
#pragma comment(lib, "osgWidget.lib")
#pragma comment(lib, "osgShadow.lib")
#pragma comment(lib, "osgAnimation.lib")
#pragma comment(lib, "osgParticle.lib")
#pragma comment(lib, "osgManipulator.lib")
#pragma comment(lib, "osgSim.lib")
#pragma comment(lib, "osgEarth.lib")
#pragma comment(lib, "osgEarthUtil.lib")
#pragma comment(lib, "osgEarthFeatures.lib")
#pragma comment(lib, "osgEarthSymbology.lib")
#pragma comment(lib, "osgEarthAnnotation.lib")
#pragma comment(lib, "osgEarthSplat.lib")
#endif
- 设置两个变量
QTimer* _timer;
osgViewer::Viewer viewer;
- 初始化OSG场景及其相关
void OSG::initOSG() {
//设置模型
osg::ref_ptr<osg::Group> root = new osg::Group;
osg::ref_ptr<osg::Node> child = osgDB::readNodeFile("cow.osg");
root->addChild(child);
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowDecoration = false;//声明是否显示窗口的描述
traits->x = 50;
traits->y = 50;
traits->width = 800;
traits->height = 600;
traits->doubleBuffer = true;//创建图形窗口是否使用双缓存
//设置照相机
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext(new GraphicsWindowQt(traits.get()));
camera->setClearColor(osg::Vec4(0.2, 0.2, 0.6, 1.0));
camera->setViewport(new osg::Viewport(0, 0, 800, 600));
camera->setProjectionMatrixAsPerspective(30.0f, 800.0f/600.0f, 1.0f, 10000.0f);
//设置渲染器
viewer.setCamera(camera);
viewer.setSceneData(root);
viewer.setCameraManipulator(new osgGA::TrackballManipulator);
viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded);//创建为单线程
viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
viewer.addEventHandler(new osgViewer::WindowSizeHandler);
viewer.addEventHandler(new osgViewer::StatsHandler);
GraphicsWindowQt* gw = dynamic_cast<GraphicsWindowQt*> (camera->getGraphicsContext());
ui.horizontalLayoutOSG->addWidget(gw->getGLWidget());
_timer = new QTimer;
connect(_timer, SIGNAL(timeout()), this, SLOT(slotUpdate()));
_timer->start(25);
}
void OSG::slotUpdate() {
viewer.frame();
}
- UI文件
- 运行结果:
aaa