#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. codevs1358 棋盘游戏

    题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初始时棋子位于左上角,终点为右下角,棋盘上每个格子内有一个0到9的数字,每次棋子可以往右方或下方的相邻格子移动,求一条经 ...

  2. 2个比较经典的PHP加密解密函数分享

    项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 最常见的应用在用户登录以及一些AP ...

  3. hdu 2085 核反应堆

    看完题,想到用结构体存储高质点和低质点,然后打表存储<33的质点数量. #include<stdio.h> struct hilo { long long hi,lo; }; int ...

  4. jQuery中prop()函数控制多选框(全选,反选)

    今天看了jQuery手册,对prop()函数又多了一点认识,记忆力不好,记录下来. prop() : 获取匹配元素集中第一个元素的值 判断checkbox中的第一个是否被选中: $(":ch ...

  5. Linux下使用popen()执行shell命令

    转载 http://www.cnblogs.com/caosiyang/archive/2012/06/25/2560976.html 简单说一下popen()函数 函数定义 #include < ...

  6. Nginx Http框架的理解

    Nginx Http框架的理解 HTTP框架是Nginx基础框架的一部分,Nginx的其它底层框架如master-worker进程模型.event模块.mail 模块等. HTTP框架代码主要有2个模 ...

  7. 在CentOS 7上利用systemctl添加自定义系统服务

    每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install],具体内容如下: [Unit]Description=*****After=network.ta ...

  8. C++_Eigen函数库用法笔记——Block Operations

    Using block operations rvalue, i.e. it was only read from lvalues, i.e. you can assign to a block Co ...

  9. 使用 GDB 调试多进程程序

    使用 GDB 调试多进程程序 GDB 是 linux 系统上常用的调试工具,本文介绍了使用 GDB 调试多进程程序的几种方法,并对各种方法进行比较. 3 评论 田 强 (tianq@cn.ibm.co ...

  10. OpenXML_导入Excel到数据库(转)

    (1).实现功能:通过前台选择.xlsx文件的Excel,将其文件转化为DataTable和List集合 (2).开发环境:Window7旗舰版+vs2013+Mvc4.0 (2).在使用中需要用到的 ...