osganimationsolid例子

来源:互联网 发布:淘宝促销活动可以关么? 编辑:程序博客网 时间:2024/05/16 14:35

本例子中添加了两个动画,一个是平移动画,一个是旋转动画,相对于之前的例子比较简单。
创建一个坐标轴、一个正方体,
MatrixTransform中添加了一个osgAnimation::UpdateMatrixTransform* updatecb = new

osgAnimation::UpdateMatrixTransform("AnimatedCallback");
UpdateMatrixTransform继承自AnimationUpdateCallback<osg::NodeCallback>,因此前文说过,会调用

bool link(osgAnimation::Channel* channel);link中根据StackedTransform,去更新

setUpdateCallback绑定的节点。下面看看StackedTransform是什么,从字面上理解是转换的堆栈,就是

这样,转换包括平移、旋转、缩放,堆栈可以进栈,出栈,进而改变node。看看是不是这样,
StackedTransform继承自osg::MixinVector<osg::ref_ptr<StackedTransformElement> >。MixinVector

具有vector的功能,类型是StackedTransformElement的指针,StackedTransformElement是什么呢?变

化元素,是一个纯虚类,里面需要绑定目标(Target),需要update,因此,StackedTransform具有

vector的功能,并且里面有很多的StackedTransformElement。
StackedTransformElement有好多子类:StackedTranslateElement、StackedMatrixElement、

StackedQuaternionElement、StackedRotateAxisElement、StackedScaleElement,平移、旋转、缩放、

矩阵、四元数,这些正是Transform的内容。
回到程序中,updatecb->getStackedTransforms().push_back(new

osgAnimation::StackedTranslateElement("position"));
    updatecb->getStackedTransforms

().push_back(new osgAnimation::StackedRotateAxisElement("euler",osg::Vec3(1,0,0),0));
增加了一个平移要素和一个旋转要素,指定要素的名称。

之后还是BasicAnimationManager管理动画,定义了两个animation,
// And we finaly define our channel for linear Vector interpolation
   

osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel;
  

 //name of the AnimationUpdateCallback
    channelAnimation1->setTargetName

("AnimatedCallback");
    //name of the StackedElementTransform for position modification
   

channelAnimation1->setName("position");
    //Create keyframes for (in this case linear)

interpolation of a osg::Vec3
    channelAnimation1->getOrCreateSampler()-

>getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3

(0,0,0)));
    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()-

>push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0)));
    osgAnimation::Animation*

anim1 = new osgAnimation::Animation;
    anim1->addChannel(channelAnimation1);   
    anim1

->setPlayMode(osgAnimation::Animation::PPONG);


    //define the channel for interpolation of

a float angle value
    osgAnimation::FloatLinearChannel* channelAnimation2 = new

osgAnimation::FloatLinearChannel;
    //name of the AnimationUpdateCallback
   

channelAnimation2->setTargetName("AnimatedCallback");
    //name of the

StackedElementTransform for position modification
    channelAnimation2->setName("euler");
  

 //Create keyframes for (in this case linear) interpolation of a osg::Vec3
   

channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back

(osgAnimation::FloatKeyframe(0, 0));
    channelAnimation2->getOrCreateSampler()-

>getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1.5, 2*osg::PI));
   

osgAnimation::Animation* anim2 = new osgAnimation::Animation;
    anim2->addChannel

(channelAnimation2);
    anim2->setPlayMode(osgAnimation::Animation::LOOP);
开始启动吧!

原创粉丝点击