日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

osgEarth示例分析——osgearth_manip

發布時間:2024/3/13 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 osgEarth示例分析——osgearth_manip 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

本示例主要演示osgEarth的事件處理的用法,內容比較多,這部分功能也很重要。

輸入命令依然采用china-simple.earth的示例,加上了模型,但是模型并沒有看到,可能是因為模型沒有放大太小的原因。在代碼中設置了不加載模型的話,會繪制兩個半圓。

// 加載模型,但是看不到,估計是模型太小,需要放大 osgearth_manipd.exe earth_image/china-simple.earth --model axes.osgt// 并沒有加載模型 osgearth_manipd.exe earth_image/china-simple.earth --model modelToLoad// 不加載模型,會默認創建一個半球,距離太遠的話,半球也看不到 osgearth_manipd.exe earth_image/china-simple.earth

執行效果

按下W鍵,開啟坐標,也就是黃色的兩條線,交點始終跟隨 Thing 1 節點。

按鍵說明

左上角操作說明
*左鍵按住左鍵拖動地球*u看不出來,沒啥變化
*中鍵按住中鍵可以旋轉地球*o切換操作器,地球會有大小變化
*右鍵按住右鍵前后移動,縮放地球*8視角綁定thing 1,也可以說跟蹤
*

右鍵/左鍵

雙擊

越來越遠/越來越近*9視角綁定thing 2,也可以說跟蹤
*滾輪滾動視角遠近控制*t地球三種轉動狀態的切換
*arrows

shift+上/下/左/右鍵,

好像沒啥作用

*b解除綁定 '8' ‘9’,,或者說解除跟蹤
*shift+左鍵看不出來,沒啥變化*a看不出來,沒啥變化
*q看不出來,沒啥變化
*k看不出來,沒啥變化
*L看不出來,沒啥變化
*j定位到可以看見一組點的視點
*W繪制兩條線,交點始終定位在thing 1上,屏幕坐標系

[注] 必須在英文下輸入,如果沒有注意,在中文狀態下輸入字符,地球會被卡住。此時只能退出重新運行程序。

thing1 thing 2,這兩個文本是移動的。

代碼分析

#include <string>#include <osg/Notify> #include <osg/Timer> #include <osg/ShapeDrawable> #include <osg/Depth> #include <osg/PositionAttitudeTransform> #include <osgGA/StateSetManipulator> #include <osgGA/GUIEventHandler> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgEarth/GeoMath> #include <osgEarth/GeoTransform> #include <osgEarth/MapNode> #include <osgEarth/TerrainEngineNode> #include <osgEarth/Viewpoint> #include <osgEarthUtil/EarthManipulator> #include <osgEarthUtil/Controls> #include <osgEarthUtil/ExampleResources> #include <osgEarthUtil/LogarithmicDepthBuffer> #include <osgEarthUtil/ViewFitter> #include <osgEarthAnnotation/AnnotationUtils> #include <osgEarthAnnotation/LabelNode> #include <osgEarthSymbology/Style> #include <osgEarth/ScreenSpaceLayout>using namespace osgEarth::Util; using namespace osgEarth::Util::Controls; using namespace osgEarth::Annotation;#define D2R (osg::PI/180.0) #define R2D (180.0/osg::PI)namespace {/*** Tether callback test.是否綁定*/struct TetherCB : public EarthManipulator::TetherCallback{void operator()(osg::Node* node){if ( node ) {OE_WARN << "Tether on\n";}else {OE_WARN << "Tether off\n";}}};/*** Builds our help menu UI.創建左上角的說明文檔*/Container* createHelp( osgViewer::View* view ){const char* text[] ={"left mouse :", "pan","middle mouse :", "rotate","right mouse :", "continuous zoom","double-click :", "zoom to point","scroll wheel :", "zoom in/out","arrows :", "pan",//"1-6 :", "fly to preset viewpoints","shift-left-mouse :", "locked pan","u :", "toggle azimuth lock","o :", "toggle perspective/ortho","8 :", "Tether to thing 1","9 :", "Tether to thing 2","t :", "cycle tethermode","b :", "break tether","a :", "toggle viewpoint arcing","q :", "toggle throwing","k :", "toggle collision","L :", "toggle log depth buffer"//"W :", "坐標系", // 源代碼并沒有這兩個提示//"j :", "定位到指定點組的視點",};Grid* g = new Grid();// 創建網格unsigned i, c, r;std::cout << sizeof(text) << std::endl; // 存儲所有指針占位多少std::cout << sizeof(text[0]) << std::endl; // 每個指針占位多少for( i=0; i<sizeof(text)/sizeof(text[0]); ++i ){c = i % 2;r = i / 2;g->setControl( c, r, new LabelControl(text[i]) );// 兩列c,i行r,顯示label控件}VBox* v = new VBox();v->addControl( g );// 將網格控件放入VBox控件中return v;}/*** Some preset viewpoints to show off the setViewpoint function.*/static Viewpoint VPs[] = {Viewpoint( "Africa", 0.0, 0.0, 0.0, 0.0, -90.0, 10e6 ),Viewpoint( "California", -121.0, 34.0, 0.0, 0.0, -90.0, 6e6 ),Viewpoint( "Europe", 0.0, 45.0, 0.0, 0.0, -90.0, 4e6 ),Viewpoint( "Washington DC", -77.0, 38.0, 0.0, 0.0, -90.0, 1e6 ),Viewpoint( "Australia", 135.0, -20.0, 0.0, 0.0, -90.0, 2e6 ),Viewpoint( "Boston", -71.096936, 42.332771, 0, 0.0, -90, 1e5 )};/*** Handler that demonstrates the "viewpoint" functionality in * osgEarthUtil::EarthManipulator. Press a number key to fly to a viewpoint.*/struct FlyToViewpointHandler : public osgGA::GUIEventHandler {FlyToViewpointHandler( EarthManipulator* manip ) : _manip(manip) { }bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa ){if ( ea.getEventType() == ea.KEYDOWN && ea.getKey() >= '1' && ea.getKey() <= '6' ){_manip->setViewpoint( VPs[ea.getKey()-'1'], 4.0 );// 根據設置好的位置,123456定位不同的地點aa.requestRedraw();}return false;}osg::observer_ptr<EarthManipulator> _manip;};/*** Toggles the logarithmic depth buffer切換對數深度緩沖區*/struct ToggleLDB : public osgGA::GUIEventHandler{ToggleLDB(char key) : _key(key), _installed(false) { }// key=Lbool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){if ( !_installed ){_nfratio = aa.asView()->getCamera()->getNearFarRatio();// 獲取到近/遠率_ldb.install(aa.asView()->getCamera());aa.asView()->getCamera()->setNearFarRatio(0.00001);}else{_ldb.uninstall(aa.asView()->getCamera());aa.asView()->getCamera()->setNearFarRatio(_nfratio);}_installed = !_installed;// 每次切換return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Toggle LDB"));}char _key;float _nfratio;bool _installed;osgEarth::Util::LogarithmicDepthBuffer _ldb;};/*** Toggles screen space layout on the sismulated objects 切換模擬對象的屏幕空間布局*/struct ToggleSSL : public osgGA::GUIEventHandler{ToggleSSL(osg::Group* g, char key) : _group(g), _key(key), _installed(false) { }// key=)bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){_installed = !_installed;ScreenSpaceLayout::setDeclutteringEnabled(_installed);// 啟動或禁用清理return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Toggle SSL"));}char _key;osg::Group* _group;bool _installed;};/*** Handler to toggle "azimuth locking", which locks the camera's relative Azimuth* while panning. For example, it can maintain "north-up" as you pan around. The* caveat is that when azimuth is locked you cannot cross the poles.*/struct LockAzimuthHandler : public osgGA::GUIEventHandler // 鎖定/解鎖 方位角{LockAzimuthHandler(char key, EarthManipulator* manip) // 傳入key = u: _key(key), _manip(manip) { }bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){bool lockAzimuth = _manip->getSettings()->getLockAzimuthWhilePanning();std::cout << "按下 u" << std::endl;// 確實收到消息,但是在操作上并沒有看到所定方位角_manip->getSettings()->setLockAzimuthWhilePanning(!lockAzimuth);// 設置為當前狀態相反的狀態aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Toggle azimuth locking"));}char _key;osg::ref_ptr<EarthManipulator> _manip;};/*** Handler to toggle "viewpoint transition arcing", which causes the camera to "arc"* as it travels from one viewpoint to another.*/struct ToggleArcViewpointTransitionsHandler : public osgGA::GUIEventHandler{ToggleArcViewpointTransitionsHandler(char key, EarthManipulator* manip): _key(key), _manip(manip) { }// key=abool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){bool arc = _manip->getSettings()->getArcViewpointTransitions();std::cout << "按下 a" << std::endl;// 確實收到消息,但是沒看出什么變化_manip->getSettings()->setArcViewpointTransitions(!arc);aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Arc viewpoint transitions"));}char _key;osg::ref_ptr<EarthManipulator> _manip;};/*** Toggles the throwing feature.切換投擲功能。*/struct ToggleThrowingHandler : public osgGA::GUIEventHandler{ToggleThrowingHandler(char key, EarthManipulator* manip): _key(key), _manip(manip)// key=q{}bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){bool throwing = _manip->getSettings()->getThrowingEnabled();std::cout << "按下 q" << std::endl;// 確實收到消息,但是沒看出什么變化_manip->getSettings()->setThrowingEnabled( !throwing );aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Toggle throwing"));}char _key;osg::ref_ptr<EarthManipulator> _manip;};/*** Toggles the collision feature.切換碰撞功能。*/struct ToggleCollisionHandler : public osgGA::GUIEventHandler{ToggleCollisionHandler(char key, EarthManipulator* manip): _key(key), _manip(manip)// key=k{}bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){bool value = _manip->getSettings()->getTerrainAvoidanceEnabled();std::cout << "按下 k" << std::endl;// 確實收到消息,但是沒看出什么變化_manip->getSettings()->setTerrainAvoidanceEnabled( !value );aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Toggle terrain avoidance"));}char _key;osg::ref_ptr<EarthManipulator> _manip;};/*** Breaks a tether.三種狀態的切換*/struct CycleTetherMode : public osgGA::GUIEventHandler{CycleTetherMode(char key, EarthManipulator* manip): _key(key), _manip(manip) { }// key=tbool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){EarthManipulator::TetherMode mode = _manip->getSettings()->getTetherMode();if ( mode == _manip->TETHER_CENTER ) { _manip->getSettings()->setTetherMode( _manip->TETHER_CENTER_AND_HEADING );// 相機將跟隨節點,僅跟隨方向旋轉OE_NOTICE << "Tether mode = TETHER_CENTER_AND_HEADING\n";}else if ( mode == _manip->TETHER_CENTER_AND_HEADING ) {_manip->getSettings()->setTetherMode( _manip->TETHER_CENTER_AND_ROTATION ); // 攝影機將跟隨節點和節點所做的所有旋轉OE_NOTICE << "Tether mode = TETHER_CENTER_AND_ROTATION\n";}else {_manip->getSettings()->setTetherMode( _manip->TETHER_CENTER );// 攝影機將跟隨節點的中心。OE_NOTICE << "Tether mode = CENTER\n";}aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Cycle Tether Mode"));}char _key;osg::ref_ptr<EarthManipulator> _manip;};/*** Breaks a tether.解綁*/struct BreakTetherHandler : public osgGA::GUIEventHandler{BreakTetherHandler(char key, EarthManipulator* manip): _key(key), _manip(manip) { }//key=bbool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){_manip->clearViewpoint();// 解除所有操作器綁定,恢復自由狀態。thing1和 thing2 的狀態改變明顯aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Break a tether"));}char _key;osg::ref_ptr<EarthManipulator> _manip;};/*** Adjusts the position offset.調整位置偏移。*/struct SetPositionOffset : public osgGA::GUIEventHandler{SetPositionOffset(EarthManipulator* manip): _manip(manip) { }bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && (ea.getModKeyMask() & ea.MODKEY_SHIFT) ){Viewpoint oldvp = _manip->getViewpoint();double seconds = 0.5; // 時間秒。每次切換都需要0.5秒if ( ea.getKey() == ea.KEY_Left )// 左鍵{Viewpoint vp;vp.positionOffset() = oldvp.positionOffset().get() + osg::Vec3f(-1000,0,0);_manip->setViewpoint( vp, seconds );}else if ( ea.getKey() == ea.KEY_Right )// 右鍵{Viewpoint vp;vp.positionOffset() = oldvp.positionOffset().get() + osg::Vec3f(1000,0,0);_manip->setViewpoint( vp, seconds );}else if ( ea.getKey() == ea.KEY_Up )// 上鍵{Viewpoint vp;vp.positionOffset() = oldvp.positionOffset().get() + osg::Vec3f(0,0,1000);_manip->setViewpoint( vp, seconds );}else if ( ea.getKey() == ea.KEY_Down )// 下鍵{Viewpoint vp;vp.positionOffset() = oldvp.positionOffset().get() + osg::Vec3f(0,0,-1000);_manip->setViewpoint( vp, seconds );}aa.requestRedraw();return true;}return false;}osg::ref_ptr<EarthManipulator> _manip;};/*** Toggles perspective/ortho projection matrix. 切換投影矩陣,透視投影矩陣和正射投影矩陣*/struct ToggleProjMatrix : public osgGA::GUIEventHandler{ToggleProjMatrix(char key, EarthManipulator* manip): _key(key), _manip(manip)// ley=o{}bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){osg::Matrix proj = aa.asView()->getCamera()->getProjectionMatrix();if ( proj(3,3) == 0 ){OE_NOTICE << "Switching to orthographc.\n";proj.getPerspective(_vfov, _ar, _zn, _zf); // 獲取值aa.asView()->getCamera()->setProjectionMatrixAsOrtho(-1, 1, -1, 1, _zn, _zf);// 正射投影矩陣}else{OE_NOTICE << "Switching to perspective.\n";aa.asView()->getCamera()->setProjectionMatrixAsPerspective(_vfov, _ar, _zn, _zf);// 透視矩陣}aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Toggle projection matrix type"));}char _key;osg::ref_ptr<EarthManipulator> _manip;double _vfov, _ar, _zn, _zf;};// 定位到某組點的視點,一組點都可以看到struct FitViewToPoints : public osgGA::GUIEventHandler{std::vector<GeoPoint> _points;const SpatialReference* _mapSRS;FitViewToPoints(char key, EarthManipulator* manip, const SpatialReference* mapSRS): _key(key), _manip(manip), _mapSRS(mapSRS)// key=j{// Set up a list of control pointsconst SpatialReference* srs = SpatialReference::get("wgs84");_points.push_back(GeoPoint(srs, -120, 30, 0));_points.push_back(GeoPoint(srs, -100, 45, 0));}bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){// fitter 創建適合攝影機視圖平截頭體的視點,以盡可能緊密地包圍一組地理空間點。ViewFitter fitter(_mapSRS, aa.asView()->getCamera());fitter.setBuffer( 100000.0 );Viewpoint vp;if (fitter.createViewpoint(_points, vp))// 根據_points重新生成一個vp視點{_manip->setViewpoint(vp);// 定位到vp視點aa.requestRedraw();}return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("FitViewToPoints"));}char _key;osg::ref_ptr<EarthManipulator> _manip;};/*** A simple simulator that moves an object around the Earth. We use this to* demonstrate/test tethering.*/// 一個簡單的模擬器,可以使物體繞地球運動。用來演示跟蹤thingstruct Simulator : public osgGA::GUIEventHandler{Simulator( osg::Group* root, EarthManipulator* manip, MapNode* mapnode, osg::Node* model, const char* name, char key): _manip(manip), _mapnode(mapnode), _model(model), _name(name), _key(key) // key=8 或 9{if ( !model ){ // 模型不存在,則創建一個半球,半徑和顏色_model = AnnotationUtils::createHemisphere(250.0, osg::Vec4(1,.7,.4,1));std::cout << "no model" << std::endl;}_geo = new GeoPositionNode();_geo->getPositionAttitudeTransform()->addChild(_model);Style style;TextSymbol* text = style.getOrCreate<TextSymbol>();text->size() = 32.0f;text->declutter() = false;text->pixelOffset()->set(50, 50);// 文本偏移像素點_label = new LabelNode(_name, style);// _name 即要顯示的名稱_label->setDynamic( true );// 因為文本移動,所以此處設置為true_label->setHorizonCulling(false);// 不需要裁剪_geo->getPositionAttitudeTransform()->addChild(_label);mapnode->addChild(_geo.get());}bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if ( ea.getEventType() == ea.FRAME )// 此語段為標簽添加了動畫{double t0 = osg::Timer::instance()->time_s();// 儒略日時間double t = fmod( t0, 6000.0 ) / 6000.0;// t0%6000/6000, 將數字控制在[0,1)范圍內GeoPoint p = _start.interpolate(_end, t);// 差值,在_end和t之間// GeoMath::bearing 以弧度計算從一個點到下一個點的初始方位double bearing = GeoMath::bearing(_start.y(), _start.x(), p.y(), p.x());float a = sin(t0*0.2);// a值沒有被用float pitch = 0.0;_geo->setPosition(p);_geo->setLocalRotation(osg::Quat(pitch, osg::Vec3d(1, 0, 0)) *osg::Quat(bearing, osg::Vec3d(0, 0, -1)));}else if ( ea.getEventType() == ea.KEYDOWN ){if ( ea.getKey() == _key )// 8 或 9{ Viewpoint vp = _manip->getViewpoint();//vp.setNode( _pat.get() );vp.setNode(_model);vp.range() = 25000.0;vp.pitch() = -45.0;_manip->setViewpoint(vp, 2.0);// 2秒內飛到指定位置}return true;}return false;}std::string _name;char _key;MapNode* _mapnode;EarthManipulator* _manip;GeoPoint _start, _end;LabelNode* _label;osg::Node* _model;float _heading;float _pitch;osg::ref_ptr<GeoPositionNode> _geo;};/*** Place an X at the sim entity position, in screen space.* The point of this is to test the EarthManipulator::UpdateCameraCallback* which provides a frame-synched camera matrix (post-update traversal)*/// 創建一個十字坐標struct CalculateWindowCoords : public osgGA::GUIEventHandler{CalculateWindowCoords(char key, EarthManipulator* manip, Simulator* sim): _key(key), _active(false), _sim(sim), _xform(0L){//nop}void onUpdateCamera(const osg::Camera* cam){if (_active){if (!_xform){osg::Geometry* geom = new osg::Geometry();osg::Vec3Array* verts = new osg::Vec3Array();verts->push_back(osg::Vec3(-10000, 0, 0));verts->push_back(osg::Vec3( 10000, 0, 0));verts->push_back(osg::Vec3( 0, -10000, 0));verts->push_back(osg::Vec3( 0, 10000, 0));verts->push_back(osg::Vec3( 0, 0, -10000));verts->push_back(osg::Vec3( 0, 0, 10000));geom->setVertexArray(verts);osg::Vec4Array* colors = new osg::Vec4Array();colors->push_back(osg::Vec4(1, 1, 0, 1));// 黃色線colors->setBinding(colors->BIND_OVERALL);geom->setColorArray(colors);geom->addPrimitiveSet(new osg::DrawArrays(GL_LINES, 0, 6));// 6個點,繪制3條線,但屏幕上僅能看出2條線,水平和垂直geom->setCullingActive(false);// 不裁剪geom->getOrCreateStateSet()->setAttributeAndModes(new osg::Depth(osg::Depth::ALWAYS, 0, 1, true), 1);//深度設置_xform = new osg::MatrixTransform();_xform->addChild(geom);osg::View* view = const_cast<osg::View*>(cam->getView());ControlCanvas::getOrCreate(view)->addChild(_xform);}/// 坐標變換 V_clip=M_projection?M_view?M_model?V_local 裁剪坐標=透視矩陣*觀察矩陣*模型矩陣*本地坐標——來自OpenGLGeoPoint p = _sim->_geo->getPosition();// 獲取thing1的坐標點osg::Vec3d world;p.toWorld(world);// 轉化為世界坐標osg::Matrix worldToWindow =cam->getViewMatrix() *cam->getProjectionMatrix() *cam->getViewport()->computeWindowMatrix();// 世界坐標轉到窗口坐標osg::Vec3d win = world * worldToWindow;// 得到窗口坐標_xform->setMatrix(osg::Matrix::translate(win));}}bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa){if (ea.getEventType() == ea.KEYDOWN && ea.getKey() == _key){_active = !_active;aa.requestRedraw();return true;}return false;}void getUsage(osg::ApplicationUsage& usage) const{using namespace std;usage.addKeyboardMouseBinding(string(1, _key), string("Show Window Coords"));}osg::MatrixTransform* _xform;Simulator* _sim;bool _active;char _key;};// 更新相機回調struct CameraUpdater : public EarthManipulator::UpdateCameraCallback{CalculateWindowCoords* _calc;CameraUpdater(CalculateWindowCoords* calc) : _calc(calc) { }void onUpdateCamera(const osg::Camera* cam){_calc->onUpdateCamera(cam);// 當物體運動時,需要讓十字線跟隨物體運動}}; }int main(int argc, char** argv) {osg::ArgumentParser arguments(&argc,argv);if (arguments.read("--help") || argc==1){OE_WARN << "Usage: " << argv[0] << " [earthFile] [--model modelToLoad]"<< std::endl;return 0;}osgViewer::Viewer viewer(arguments);// install the programmable manipulator.安裝操作器EarthManipulator* manip = new EarthManipulator();viewer.setCameraManipulator( manip );// UI: 創建幫助說明控件,在左上角的位置。當鼠標移動到控件上之后,是無法點擊到地球上的,沒有鼠標穿透功能。Container* help = createHelp(&viewer);// MapNodeHelper 顯示幫助// load() 加載earth映射文件并處理所有內置示例命令行參數和XML外部變量(help)。osg::Node* earthNode = MapNodeHelper().load( arguments, &viewer, help );if (!earthNode){OE_WARN << "Unable to load earth model." << std::endl;return -1;}osg::Group* root = new osg::Group();root->addChild( earthNode );// 獲取mapNodeosgEarth::MapNode* mapNode = osgEarth::MapNode::findMapNode( earthNode );// user model?// 加載模型,這里傳入osg osgt格式的模型,并沒有顯示osg::ref_ptr<osg::Node> model;std::string modelFile;if (arguments.read("--model", modelFile))model = osgDB::readRefNodeFile(modelFile + ".osgearth_shadergen");osg::Group* sims = new osg::Group();root->addChild( sims );const SpatialReference* wgs84 = SpatialReference::get("wgs84");// 坐標系// Simulator for tethering: 系留模擬器// thing 1 和 thing 2 會隨著時間移動Simulator* sim1 = new Simulator(sims, manip, mapNode, model.get(), "Thing 1", '8');// 8:綁住第一個物體sim1->_name = "Thing 1";sim1->_start = GeoPoint(wgs84, 45.0, 55.0, 10000);// 開始坐標點sim1->_end = GeoPoint(wgs84, -45, -55.0, 10000); // 結束坐標點viewer.addEventHandler(sim1); // 加入到操作器Simulator* sim2 = new Simulator(sims, manip, mapNode, model.get(), "Thing 2", '9');// 9:綁住第二個物體sim2->_name = "Thing 2";sim2->_start = GeoPoint(wgs84, 45.0, 54.0, 10000);sim2->_end = GeoPoint(wgs84, -44.0, -54.0, 10000);viewer.addEventHandler(sim2);manip->getSettings()->getBreakTetherActions().push_back( EarthManipulator::ACTION_GOTO ); // 操作器動作,直接過去 // Set the minimum distance to something larger than the default// 距離物體小于最小距離時,物體會變大manip->getSettings()->setMinMaxDistance(10.0, manip->getSettings()->getMaxDistance());// Sets the maximum focal point offsets (usually for tethering)manip->getSettings()->setMaxOffset(5000.0, 5000.0);// Pitch limits. 俯仰角范圍manip->getSettings()->setMinMaxPitch(-90, 90);viewer.setSceneData( root );//為 鼠標左鍵按下、修改鍵、拖動鼠標的動作指定行為。manip->getSettings()->bindMouse(EarthManipulator::ACTION_EARTH_DRAG,osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON,osgGA::GUIEventAdapter::MODKEY_SHIFT );manip->getSettings()->bindMouseClick(EarthManipulator::ACTION_GOTO,osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON,osgGA::GUIEventAdapter::MODKEY_SHIFT);manip->getSettings()->setArcViewpointTransitions( true ); // 設置tether連接和斷裂時的動作回調manip->setTetherCallback( new TetherCB() );//viewer.addEventHandler(new FlyToViewpointHandler( manip ));// 添加 自定義的各個事件處理viewer.addEventHandler(new LockAzimuthHandler('u', manip));viewer.addEventHandler(new ToggleArcViewpointTransitionsHandler('a', manip));viewer.addEventHandler(new ToggleThrowingHandler('q', manip));viewer.addEventHandler(new ToggleCollisionHandler('k', manip));viewer.addEventHandler(new ToggleProjMatrix('o', manip));viewer.addEventHandler(new BreakTetherHandler('b', manip));viewer.addEventHandler(new CycleTetherMode('t', manip));viewer.addEventHandler(new SetPositionOffset(manip));// 設置偏移viewer.addEventHandler(new ToggleLDB('L'));viewer.addEventHandler(new ToggleSSL(sims, ')'));viewer.addEventHandler(new FitViewToPoints('j', manip, mapNode->getMapSRS()));// 根據 thing 1 的位置,更新相機CalculateWindowCoords* calc = new CalculateWindowCoords('W', manip, sim1);// 根據thing 1計算窗口坐標viewer.addEventHandler(calc);manip->setUpdateCameraCallback(new CameraUpdater(calc));// 相機回調viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);while(!viewer.done()){viewer.frame();// simulate slow frame rate//OpenThreads::Thread::microSleep(1000*1000);}return 0; }

總結

以上是生活随笔為你收集整理的osgEarth示例分析——osgearth_manip的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。