查看: 943|回复: 1

osgAnimation回调问题

[复制链接]

该用户从未签到

发表于 2015-1-29 15:15:58 | 显示全部楼层 |阅读模式
按照书上教程做的,只是修改了一个地方animRoot->setUpdateCallback(new osgAnimation::UpdateMatrixTransform("athCallback"));
书上是updateTransform。最后的结果不能实现动画效果,请指教,谢谢!代码如下:
  1. void createKeyframes1(osgAnimation::Vec3KeyframeContainer* ks)
  2. {
  3.         ks->push_back(osgAnimation::Vec3Keyframe(0,osg::Vec3(0.0,0.0,0.0)));
  4.         ks->push_back(osgAnimation::Vec3Keyframe(10,osg::Vec3(10.0,0.0,-10.0)));
  5.         ks->push_back(osgAnimation::Vec3Keyframe(20,osg::Vec3(0.0,0.0,0.0)));
  6. }

  7. void createKeyframes2(osgAnimation::Vec3KeyframeContainer* ks)
  8. {
  9.         ks->push_back(osgAnimation::Vec3Keyframe(0,osg::Vec3(0.0,0.0,0.0)));
  10.         ks->push_back(osgAnimation::Vec3Keyframe(10,osg::Vec3(0.0,0.0,10.0)));
  11.         ks->push_back(osgAnimation::Vec3Keyframe(15,osg::Vec3(0.0,0.0,8.0)));
  12.         ks->push_back(osgAnimation::Vec3Keyframe(20,osg::Vec3(0.0,0.0,0.0)));
  13. }


  14. int main(int argc, char** argv)
  15. {
  16.         osg::ref_ptr<osgViewer::Viewer>                                        viewer        = new osgViewer::Viewer();
  17.         osg::ref_ptr<osg::Group>                                                root                = new osg::Group();
  18.        
  19.         osg::ref_ptr<osg::Node>                                                model        = osgDB::readNodeFile("glider.osg");


  20.         osg::ref_ptr<osgAnimation::Vec3LinearChannel>        ch1                = new osgAnimation::Vec3LinearChannel();
  21.         ch1->setName("position");
  22.         ch1->setTargetName("PathCallback");
  23.         createKeyframes1(ch1->getOrCreateSampler()->getOrCreateKeyframeContainer());

  24.         osg::ref_ptr<osgAnimation::Animation>                        anim1        = new osgAnimation::Animation();
  25.         anim1->setPlayMode(osgAnimation::Animation::LOOP);
  26.         anim1->setStartTime(0.0);
  27.         anim1->addChannel(ch1.get());

  28.         osg::ref_ptr<osgAnimation::Vec3LinearChannel>        ch2                =  new osgAnimation::Vec3LinearChannel();
  29.         ch2->setName("position");
  30.         ch2->setTargetName("PathCallback");
  31.         createKeyframes2(ch2->getOrCreateSampler()->getOrCreateKeyframeContainer());

  32.         osg::ref_ptr<osgAnimation::Animation>                        anim2        = new osgAnimation::Animation();
  33.         anim2->setPlayMode(osgAnimation::Animation::LOOP);
  34.         anim2->addChannel(ch2);


  35.         osg::ref_ptr<osg::MatrixTransform>                                animRoot        = new osg::MatrixTransform();
  36.         animRoot->addChild(model);
  37.         animRoot->setDataVariance(osg::Object::DYNAMIC);
  38.         animRoot->setUpdateCallback(new osgAnimation::UpdateMatrixTransform("PathCallback"));

  39.         osg::ref_ptr<osgAnimation::BasicAnimationManager>        mng                = new osgAnimation::BasicAnimationManager();
  40.         mng->registerAnimation(anim1.get());
  41.         mng->registerAnimation(anim2.get());

  42.        
  43.         root->addChild(animRoot.get());
  44.         root->setUpdateCallback(mng.get());

  45.         mng->playAnimation(anim1.get(),0,0.3);
  46.         mng->playAnimation(anim2.get(),0,0.7);

  47.         osgUtil::Optimizer optimizer;
  48.         optimizer.optimize(root.get());

  49.         viewer->setSceneData(root.get());
  50.         viewer->realize();
  51.        

  52.         return viewer->run();

  53. }
复制代码

该用户从未签到

 楼主| 发表于 2015-1-29 16:19:33 | 显示全部楼层
找到问题所在了
  1.         osg::ref_ptr<osg::MatrixTransform>                                animRoot        = new osg::MatrixTransform();
  2.         animRoot->setDataVariance(osg::Object::DYNAMIC);
  3.         osgAnimation::UpdateMatrixTransform*  upMat = new osgAnimation::UpdateMatrixTransform("PathCallback");
  4.         upMat->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position"));
  5.         upMat->getStackedTransforms().push_back(new osgAnimation::StackedRotateAxisElement("euler",osg::Vec3(0.0,0.0,1.0),0));
  6.         animRoot->setUpdateCallback(upMat);
  7.         animRoot->addChild(model.get());
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表