OSG中從當前場景位置漫遊到指定點座標位置,osg中場景的視口狀態包括以下參數:spa
一、視點的位置3d
二、參考點的位置,該點一般爲場景中的中心軸上的點code
三、視點向上的方向向量orm
( const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up );blog
經過這三個參數就能夠肯定視口中模型的狀態位置ip
實現的漫遊代碼以下:get
1 //是否漫遊 2 //視角移動的 效果 3 void LargeScaleManipulator::setIsTrans(bool b) 4 { 5 isTrans = b; 6 } 7 void LargeScaleManipulator::setcurrentviewmatrix(osg::Vec3 eye, osg::Vec3 center, osg::Vec3 up) 8 { 9 currenteye = eye; 10 currentcenter = center; 11 currentup = up; 12 } 13 14 void LargeScaleManipulator::settargetviewmatrix(osg::Vec3 eye, osg::Vec3 center, osg::Vec3 up) 15 { 16 targeteye = eye; 17 targetcenter = center; 18 targetup = up; 19 } 20 21 void LargeScaleManipulator::calintervalviewmatrix() 22 { 23 intervalcenter = (targetcenter - currentcenter) / 60; 24 intervaleye = (targeteye - currenteye) / 60; 25 intervalup = (targetup - currentup) / 60; 26 } 27 28 29 bool LargeScaleManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us) 30 { 31 if (ea.getEventType() == osgGA::GUIEventAdapter::FRAME) 32 { 33 if (isTrans) 34 { 35 if (fabs(intervaleye[0]) < 1e-6 && fabs(intervaleye[1]) < 1e-6) 36 { 37 setTransformation(targeteye, targetcenter, targetup); 38 isTrans = false; 39 } 40 if (((fabs(currenteye[0] - targeteye[0]) >= fabs(intervaleye[0])) || (fabs(currentcenter[0] - targetcenter[0]) >= fabs(intervalcenter[0]))) 41 &&((fabs(currenteye[1] - targeteye[1]) >= fabs(intervaleye[1])) || (fabs(currentcenter[1] - targetcenter[1]) >= fabs(intervalcenter[1])))) 42 { 43 if (fabs(currenteye[0] - targeteye[0]) >= fabs(intervaleye[0])) 44 { 45 currenteye += intervaleye; 46 } 47 if (fabs(currentcenter[0] - targetcenter[0]) >= fabs(intervalcenter[0])) 48 { 49 currentcenter += intervalcenter; 50 } 51 currentup += intervalup; 52 setTransformation(currenteye, currentcenter, currentup); 53 } 54 else 55 { 56 setTransformation(targeteye, targetcenter, targetup); 57 isTrans = false; 58 } 59 } 60 } 61 return OrbitManipulator::handle(ea, us); 62 }
以上就是經過傳入一個當前位置狀態,而後給定一個目標位置狀態,經過設置漫遊速度,場景就會一直朝着目標位置變換,也就有了漫遊效果it
調用代碼以下:io
1 osg::Vec3d target; 2 target.set(x, y, z); 3 osg::Vec3d currenteye; 4 osg::Vec3d currentcenter; 5 osg::Vec3d currentup; 6 osg::Vec3d targeteye; 7 osg::Vec3d targetcenter; 8 osg::Vec3d targetup; 9 10 m_pOsgView->getCamera()->getViewMatrixAsLookAt(currenteye, currentcenter, currentup); 11 12 targeteye = target + currenteye - currentcenter; 13 targetcenter = target; 14 targetup = currentup; 15 16 KeySwitchMatrixManipulator* keyswitchManipulator = dynamic_cast<KeySwitchMatrixManipulator*>(m_pDataManagerSingleton->getViewer()->getCameraManipulator()); 17 if (keyswitchManipulator) 18 { 19 keyswitchManipulator->selectMatrixManipulator(0); 20 LargeScaleManipulator* mp = dynamic_cast<LargeScaleManipulator*>(keyswitchManipulator->getCurrentMatrixManipulator()); 21 if (mp) 22 { 23 mp->setcurrentviewmatrix(currenteye, currentcenter, currentup); 24 mp->settargetviewmatrix(targeteye, targetcenter, targetup); 25 mp->calintervalviewmatrix(); 26 mp->isTrans = true; 27 } 28 }