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();
}
运行效果如下