在osg中添加相機動畫路徑請參考:http://www.cnblogs.com/lyggqm/p/8075277.html
這里的代碼是在osgearth中添加相機動畫路徑漫游器:
#include <osg/Image> #include <osgGA/StateSetManipulator> #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #include <osgEarth/Map> #include <osgEarth/MapNode> #include <osgEarth/Registry> #include <osgEarthSymbology/Geometry> #include <osgEarthSymbology/GeometryRasterizer> #include <osgEarthUtil/EarthManipulator> #include <osgEarthUtil/AutoClipPlaneHandler> #include <osgEarth/ImageToHeightFieldConverter> #include <osgEarth/ImageUtils> #include <osgEarth/FileUtils> #include <osgEarth/Registry> #include <osgEarth/MapFrame> #include <osgDB/FileUtils> #include <osgDB/FileNameUtils> #include <osgDB/ReadFile> #include <osgDB/WriteFile> #include <osgEarthUtil/ExampleResources> #include <math.h> #include <osgGA/NodeTrackerManipulator> #include <osgGA/AnimationPathManipulator> #include <osgGA/KeySwitchMatrixManipulator> using namespace osgEarth; using namespace osgEarth::Util; using namespace osgEarth::Symbology; int main(int argc, char** argv) { //正常的.earth文件加載 osg::ArgumentParser arguments(&argc, argv); osgViewer::Viewer viewer(arguments); MapNode* s_mapNode = 0L; osg::Node* earthFile = MapNodeHelper().load(arguments, &viewer); if (earthFile) s_mapNode = MapNode::get(earthFile); if (!s_mapNode) { OE_WARN << "Unable to load earth file." << std::endl; return -1; } osg::Group* root = new osg::Group(); root->addChild(earthFile); viewer.setSceneData(root); //模型漫游器 osgGA::NodeTrackerManipulator* nodeTrack = new osgGA::NodeTrackerManipulator(); nodeTrack->setTrackNode(root); /*************************************動畫漫游器**下*********************************/ GeoPoint gPoint1(s_mapNode->getMap()->getSRS(), 32, 118, 400); osg::Matrix gMatrix1; gPoint1.createLocalToWorld(gMatrix1);//獲取當前地球上的正確姿態 //由於相機,自身向下看,所以在當前姿態基礎上抬起60度,注意是前乘! gMatrix1.preMultRotate(osg::Quat(osg::DegreesToRadians(60.0), osg::X_AXIS)); osg::Quat q1; gMatrix1.get(q1);//獲取當前矩陣姿態 osg::Vec3d vPos1 = gMatrix1.getTrans();//獲取當前矩陣位置 GeoPoint gPoint2(s_mapNode->getMap()->getSRS(), 32.01, 118.01, 400); osg::Matrix gMatrix2; gPoint2.createLocalToWorld(gMatrix2); gMatrix2.preMultRotate(osg::Quat(osg::DegreesToRadians(60.0), osg::X_AXIS)); osg::Quat q2; gMatrix2.get(q2); osg::Vec3d vPos2 = gMatrix2.getTrans(); GeoPoint gPoint3(s_mapNode->getMap()->getSRS(), 32.02, 118.02, 400); osg::Matrix gMatrix3; gPoint3.createLocalToWorld(gMatrix3); osg::Quat q3; gMatrix3.get(q3); osg::Vec3d vPos3 = gMatrix3.getTrans(); //獲取相機之后再順旋轉,其實是錯誤的姿態 osg::Quat qbuf(osg::DegreesToRadians(60.0), osg::X_AXIS); q3 *= qbuf; //使用動畫漫游器 osgGA::AnimationPathManipulator *animationPathMp = new osgGA::AnimationPathManipulator(); //給動畫漫游器添加關鍵幀 osg::AnimationPath* _animationPath = new osg::AnimationPath; _animationPath->insert(0.0, osg::AnimationPath::ControlPoint(vPos1, q1));//姿態正確 _animationPath->insert(3.0, osg::AnimationPath::ControlPoint(vPos2, q2));//姿態正確 _animationPath->insert(6.0, osg::AnimationPath::ControlPoint(vPos3, q3));//姿態錯誤! _animationPath->setLoopMode(osg::AnimationPath::SWING);//設置路徑是回擺的 animationPathMp->setAnimationPath(_animationPath); /*************************************動畫漫游器**上*********************************/ //這里添加三個漫游器,使用一個控制漫游器選擇,按鍵盤‘3’就切換到路徑動畫漫游器了 osgGA::KeySwitchMatrixManipulator* keyPtr = new osgGA::KeySwitchMatrixManipulator(); keyPtr->addMatrixManipulator('1', "earthMan", new EarthManipulator()); keyPtr->addMatrixManipulator('2', "trakerMan", nodeTrack); keyPtr->addMatrixManipulator('3', "animationPathMan", animationPathMp); viewer.setCameraManipulator(keyPtr); return viewer.run(); }
有幾點要注意:
1.這個dome在vs運行時,需要添加外部earth文件參數:
紅色部分填寫適合當前earth版本的‘.earth’文件
2.添加的其實是一個漫游器,基於路徑點的相機漫游器
3.添加的路徑點有兩個姿態的變幻,展示一個正確相機姿態調整方式,一個錯誤的:
正確的:
錯誤的: