Show
Ignore:
Timestamp:
11/04/10 16:24:00 (3 years ago)
Author:
robert
Message:

From Jan Klimke, "I did recently some work understanding the osgAnimation classes. Here the osganimationsolid example seems not to be very helpful at the moment. There are basically no comments in it and additionally there is a second animation defined which was simply not working (wrong channel type for angle animation). I added some comments and fixed the example to contain 2 working animations by now. I think this could help others who are trying to understand the osgAnimation plugin.

"

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osganimationsolid/osganimationsolid.cpp

    r11338 r11890  
    2525#include <osgAnimation/UpdateMatrixTransform> 
    2626#include <osgAnimation/StackedTranslateElement> 
     27#include <osgAnimation/StackedRotateAxisElement> 
    2728 
    2829using namespace osgAnimation; 
     
    7374    geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),0.5))); 
    7475 
     76    //Tranformation to be manipulated by the animation 
    7577    osg::ref_ptr<osg::MatrixTransform> trans = new osg::MatrixTransform(); 
    7678    trans->setName("AnimatedNode"); 
     79    //Dynamic object, has to be updated during update traversal 
    7780    trans->setDataVariance(osg::Object::DYNAMIC); 
     81    //Animation callback for Matrix transforms, name is targetName for Channels 
    7882    osgAnimation::UpdateMatrixTransform* updatecb = new osgAnimation::UpdateMatrixTransform("AnimatedCallback"); 
     83    //add manipulator Stack, names must match with channel names 
     84    //elements are applied in LIFO order 
     85    //The first element modifies the position component of the matrix 
     86    //The second element modifies the rotation around x-axis 
    7987    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedTranslateElement("position")); 
     88    updatecb->getStackedTransforms().push_back(new osgAnimation::StackedRotateAxisElement("euler",osg::Vec3(1,0,0),0)); 
     89    //connect the UpdateMatrixTransform callback to the MatrixTRanform 
    8090    trans->setUpdateCallback(updatecb); 
     91    //initialize MatrixTranform 
    8192    trans->setMatrix(osg::Matrix::identity()); 
     93    //append geometry node 
    8294    trans->addChild (geode.get()); 
    8395 
     
    8799    // Define a scheduler for our animations 
    88100    osg::Group* grp = new osg::Group; 
     101    //add the animation manager to the scene graph to get it called during update traversals 
    89102    osgAnimation::BasicAnimationManager* mng = new osgAnimation::BasicAnimationManager(); 
    90103    grp->setUpdateCallback(mng); 
    91  
     104    //add the rest of the scene to the grp node 
    92105    grp->addChild(root); 
    93106 
    94     // And we finaly define our channel 
     107    // And we finaly define our channel for linear Vector interpolation 
    95108    osgAnimation::Vec3LinearChannel* channelAnimation1 = new osgAnimation::Vec3LinearChannel; 
     109    //name of the AnimationUpdateCallback 
    96110    channelAnimation1->setTargetName("AnimatedCallback"); 
     111    //name of the StackedElementTransform for position modification 
    97112    channelAnimation1->setName("position"); 
     113    //Create keyframes for (in this case linear) interpolation of a osg::Vec3 
    98114    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0))); 
    99115    channelAnimation1->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(2, osg::Vec3(1,1,0))); 
    100116    osgAnimation::Animation* anim1 = new osgAnimation::Animation; 
    101     anim1->addChannel(channelAnimation1); 
     117    anim1->addChannel(channelAnimation1);     
    102118    anim1->setPlayMode(osgAnimation::Animation::PPONG); 
    103119 
    104120 
    105     osgAnimation::Vec3LinearChannel* channelAnimation2 = new osgAnimation::Vec3LinearChannel; 
     121    //define the channel for interpolation of a float angle value 
     122    osgAnimation::FloatLinearChannel* channelAnimation2 = new osgAnimation::FloatLinearChannel; 
     123    //name of the AnimationUpdateCallback 
    106124    channelAnimation2->setTargetName("AnimatedCallback"); 
     125    //name of the StackedElementTransform for position modification 
    107126    channelAnimation2->setName("euler"); 
    108     channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(0, osg::Vec3(0,0,0))); 
    109     channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::Vec3Keyframe(1.5, osg::Vec3(2*osg::PI,0,0))); 
     127    //Create keyframes for (in this case linear) interpolation of a osg::Vec3 
     128    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(0, 0)); 
     129    channelAnimation2->getOrCreateSampler()->getOrCreateKeyframeContainer()->push_back(osgAnimation::FloatKeyframe(1.5, 2*osg::PI)); 
    110130    osgAnimation::Animation* anim2 = new osgAnimation::Animation; 
    111131    anim2->addChannel(channelAnimation2); 
     
    116136    mng->registerAnimation(anim1); 
    117137    mng->registerAnimation(anim2); 
    118  
     138     
     139    //start the animation 
    119140    mng->playAnimation(anim1); 
    120141    mng->playAnimation(anim2); 
    121142 
     143    //set the grp-Group with the scene and the AnimationManager as viewer's scene data 
    122144    viewer.setSceneData( grp ); 
    123145    return viewer.run();