OsgEarth开发——加载模型文件并设置星空

OsgEarth开发难度比较大,但是再掌握了它的开发流程之后,往往会事半功倍。本文参照示例建立最简单的OE开发框架,首先加载EARTH文件,然后加如星空的效果,具体代码如下所示。开发工具为VS2015,对应版本为OSG:3.4,OSGEARTH:2.8。

#include <vector>
#include <iostream>
#include <fstream>
#include <cstring>
#include <time.h>
#include <Windows.h>

#include <osgEarth/MapNode>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/ExampleResources>

#include <osgViewer/Viewer>
#include <osgDB/WriteFile>

using namespace std;

#ifdef _DEBUG
#pragma comment(lib,"OpenThreadsd.lib")
#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgEarthd.lib")
#pragma comment(lib,"osgEarthUtild.lib")
#else
#pragma comment(lib,"OpenThreads.lib")
#pragma comment(lib,"osg.lib")
#pragma comment(lib,"osgDB.lib")
#pragma comment(lib,"osgGA.lib")
#pragma comment(lib,"osgViewer.lib")
#pragma comment(lib,"osgEarth.lib")
#pragma comment(lib,"osgEarthUtil.lib")
#endif

using namespace osgEarth;
using namespace osgEarth::Util;

int main(int argc, char* argv[])
{
	osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
	//定义根节点
	//osg::Group* root = new osg::Group();
	osg::ref_ptr<osg::Group> root = new osg::Group();	//智能指针方式
	osg::ref_ptr<osg::Node> earthNode = osgDB::readNodeFile("gdal_tiff.earth");
	if (!earthNode)
	{
		OE_NOTICE << "Unable to load earth model" << std::endl;
		return 1;
	}
	root->addChild(earthNode);
	//查询地图节点
	osgEarth::MapNode* mapNode = osgEarth::MapNode::findMapNode(earthNode);
	if (!mapNode)
	{
		OE_NOTICE << "Could not find MapNode " << std::endl;
		return 1;
	}
	//添加到场景
	viewer->setSceneData(root.get());
	viewer->realize();
	// 设置时间;
	osgEarth::DateTime dateTime(2019, 5, 8, 9);
	osgEarth::Util::Ephemeris* ephemeris = new osgEarth::Util::Ephemeris;

	osgEarth::Util::SkyNode* m_pSkyNode = osgEarth::Util::SkyNode::create(mapNode);
	m_pSkyNode->setName("SkyNode");
	m_pSkyNode->setEphemeris(ephemeris);
	m_pSkyNode->setDateTime(dateTime);
	m_pSkyNode->attach(viewer, 0);
	m_pSkyNode->setLighting(true);
	m_pSkyNode->addChild(mapNode);
	root->addChild(m_pSkyNode);

	return viewer->run();

}

运行效果如下

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值