Introduction
This example uses the timeline timeline in osganimation to manage animations in the scene. The concept of the timeline is a bit like the three-dimensional rendering process into a frame, a metaphor: timeline a bit like when watching cartoons with the camera to play the animated scene at a fixed interval shot into a picture, and finally the picture management, a bit like the reverse simulation of the process of animation. Example
This example can only be run with the NATHAN.OSG model, because the animation animation in the code relies on the contents of this file, we can open the NATHAN.OSG look, we can find such a few pieces of content:
osganimation::animation {
UniqueID uniqid_animation_698
name "Idle_main"
num_channels
Vec3linearchannel {
name "scale"
target "Foot_ik". L "
Keyframes {
key 0.00000 1.00000 1.00000 1.00000
key 0.04000 1.00000 1.00000 1.00000
key 0.08000 1 .00000 1.00000 1.00000
key 0.12000 1.00000 1.00000 1.00000
key 0.16000 1.00000 1.00000 1.00000
key 0.20000 1.00000 1.00000 1.00000
key 0.24000 1.00000 1.00000 1.00000
...
You can see that this idle_main is exactly a keyframe animation in the NATHAN.OSG, and idle_head_scratch.02 and idle_nose_scratch.01 in the same source can also find their corresponding animations in NATHAN.OSG.
Callback definition
struct Nosebegin:public osganimation::action::callback
{
virtual void operator () (osganimation::action* Action, osganimation::actionvisitor* nv)
{
std::cout << "Sacrebleu, it scratches my nose, let me scratch it" << Std::endl;
Std::cout << "Process Nosebegin call back" << action->getname () << Std::endl << std::endl;
}
};
struct Noseend:public osganimation::action::callback
{
virtual void operator () (osganimation::action* Action , osganimation::actionvisitor* nv)
{
std::cout << "Shhhrt shrrrrt shhhhhhrrrrt, haaa it ' s better" < < Std::endl;
Std::cout << "Process noseend call back" << action->getname () << Std::endl << Std::endl;
}
};
These two sections of callback are defined under the Nose_scratch action, and they are called at some point. Callback are defined in action, and they are intended to be invoked at some point in an action. These actions are invoked on one of the timelines of the scene animation (a bit like the metaphor we played before, a action is bound to a shot in a photo, and this action is triggered to run the animation and callbacks it contains when this time comes)
_scratchnose->setcallback (0.0, new nosebegin);
_scratchnose->setcallback (_scratchnose->getnumframes ()-1, new noseend);
It is known that they were called at the beginning of the _scratchnose (0.0) and at the end of _scratchnose.
Define Action
In code, action is defined in Nodecallback (exampletimelineusage)
_releasekey = false;
_manager = Manager;
Const osganimation::animationlist& list = _manager->getanimationlist ();
Osganimation::animationmap map; for (Osganimation::animationlist::const_iterator it = List.begin (); It! = List.end (); it++) map[(*it)->getn
Ame ()] = *it;
_mainloop = new Osganimation::actionstripanimation (map["Idle_main"].get (), 0.0,0.0); _mainloop->setloop (0);
means forever map["idle_head_scratch.02"]->setduration (4.0);
_scratchhead = new Osganimation::actionstripanimation (map["idle_head_scratch.02"].get (), 0.2,0.3); _scratchhead->setloop (1); One time map["idle_nose_scratch.01"]->setduration (10.0); Set this animation duration to seconds _scratchnose = new Osganimation::actionstripanimation (map["Idle_nose_
scratch.01 "].get (), 0.2,0.3); _scratchnose->setloop (1);
One time//Add the main loop at priority 0 at time 0.
osganimation::timeline* tml = _manager->gettimeline ();
Tml->play ();
Tml->addactionat (0.0, _mainloop.get (), 0);
Add a scratch Head 1 at 3.0 second.
Tml->addactionat (5.0, _scratchhead.get (), 1); Populate time with scratch head for (int i = 1; i <; i++) {//We add a scratch head Priority 1 Second//NOTE://It's possible to add the same instance more then once O n the timeline//The only things your need to take care are if you remove it. It'll remove//All instance this exist on the timeline. If you need to differtiate//It's better to create a new instance Tml->addactionat (5.0 +
10.0 * I, _scratchhead.get (), 1); }//We'll add the scratch nose action only when the player hits a key//in the operator ()// Now we'll add callback at End and begin of animation of idle_nose_scratch.02 _scratchnose->setcallback (0.0, new nosebegin); _scratchnose->setcallback (_scratchnose->getnumframes ()-1, new noseend);
Add them to the timeline and set the button to add an action when the keyboard is bouncing
if (NV && nv->getvisitortype () = = Osg::nodevisitor::update_visitor)
{
if (_releasekey)//We hit a key and release it execute an action
{
osganimation::timeline* tml = _manager->gettimeline ();
Dont play if already playing
if (!tml->isactive (_scratchnose.get ()))
{
//Add this animation on top of T Wo other
//We add one to evaluate the animation at the next frame, else we
//would miss the current frame
T Ml->addactionat (Tml->getcurrentframe () + 1, _scratchnose.get (), 1);
}
_releasekey = false;
}
}
Add Manager
Use the same way as other managers to add managers to nodes in the scene
osg::ref_ptr<osganimation::timelineanimationmanager> tl = new Osganimation::timelineanimationmanager (* Animationmanager);
Root->setupdatecallback (Tl.get ());
exampletimelineusage* callback = new Exampletimelineusage (Tl.get ());
Root->seteventcallback (callback);
Root->getupdatecallback ()->addnestedcallback (callback);
Attached: There is a bug,idle_head_scratch.02 in this example that will not be called, we need to add the action time, and use Setduration to set an action to run the time just fine.