OSG在QT的中显示之一

58 篇文章 27 订阅

之前写过关于OSG的一些文章,主要是以基础为主,但是结合QT来显示还没有写过。

OSG和QT结合显示有单独的类:GraphicsWindowQt。如果编译过程中没有把这个类编译成库文件,可以直接在源码中拷贝出来,把头文件和cpp文件修改一下文件名就可以了,然后把修改好的.h和.cpp加入到工程中。

环境:VS2015+QT5.10+64位+OSG3.5.1

  1. 把环境设置好,头文件目录、链接文件目录在VS2015中配置好。
  2. 把链接文件添加上:
#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
  1. 设置两个变量
	QTimer* _timer;
	osgViewer::Viewer viewer;
  1. 初始化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();
}
  1. UI文件
    在这里插入图片描述
  2. 运行结果:
    在这里插入图片描述
    aaa
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wb175208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值