|
代码来自王锐的OSG设计与实践第9章关键帧动画的例子,敲完编译后发现代码有点low了,代码中体现如下几点:1.osgAnimation::UpdateTransform类在3.0版本后被osgAnimation::UpdateMatrixTransform取代;
2. osgAnimation::AnimationUpdateCallback类由非模板变成模板了;
3.osgAnimation::Target类的normalize功能函数被废除了。。
这里简要阐述下我的问题:
根据书上所述,Animiation动画体的update(time)内部调用对应动画频道channel的update(t),而channel的update(t)内部继续调用
Target的update(weight,val)..而target的update已经执行了_result的更新,那么target的normalize用来干嘛(书上说是加权计算,target的update已经加权了啊)?
所以我注掉了target->normalize(),可是运行结果仍是是静态的,why?下面贴出源码:
- #include <iostream>
- using namespace std;
- #include <osg/MatrixTransform>
- #include <osgAnimation/UpdateMatrixTransform>
- #include <osgDB/ReadFile>
- #include <osgViewer/Viewer>
-
- class PlayCallback: public osg::NodeCallback
- {
- public:
- PlayCallback(osgAnimation::Animation* anim):_animation(anim){}
-
- void operator()(osg::Node* node,osg::NodeVisitor* nv)
- {
- if(nv->getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR)
- {
- osgAnimation::ChannelList& channels=_animation->getChannels();
- for(osgAnimation::ChannelList::iterator itr=channels.begin();
- itr!=channels.end();++itr)
- {
- osgAnimation::Target* target=(*itr)->getTarget();
- target->reset();
- }
- const osg::FrameStamp* fs=nv->getFrameStamp();
- _animation->update(fs->getSimulationTime());
-
- for(osgAnimation::ChannelList::iterator itr=channels.begin();
- itr!=channels.end();++itr)
- {
- osgAnimation::Target* target=(*itr)->getTarget();
- //target->normalize();
-
- }
- }
- traverse(node,nv);
- }
- protected:
- osgAnimation::Animation* _animation;
- };
-
- void createPositionKeyframes(osgAnimation::Vec3KeyframeContainer* kc)
- {
- kc->push_back(osgAnimation::Vec3Keyframe(0.0,osg::Vec3(0.0,0.0,0.0)));
- kc->push_back(osgAnimation::Vec3Keyframe(2.0,osg::Vec3(5.0,0.0,0.0)));
- kc->push_back(osgAnimation::Vec3Keyframe(4.0,osg::Vec3(5.0,0.0,5.0)));
- kc->push_back(osgAnimation::Vec3Keyframe(6.0,osg::Vec3(0.0,0.0,5.0)));
- kc->push_back(osgAnimation::Vec3Keyframe(8.0,osg::Vec3(0.0,0.0,0.0)));
- }
-
- void createEulerKeyframes(osgAnimation::Vec3KeyframeContainer* kc)
- {
-
- kc->push_back(osgAnimation::Vec3Keyframe(0.0,osg::Vec3(0.0,0.0,0.0)));
- kc->push_back(osgAnimation::Vec3Keyframe(2.0,osg::Vec3(osg::PI,0.0,0.0)));
- kc->push_back(osgAnimation::Vec3Keyframe(4.0,osg::Vec3(osg::PI,0.0,osg::PI)));
- kc->push_back(osgAnimation::Vec3Keyframe(6.0,osg::Vec3(0.0,0.0,osg::PI)));
- kc->push_back(osgAnimation::Vec3Keyframe(8.0,osg::Vec3(0.0,0.0,0.0)));
- }
-
- int main(int argc, char** argv)
- {
- osg::ArgumentParser arguments(&argc,argv);
- osg::Node* model=osgDB::readNodeFiles(arguments);
- if(!model) model=osgDB::readNodeFile("glider.osg");
-
- osg::ref_ptr<osgAnimation::Vec3LinearChannel> ch1=
- new osgAnimation::Vec3LinearChannel;
- ch1->setName("position");
- ch1->setTargetName("PathCallback");
- createPositionKeyframes(
- ch1->getOrCreateSampler()->getOrCreateKeyframeContainer());
-
- osg::ref_ptr<osgAnimation::Vec3LinearChannel> ch2=
- new osgAnimation::Vec3LinearChannel;
-
- ch2->setName("euler");
- ch2->setTargetName("PathCallback");
- createEulerKeyframes(
- ch2->getOrCreateSampler()->getOrCreateKeyframeContainer());
-
- osg::ref_ptr<osgAnimation::Animation> anim=
- new osgAnimation::Animation;
- anim->setPlayMode(osgAnimation::Animation::LOOP);
- anim->setStartTime(0.0);
- anim->setWeight(1.0);
- anim->addChannel(ch1.get());
- anim->addChannel(ch2.get());
-
- osg::ref_ptr<osgAnimation::UpdateMatrixTransform> callback=
- new osgAnimation::UpdateMatrixTransform("PathCallback");
- callback->osgAnimation::AnimationUpdateCallback<osg::NodeCallback>::link(anim.get());
-
-
- osg::ref_ptr<osg::MatrixTransform> animMat=new osg::MatrixTransform;
- animMat->addChild(model);
- animMat->setDataVariance(osg::Object::DYNAMIC );
- animMat->setUpdateCallback(callback.get());
-
- osg::ref_ptr<osg::Group> root=new osg::Group;
- root->addChild(animMat.get());
- root->setUpdateCallback(new PlayCallback(anim.get()));
-
- osgViewer::Viewer viewer;
- viewer.setSceneData(root.get());
- return viewer.run();
- }
复制代码 |
|