您的位置:首页 > 其它

osg 自动漫游

2015-07-22 17:38 295 查看
介绍两种漫游方式,都是操作器的方式,但是都有所不同。

第一种为节点跟踪漫游器(NodeTrackerManipulator),主要是当前视点跟随这个节点的包围盒中心点,所以只要设置这个节点一个路径动画,那么当前视点就跟着节点一起漫游了。

第二种为路径漫游器(AnimationPathManipulator),主要是提前设置路径(AnimationPath),可以设置这个路径上的每个位置上的时间和旋转量,但是在漫游过程中不能修改当前的视点距离,只能按照提前设置好的视角和位置进行观察。

下面给出这两种方式的简单示例代码:其中的路径设置函数可以参考我前面文章关于路径动画的介绍,也可以自己写。

其中main2使用的节点跟踪漫游器(可以一直跟者飞机移动视点),main使用的路径漫游器(当前视点会从牛屁股里出来)。

int main2()
{
osgViewer::Viewer viewer;
viewer.addEventHandler(new ChangeWindow);

osg::Group *pGroup = new osg::Group;

osg::MatrixTransform* mt = new osg::MatrixTransform;
osg::Node*glider = osgDB::readNodeFile("glider.osg");
mt->addChild(glider);

osg::Vec3 ptS = osg::Vec3(0,0,0);
osg::Vec3 ptE = osg::Vec3(-1000, 0, 0);
osg::AnimationPath* path = createAnimationPath(ptS, ptE, 0, 1000);
osg::NodeCallback* nc = new osg::AnimationPathCallback(path);

mt->setUpdateCallback(nc);

osgGA::NodeTrackerManipulator *tm = new osgGA::NodeTrackerManipulator;
osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER_AND_ROTATION;
osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::TRACKBALL;

tm->setTrackerMode(trackerMode);
tm->setRotationMode(rotationMode);
tm->setTrackNode(glider);

pGroup->addChild(mt);

pGroup->addChild(osgDB::readNodeFile("cow.osg"));

viewer.setCameraManipulator(tm);

viewer.setSceneData(pGroup);
viewer.run();
return 0;
}

int main()
{
osgViewer::Viewer viewer;
viewer.addEventHandler(new ChangeWindow);
osg::Group *pGroup = new osg::Group;

osg::MatrixTransform* mt = new osg::MatrixTransform;
osg::Node*glider = osgDB::readNodeFile("glider.osg");
mt->addChild(glider);

osg::Vec3 ptS = osg::Vec3(0,0,0);
osg::Vec3 ptE = osg::Vec3(-1000, 0, 0);
osg::AnimationPath* path = createAnimationPath(ptS, ptE, 0, 1000);

osg::NodeCallback* nc = 0;
nc = new osg::AnimationPathCallback(path);

osgGA::AnimationPathManipulator *pAn = new osgGA::AnimationPathManipulator(path);

pGroup->addChild(mt);

pGroup->addChild(osgDB::readNodeFile("cow.osg"));

viewer.setCameraManipulator(pAn);

viewer.setSceneData(pGroup);
viewer.run();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: