Osganimation's animation manager

Source: Internet
Author: User
Introduction

In the Updatematrixtransform example in the previous section of the Osganimation action object, we animate by adding updatecallback, In fact Osganimation has provided us with the basic animation processing class Basicanimationmanager, which inherits from Nodecallback and is responsible for managing multiple animated objects, and now let's see how it's done. Animation Manager

There are several classes involved in the animation manager: Linkvisitor, Animationmanagerbase, and Basicanimationmanager. Let's look at the implementation of these classes. Linkvisitor

Linkvisitor uses an internal helper class in Animationmanagerbase to traverse the objects under the sub-scene (Updatematrixtransform, etc.), and we do not use this class directly in the development code. Let's focus on the role of this class: The code is as follows

void Linkvisitor::apply (osg::node& Node)
{
    osg::stateset* st = Node.getstateset ();
    if (ST)
        Handle_stateset (ST);

    osg::nodecallback* cb = Node.getupdatecallback ();
    while (CB)
    {
        osganimation::animationupdatecallbackbase* CBA = dynamic_cast<osganimation:: Animationupdatecallbackbase*> (CB);
        if (CBA)
            link (CBA);
        CB = Cb->getnestedcallback ();
    }
    Traverse (node);
}

void Linkvisitor::apply (osg::geode& node)
{for
    (unsigned int i = 0; i < node.getnumdrawables (); i++) 
  {
        osg::D rawable* drawable = node.getdrawable (i);
        if (drawable && drawable->getstateset ())
            Handle_stateset (Drawable->getstateset ());
    }
    Apply (static_cast<osg::node&> (Node));
}

Linkvisitor is a nodevisitor to see if it is implemented we know it is used to collect objects, and the object and the animation link (essentially the channel and the role of the object link) That is, the target object that holds the result of the operation in the channel is set to the target of the element in the array in the object (the target in Stackedtransformelement is set to channel)

Animationmanagerbase

Animationmanagerbase is the base class for the animation manager, which provides the basic functionality required by the animation manager, which is a virtual base class that requires a derived class to implement the update member function. Since this class is a nodecallback, it will inevitably overload the operator () operator, so let's see what it does:

void Animationmanagerbase::operator () (osg::node* Node, osg::nodevisitor* nv)
{
    if (NV && nv-> Getvisitortype () = = Osg::nodevisitor::update_visitor)
    {
        if (Needtolink ())
        {
            Link (node);
        }
        Const osg::framestamp* FS = Nv->getframestamp ();
        Update (Fs->getsimulationtime ());
    }
    Traverse (NODE,NV);
}
It is not hard to see that our playcallback is very similar to this class, compared to the example we updatematrixtransform in the Osganimation object, this update (Fs->getsimulationtime ( ) Call drives the entire animation animation, making the whole animation frame-by-frame move up.

In addition, we set Playcallback as the parent of the animated sub-scene (Ball node) in the Updatematrixtransform example of the Osganimation action object. The playcallback to update the matrix value of the MatrixTransform on the animated sub-node before the scene traverses to the animation sub-node, thus achieving the effect of moving the position of the sub-scene when traversing to the animated sub-scene. Similarly, here animationmanagerbase should also do it. Yes, that's it. This is what link is doing in the code above, and let's take a look at the implementation:

void Animationmanagerbase::link (osg::node* subgraph)
{
    linkvisitor* linker = Getorcreatelinkvisitor ();
    Linker->getanimationlist (). Clear ();
    Linker->getanimationlist () = _animations;

    Subgraph->accept (*linker);
    _needtolink = false;
    Buildtargetreference ();
}
Here, the target of all stackedtransformelement is set to the channel via Linkvisitor, and then via Update (Fs->getsimulationtime ()) To update the value of target in all channel. As to what the update (Fs->getsimulationtime ()) has done, please read on.

Basicanimationmanager

This class integrates the Animationmanagerbase class and overrides the Update method, which is also the manager class we use most often in our code, and now look at what it does in its update:

void basicanimationmanager::update (double time) {_lastupdate = time;

for (Targetset::iterator it = _targets.begin (); It! = _targets.end (); ++it) (*it). Get ()->reset (); The Std::map key value is low to high, where a reverse iterator is used, so the for (Animationlayers::reverse_iterator Iteranim = _) is operated from highest to lowest priority Animationsplaying.rbegin (); Iteranim! = _animationsplaying.rend ();
        ++iteranim) {std::vector<int> toremove;
		
        int priority = iteranim->first;
        animationlist& list = iteranim->second;
            for (unsigned int i = 0; i < list.size (); i++) {if (. list[i]->update (time, priority))
            {Toremove.push_back (i);
            }} while (!toremove.empty ()) {List.erase (List.begin () + toremove.back ());
        Toremove.pop_back (); }
    }
}
The animationlayers here are defined as: typedef std::map<int, Animationlist > animationlayers; It is a std::map, the first element represents the priority, that is, when the data in the update target is prioritized from high to low. In the updatematrixtransform example of the Osganimation object, we have just updated a animation object, here we have updated more, but the principle is similar.

Summary

In combination with the above analysis, we know that the job of the manager is actually to use an update callback to drive the data to complete the update. It requires several processes such as:

1. Set the target of the element (that is, stackedtransformelement) in the scene to the channel, which establishes the transmission link of the target for the data updatematrixtransform ; This process is done through linkvisitor.

2. Update the call in the callback, this update call to play a series of chain reaction, the animation all channel Update call, essentially the call channel inside all target update, so as to complete the data update , this process is done in update Basicanimationmanager.

An example of this lesson is in the Osganimationsolid sample in OSG Examples.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.