#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>
#include <osgGA/TrackballManipulator>
#include <osgEarth/Map>
#include <osgEarth/MapNode>
#include <osgEarthDrivers/tms/TMSOptions>
#include <osgEarthDrivers/model_feature_geom/FeatureGeomModelOptions>
#include <osgEarthDrivers/feature_ogr/OGRFeatureOptions>
#include <osgEarthDrivers/cache_filesystem/FileSystemCache>
#include <osgEarthUtil/EarthManipulator>
#include <osgEarthUtil/GeodeticGraticule>
#include <osgEarthUtil/LatLongFormatter>
#include <osgEarthUtil/Controls>
#include <osgEarthUtil/MouseCoordsTool>
#include <osgEarthUtil/AutoClipPlaneHandler>
#include <osg/PositionAttitudeTransform>
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile>
//初始化影像、地形
void initImageAndElevation(osgEarth::Map* map)
{
 //影像
 osgEarth::Drivers::TMSOptions imgOption;
 imgOption.url()="D:/CacheData/OUTDATA/JiLinImage/tms.xml";
 //imgOption.url()="http://readymap.org/readymap/tiles/1.0.0/7/";
 map->addImageLayer( new osgEarth::ImageLayer( "image", imgOption ) );
 //高程
 osgEarth::Drivers::TMSOptions elvOption;
 elvOption.url()="D:/CacheData/OUTDATA/JiLinElevation/tms.xml";
 //elvOption.url()="http://readymap.org/readymap/tiles/1.0.0/9/";
 map->addElevationLayer( new osgEarth::ElevationLayer( "elevation", elvOption ) );
}
//初始化省界国界
void initBoundaries(osgEarth::Map* map)
{
 osgEarth::Drivers::FeatureGeomModelOptions worldBoundaries;
 osgEarth::Drivers::OGRFeatureOptions ogrOptions;
 ogrOptions.url()="world.shp";
 worldBoundaries.compilerOptions()=ogrOptions;
 //worldBoundaries.styles().
 map->addModelLayer(new osgEarth::ModelLayer("world", worldBoundaries));
}
//初始化事件处理器
void initEventHandler(osgViewer::Viewer* viewer,osgEarth::MapNode* mapNode)
{
 //鼠标位置信息显示
 osgEarth::Util::Formatter* formatter = new osgEarth::Util::LatLongFormatter();
 osgEarth::Util::LabelControl* readout = new osgEarth::Util::LabelControl();
 osgEarth::Util::ControlCanvas::get( viewer, true )->addControl( readout );
 osgEarth::Util::MouseCoordsTool* tool = new osgEarth::Util::MouseCoordsTool( mapNode );
 tool->addCallback( new osgEarth::Util::MouseCoordsLabelCallback(readout, formatter) );
 viewer->addEventHandler( tool );
 // add the state manipulator
    viewer->addEventHandler( new osgGA::StateSetManipulator(viewer->getCamera()->getOrCreateStateSet()) );
 // add the thread model handler
    viewer->addEventHandler(new osgViewer::ThreadingHandler);
 // add the window size toggle handler
    viewer->addEventHandler(new osgViewer::WindowSizeHandler);
 // add the stats handler
    viewer->addEventHandler(new osgViewer::StatsHandler);
 // add the record camera path handler
    viewer->addEventHandler(new osgViewer::RecordCameraPathHandler);
 // add the LOD Scale handler
    viewer->addEventHandler(new osgViewer::LODScaleHandler);
 // add the screen capture handler
    viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);
}
//初始化操纵器
void initManipulator(osgViewer::Viewer* viewer,osgEarth::Map* map)
{
 //操纵器
    osgEarth::Util::EarthManipulator* em=new osgEarth::Util::EarthManipulator();
    //赤道半径
    double equatorRadius=map->getSRS()->getEllipsoid()->getRadiusEquator();//6378137.0
 //初始视点正对中国北京
    em->setHomeViewpoint(osgEarth::Util::Viewpoint(116.3,39.9,0,0,-90,equatorRadius*4));
    viewer->setCameraManipulator(em);
 //定位吉林
    em->setViewpoint(osgEarth::Util::Viewpoint(126,43,0,0,-90,5e4), 5);//5s
}
//初始化其他杂项
void initOther(osgViewer::Viewer* viewer,osgEarth::MapNode* mapNode)
{
 //反锯齿
 osg::DisplaySettings::instance()->setNumMultiSamples( 4 );
 //设置最大PagedLOD节点数目
 //viewer->getDatabasePager()->setTargetMaximumNumberOfPageLOD(10);
    //近地面自动裁剪AutoClipPlaneCullCallback
    viewer->getCamera()->addCullCallback( new osgEarth::Util::AutoClipPlaneCullCallback(mapNode) );
 //绘制经纬度网格
 //osgEarth::Util::GeodeticGraticule* gr = new osgEarth::Util::GeodeticGraticule( mapNode );
 //root->addChild(gr);
}

void main(){
 osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
 //osgViewer::Viewer* viewer = new osgViewer::Viewer;
 osg::Group* root = new osg::Group();
 osg::Node* tankNode = NULL;
 osg::Vec3 tankPosit;
 osg::PositionAttitudeTransform* tankXform;
 //缓存
 osgEarth::Drivers::FileSystemCacheOptions cacheOptions;
 cacheOptions.rootPath() = "./cache";
 osgEarth::MapOptions mapOptions;
 mapOptions.cache() = cacheOptions;
 //osgEarth::Map* map = new osgEarth::Map(mapOptions);
 //osgEarth::MapNode* mapNode = new osgEarth::MapNode( map );
 osg::Node* configEarth = osgDB::readNodeFile("config.earth");
 osgEarth::MapNode* mapNode = osgEarth::MapNode::findMapNode( configEarth );
 osgEarth::Map* map = mapNode->getMap();
 //关闭光照
    mapNode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
 viewer->setSceneData( root );
 root->addChild(mapNode);
 //读取模型
 tankNode = osgDB::readNodeFile("Models/t72-tank/t72-tank_des.flt");
 tankXform = new osg::PositionAttitudeTransform();
 root->addChild(tankXform);
 tankXform->addChild(tankNode);
 tankPosit.set(5,0,0);
 tankXform->setPosition( tankPosit );
 viewer->setCameraManipulator(new osgGA::TrackballManipulator());

//地形影像
 //initImageAndElevation(map);

//矢量
 //initBoundaries(map);

//操纵器
 initManipulator(viewer, map);

//事件处理(键盘鼠标)
 initEventHandler(viewer, mapNode);

//其他
    initOther(viewer, mapNode);

viewer->realize();

while( !viewer->done() )
 {
  viewer->frame();
 }
}

osgEarth例子的更多相关文章

  1. [OSG][osgEarth]osgEarth例子程序简介

    1.osgearth_graticule:生成经纬线. 2.osgearth_annotation:各类标注(点.线.面.模型.文本等). 3.osgearth_city:加载一个城市三维模型,可以浏 ...

  2. [原][OSG][osgEarth]osgEarth例子程序简介

    1.osgearth_graticule:生成经纬线. 2.osgearth_annotation:各类标注(点.线.面.模型.文本等). 3.osgearth_city:加载一个城市三维模型,可以浏 ...

  3. osgearth各个例子功能概述

    osgearth各个例子功能概述 转自:http://blog.csdn.net/wl198302/article/details/21177309 最近在学习osgearth,对其还不是很理解,有些 ...

  4. osgEarth的agglite插件使用例子feature_rasterize.earth

    <!-- osgEarth Sample Demonstrates use of the "agglite" feature rasterization driver. -- ...

  5. osgearth_city例子总结

    osgearth_city例子总结 转自:http://blog.csdn.net/taor1/article/details/8242480 int main(int argc, char** ar ...

  6. osgEarth基础入门

    osgEarth基础入门 2015年3月21日 16:19 osgEarth是基于三维引擎osg开发的三维数字地球引擎库,在osg基础上实现了瓦片调度插件,可选的四叉树调度插件,更多的地理数据加载插件 ...

  7. OSGEARTH三维地形开源项目

    第一章   OSGEarth介绍 第二章   OSGEarth编译环境配置 OSGEarth的编译环境配置随着版本的不同.运行平台的不同,也有很大的差异.本章主要以Windows XP SP3(x86 ...

  8. osgEarth基础入门(转载)

    osgEarth基础入门 osgEarth是基于三维引擎osg开发的三维数字地球引擎库,在osg基础上实现了瓦片调度插件,可选的四叉树调度插件,更多的地理数据加载插件(包括GDAL,ogr,WMS,T ...

  9. OSG和osgearth显示中文(转载)

    osgEarth支持中文过程详解 OSG和osgearth显示中文 一.知识储备   要想很好的理解和解决这个问题,首先要了解什么是多字节和宽字节.说实话我之前也知道这两个字节到底有什么区别,只是简单 ...

随机推荐

  1. 如何使用lessc编译.less文件

    LESS :一种动态样式语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, Firefox) ...

  2. Tomcat Can't load AMD 64-bit .dll on a IA 32

    Java.lang.UnsatisfiedLinkError: C:\apache\apache-tomcat-7.0.14\bin\tcnative-1.dll: Can't load AMD 64 ...

  3. 转:Linux集群-----HA浅谈

    通过特殊的软件将若干服务器连接在一起并提供故障切换功能的实体我们称之为高可用集群.可用性是指系统的uptime,在7x24x365的工作环境中,99%的可用性指在一年中可以有87小时36分钟的DOWN ...

  4. Ubuntu学习总结-03 安装软件 & 技巧

    1 UBuntu 安装 Googole Chrome 首先下载软件 wget https://dl.google.com/linux/direct/google-chrome-stable_curre ...

  5. 怎样分析java线程堆栈日志

    注: 该文章的原文是由 Tae Jin Gu 编写,原文地址为 How to Analyze Java Thread Dumps 当有障碍,或者是一个基于 JAVA 的 WEB 应用运行的比预期慢的时 ...

  6. 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)

    在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...

  7. virtualbox 不能为虚拟电脑打开一个新任务/VT-x features locked or unavailable in MSR.

    确保了主机的BIOS中开启了Intel Virtual Technology,虚拟机配置中勾选了“启用VT-x/AMD-V”. 这是因为CPU不支持VT-X技术或者VT-X技术被锁定. 如果不打开虚拟 ...

  8. commons-logging和Log4j 日志管理/log4j.properties配置详解

    commons-logging和Log4j 日志管理 (zz) 什么要用日志(Log)? 这个……就不必说了吧. 为什么不用System.out.println()? 功能太弱:不易于控制.如果暂时不 ...

  9. 反射中 GetCustomAttributes

    public abstract object[] GetCustomAttributes(bool inherit); 这是GetCustomAttributes方法的一个重载,参数为bool类型返回 ...

  10. sql导入默认用户解决杰奇cms无法登陆管理员账户问题

    前些天下载杰奇cms来体验一下,从a5下载的杰奇1.8版,不是默认的安装程序,上传文件,手工导入sql数据库,修改了config配置文件,很快就完工了.前台可以展现,除了有些乱码显示之外,想要修改相关 ...