#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. 洛谷P2024 食物链

    挺神奇 题目描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B 吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的一种 ...

  2. hihocoder #1270 建造基地

    传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在遥远的未来,小Hi成为了地球联邦外空间联合开发工作组的一员,前往一颗新发现的星球开发当地的重金属资源. 为了能够 ...

  3. Notions of Flow Networks and Flows

    这篇随笔是对算法导论(Introduction to Algorithms, 3rd. Ed.)第26章 Maximum Flow的摘录. ------------------------------ ...

  4. iOS开源项目汇总

    扫描wifi信息: http://code.google.com/p/uwecaugmentedrealityproject/ http://code.google.com/p/iphone-wire ...

  5. GNUPLOT画图工具

    http://blog.csdn.net/codingkid/article/details/7211492 不得不说这个工具实在是太强大了. 1.首先命令简单,不会有那么多的语法问题. 2.其次画图 ...

  6. tp三大自动

    ThinkPHP三大自动 (2012-03-21 10:48:56) 转载▼ 标签: thinkphp 三大自动 自动验证 自动完成 自动填充 自动映射 字段映射 杂谈 分类: php 一.自动验证 ...

  7. 批量删除亚马逊kindle云端文档

    首先鄙视亚马逊的不负责任,kindle的云端管理系统犹如一坨狗屎,根本没有考虑的任何用户体验,只能一个一个删除不说,删除后又回到第一页...翻页也没有输入页码的地方,如果在第100页删除文档后,又回到 ...

  8. CruiseControl.NET学习总结(转载)

    前些日子,总结了一个NAnt的学习总结.后来就放下了,松散了一阵子.CruiseControl.NET(以下称CC.NET),是我在学习完NAnt以后才开始看的,当时学起来就是在网上疯狂的找资料.现在 ...

  9. C# 三种实现抖屏的方式

    //int a = -2; //this.BringToFront(); //for (int i = 0; i < 20; i++) //{ // a = -a; // this.Locati ...

  10. [Angularjs]angular ng-repeat与js特效加载先后导致的问题

    写在前面 最近在项目中遇到这样的一个前端的bug,在ng-repeat中绑定的图片,有一个晃动的特效,在手机端浏览的时候,图片有时候会正常展示,有时就展示不出来.当时猜测是因为angularjs与特效 ...