root/OpenSceneGraph/trunk/src/osgAnimation/BasicAnimationManager.cpp @ 11009

Revision 11009, 4.8 kB (checked in by robert, 5 years ago)

From Cedric Pinson, "Here a list of changes:
Bone now inherit from MatrixTransform?. It simplify a lot the update of
Bone matrix. It helps to have the bone system more generic. eg it's now
possible to have animation data with precomputed bind matrix. The other
benefit, is now the collada plugin will be able to use osgAnimation to
display skinned mesh. Michael Plating did a great work to improve this
aspect, he is working on the collada plugin and should be able to submit
a new version soon.
The RigGeometry? has been refactored so now it works when you save and
reload RigGeometry? because the source is not touched anymore. The
benefit with this update is that it should be now possible to use a
MorphGeometry? as source for a RigGeometry?.

The bad news is that the format has changed, so i have rebuild osg-data
related to osgAnimation data, updated the blender exporter to export to
the new format.
The fbx plugin could be touched about this commit, i dont compile it so
i can't give more information about it.
The bvh plugin has been updated by Wang rui so this one is fixed with
the new code of osgAnimation.
The examples has been updated to work with the new code too...

The example osg-data/example.osg should be remove, it's an old example
that does not work.

For people using blender the blender exporter up to date is here:
http://hg.plopbyte.net/osgexport2/
it will be merge to http://hg.plopbyte.net/osgexport/ as soon as the
modification will be push in the trunk.
"

RevLine 
[9370]1/*  -*-c++-*-
[10556]2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
[9370]3 *
4 * This library is open source and may be redistributed and/or modified under 
5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
6 * (at your option) any later version.  The full license is in LICENSE file
7 * included with this distribution, and on the openscenegraph.org website.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * OpenSceneGraph Public License for more details.
13*/
14
15#include <osgAnimation/BasicAnimationManager>
16#include <osgAnimation/LinkVisitor>
17
18using namespace osgAnimation;
19
[9673]20BasicAnimationManager::BasicAnimationManager()
21: _lastUpdate(0.0)
22{
23}
[9370]24
[9673]25BasicAnimationManager::BasicAnimationManager(const AnimationManagerBase& b, const osg::CopyOp& copyop)
26: AnimationManagerBase(b,copyop)
27, _lastUpdate(0.0)
28{
29}
30
31BasicAnimationManager::~BasicAnimationManager()
32{
33}
34
[9370]35void BasicAnimationManager::stopAll()
36{
37    // loop over all playing animation
38    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
39    {
40        AnimationList& list = iterAnim->second;
[11009]41        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
[9370]42            (*it)->resetTargets();
43    }
44    _animationsPlaying.clear();
45}
46
47void BasicAnimationManager::playAnimation(Animation* pAnimation, int priority, float weight)
48{
[9456]49    if (!findAnimation(pAnimation))
50        return;
[9370]51
52    if ( isPlaying(pAnimation) )
53        stopAnimation(pAnimation);
54 
55    _animationsPlaying[priority].push_back(pAnimation);
[9877]56    // for debug
57    //std::cout << "player Animation " << pAnimation->getName() << " at " << _lastUpdate << std::endl;
[9370]58    pAnimation->setStartTime(_lastUpdate);
59    pAnimation->setWeight(weight);
60}
61
62bool BasicAnimationManager::stopAnimation(Animation* pAnimation)
63{
64    // search though the layer and remove animation
[9877]65    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
[9370]66    {
67        AnimationList& list = iterAnim->second;
[11009]68        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
[9370]69            if( (*it) == pAnimation )
70            {
71                (*it)->resetTargets();
72                list.erase(it);
73                return true;
74            }
75    }
76    return false;
77}
78
79
80void BasicAnimationManager::update (double time)
81{
[9877]82    _lastUpdate = time; // keep time of last update
[9370]83
84    // could filtered with an active flag
[11009]85    for (TargetSet::iterator it = _targets.begin(); it != _targets.end(); ++it)
[9370]86        (*it).get()->reset();
87
88    // update from high priority to low priority
89    for( AnimationLayers::reverse_iterator iterAnim = _animationsPlaying.rbegin(); iterAnim != _animationsPlaying.rend(); ++iterAnim )
90    {
91        // update all animation
92        std::vector<int> toremove;
[10556]93        int priority = iterAnim->first;
[9370]94        AnimationList& list = iterAnim->second;
95        for (unsigned int i = 0; i < list.size(); i++)
96        {
[10556]97            if (! list[i]->update(time, priority))
[9877]98            {
99                // debug
100                // std::cout << list[i]->getName() << " finished at " << time << std::endl;
[9370]101                toremove.push_back(i);
[9877]102            } else 
103            {
104                // debug
105                //std::cout << list[i]->getName() << " updated" << std::endl;
106            }
[9370]107        }
108
109        // remove finished animation
110        while (!toremove.empty())
111        {
112            list.erase(list.begin() + toremove.back());
113            toremove.pop_back();
114        }
115    }
116}
117
118
119bool BasicAnimationManager::findAnimation(Animation* pAnimation)
120{
121    for( AnimationList::const_iterator iterAnim = _animations.begin(); iterAnim != _animations.end(); ++iterAnim )
122    {
123        if ( (*iterAnim) == pAnimation )
124            return true;
125    }
126    return false;
127}
128
129
130bool BasicAnimationManager::isPlaying(Animation* pAnimation)
131{
132    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
133    {
134        AnimationList& list = iterAnim->second;
[11009]135        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
[9370]136            if ( (*it) == pAnimation )
137                return true;
138    }
139    return false;
140}
141
142bool BasicAnimationManager::isPlaying(const std::string& name)
143{
144    // loop over all playing animation
145    for( AnimationLayers::iterator iterAnim = _animationsPlaying.begin(); iterAnim != _animationsPlaying.end(); ++iterAnim )
146    {
147        AnimationList& list = iterAnim->second;
[11009]148        for (AnimationList::iterator it = list.begin(); it != list.end(); ++it)
[9370]149            if ( (*it)->getName() == name )
150                return true;
151    }
152    return false;
153}
Note: See TracBrowser for help on using the browser.