【转】osg 自动漫游

介绍两种漫游方式,都是操作器的方式,但是都有所不同。
第一种为节点跟踪漫游器(NodeTrackerManipulator),主要是当前视点跟随这个节点的包围盒中心点,所以只要设置这个节点一个路径动画,那么当前视点就跟着节点一起漫游了。
第二种为路径漫游器(AnimationPathManipulator),主要是提前设置路径(AnimationPath),可以设置这个路径上的每个位置上的时间和旋转量,但是在漫游过程中不能修改当前的视点距离,只能按照提前设置好的视角和位置进行观察。
下面给出这两种方式的简单示例代码:其中的路径设置函数可以参考我前面文章关于路径动画的介绍,也可以自己写。
其中main2使用的节点跟踪漫游器(可以一直跟者飞机移动视点),main使用的路径漫游器(当前视点会从牛屁股里出来)。

 1 int main2()
 2 {
 3     osgViewer::Viewer viewer;
 4     viewer.addEventHandler(new ChangeWindow);
 5 
 6     osg::Group *pGroup = new osg::Group;
 7 
 8     osg::MatrixTransform* mt = new osg::MatrixTransform;
 9     osg::Node*glider = osgDB::readNodeFile("glider.osg");
10     mt->addChild(glider);
11 
12     osg::Vec3 ptS = osg::Vec3(0,0,0);
13     osg::Vec3 ptE = osg::Vec3(-1000, 0, 0);
14     osg::AnimationPath* path = createAnimationPath(ptS, ptE, 0, 1000);
15     osg::NodeCallback* nc = new osg::AnimationPathCallback(path);
16 
17     mt->setUpdateCallback(nc);
18 
19     osgGA::NodeTrackerManipulator *tm = new osgGA::NodeTrackerManipulator;
20     osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
21     osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;
22 
23     tm->setTrackerMode(trackerMode);
24     tm->setRotationMode(rotationMode);
25     tm->setTrackNode(glider);
26 
27     pGroup->addChild(mt);
28 
29     pGroup->addChild(osgDB::readNodeFile("cow.osg"));
30 
31     viewer.setCameraManipulator(tm);
32     viewer.setSceneData(pGroup);
33     viewer.run();
34     return 0;
35 }
36 
37 int main()
38 {
39     osgViewer::Viewer viewer;
40     viewer.addEventHandler(new ChangeWindow);
41     osg::Group *pGroup = new osg::Group;
42 
43     osg::MatrixTransform* mt = new osg::MatrixTransform;
44     osg::Node*glider = osgDB::readNodeFile("glider.osg");
45     mt->addChild(glider);
46 
47     osg::Vec3 ptS = osg::Vec3(0,0,0);
48     osg::Vec3 ptE = osg::Vec3(-1000, 0, 0);
49     osg::AnimationPath* path = createAnimationPath(ptS, ptE, 0, 1000);
50 
51     osg::NodeCallback* nc = 0;
52     nc = new osg::AnimationPathCallback(path);
53 
54     osgGA::AnimationPathManipulator *pAn = new osgGA::AnimationPathManipulator(path);
55 
56     pGroup->addChild(mt);
57 
58     pGroup->addChild(osgDB::readNodeFile("cow.osg"));
59 
60     viewer.setCameraManipulator(pAn);
61 
62     viewer.setSceneData(pGroup);
63     viewer.run();
64     return 0;
65 }


---------------------
作者:TheDeaf
来源:CSDN
原文:https://blog.csdn.net/mj511099781/article/details/47006757
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/tianshanhangui/p/10055987.html