|
按照书上教程做的,只是修改了一个地方animRoot->setUpdateCallback(new osgAnimation::UpdateMatrixTransform("athCallback"));
书上是updateTransform。最后的结果不能实现动画效果,请指教,谢谢!代码如下:- void createKeyframes1(osgAnimation::Vec3KeyframeContainer* ks)
- {
- ks->push_back(osgAnimation::Vec3Keyframe(0,osg::Vec3(0.0,0.0,0.0)));
- ks->push_back(osgAnimation::Vec3Keyframe(10,osg::Vec3(10.0,0.0,-10.0)));
- ks->push_back(osgAnimation::Vec3Keyframe(20,osg::Vec3(0.0,0.0,0.0)));
- }
- void createKeyframes2(osgAnimation::Vec3KeyframeContainer* ks)
- {
- ks->push_back(osgAnimation::Vec3Keyframe(0,osg::Vec3(0.0,0.0,0.0)));
- ks->push_back(osgAnimation::Vec3Keyframe(10,osg::Vec3(0.0,0.0,10.0)));
- ks->push_back(osgAnimation::Vec3Keyframe(15,osg::Vec3(0.0,0.0,8.0)));
- ks->push_back(osgAnimation::Vec3Keyframe(20,osg::Vec3(0.0,0.0,0.0)));
- }
- int main(int argc, char** argv)
- {
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
- osg::ref_ptr<osg::Group> root = new osg::Group();
-
- osg::ref_ptr<osg::Node> model = osgDB::readNodeFile("glider.osg");
- osg::ref_ptr<osgAnimation::Vec3LinearChannel> ch1 = new osgAnimation::Vec3LinearChannel();
- ch1->setName("position");
- ch1->setTargetName("PathCallback");
- createKeyframes1(ch1->getOrCreateSampler()->getOrCreateKeyframeContainer());
- osg::ref_ptr<osgAnimation::Animation> anim1 = new osgAnimation::Animation();
- anim1->setPlayMode(osgAnimation::Animation::LOOP);
- anim1->setStartTime(0.0);
- anim1->addChannel(ch1.get());
- osg::ref_ptr<osgAnimation::Vec3LinearChannel> ch2 = new osgAnimation::Vec3LinearChannel();
- ch2->setName("position");
- ch2->setTargetName("PathCallback");
- createKeyframes2(ch2->getOrCreateSampler()->getOrCreateKeyframeContainer());
- osg::ref_ptr<osgAnimation::Animation> anim2 = new osgAnimation::Animation();
- anim2->setPlayMode(osgAnimation::Animation::LOOP);
- anim2->addChannel(ch2);
- osg::ref_ptr<osg::MatrixTransform> animRoot = new osg::MatrixTransform();
- animRoot->addChild(model);
- animRoot->setDataVariance(osg::Object::DYNAMIC);
- animRoot->setUpdateCallback(new osgAnimation::UpdateMatrixTransform("PathCallback"));
- osg::ref_ptr<osgAnimation::BasicAnimationManager> mng = new osgAnimation::BasicAnimationManager();
- mng->registerAnimation(anim1.get());
- mng->registerAnimation(anim2.get());
-
- root->addChild(animRoot.get());
- root->setUpdateCallback(mng.get());
- mng->playAnimation(anim1.get(),0,0.3);
- mng->playAnimation(anim2.get(),0,0.7);
- osgUtil::Optimizer optimizer;
- optimizer.optimize(root.get());
- viewer->setSceneData(root.get());
- viewer->realize();
-
- return viewer->run();
- }
复制代码 |
|