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

Revision 11009, 3.6 kB (checked in by robert, 4 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 
[9093]1/*  -*-c++-*-
[10556]2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
[9093]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/Animation>
16
17using namespace osgAnimation;
18
[10393]19Animation::Animation(const osgAnimation::Animation& anim, const osg::CopyOp& copyop): osg::Object(anim, copyop),
[10386]20    _duration(anim._duration),
21    _originalDuration(anim._originalDuration),
22    _weight(anim._weight),
23    _startTime(anim._startTime),
24    _playmode(anim._playmode)
[9093]25{
[10386]26    const ChannelList& cl = anim.getChannels();
[11009]27    for (ChannelList::const_iterator it = cl.begin(); it != cl.end(); ++it)
[10386]28    {
29        addChannel(it->get()->clone());
30    }
[9093]31}
32
33
34void Animation::addChannel(Channel* pChannel)
35{
36    _channels.push_back(pChannel);
[9151]37    if (!_duration)
38        computeDuration();
39    else
40        _originalDuration = computeDurationFromChannels();
[9093]41}
42
[9151]43double Animation::computeDurationFromChannels() const
[9093]44{
[9151]45    double tmin = 1e5;
46    double tmax = -1e5;
[9093]47    ChannelList::const_iterator chan;
48    for( chan=_channels.begin(); chan!=_channels.end(); chan++ )
49    {
50        float min = (*chan)->getStartTime();
51        if (min < tmin)
52            tmin = min;
53        float max = (*chan)->getEndTime();
54        if (max > tmax)
55            tmax = max;
56    }
[9151]57    return tmax-tmin;
[9093]58}
59
[9151]60void Animation::computeDuration()
61{
62    _duration = computeDurationFromChannels();
63    _originalDuration = _duration;
64}
65
[9093]66osgAnimation::ChannelList& Animation::getChannels()
67{
68    return _channels;
69}
70
71const osgAnimation::ChannelList& Animation::getChannels() const
72{
73    return _channels;
74}
75
76
[9151]77void Animation::setDuration(double duration)
[9093]78{
[9151]79    _originalDuration = computeDurationFromChannels();
80    _duration = duration;
[9093]81}
82
[9151]83float Animation::getDuration() const
[9093]84{
[9151]85    return _duration;
[9093]86}
87
88float Animation::getWeight () const
89{
90    return _weight;
91}
92
93void Animation::setWeight (float weight)
94{
95    _weight = weight;
96}
97
[10556]98bool Animation::update (float time, int priority)
[9093]99{
[9151]100    if (!_duration) // if not initialized then do it
101        computeDuration();
102
103    double ratio = _originalDuration / _duration;
104
105    float t = (time - _startTime) * ratio;
[9093]106    switch (_playmode)
107    {
108    case ONCE:
109        if (t > _duration)
110            return false;
111        break;
112    case STAY:
113        if (t > _duration)
114            t = _duration;
115        break;
116    case LOOP:
117        if (!_duration)
118            t = _startTime;
119        else if (t > _duration)
[9569]120            t = fmod(t, (float)_duration);
[9093]121        //      std::cout << "t " << t << " duration " << _duration << std::endl;
122        break;
123    case PPONG:
124        if (!_duration)
125            t = _startTime;
126        else 
127        {
128            int tt = (int) (t / _duration);
[9569]129            t = fmod(t, (float)_duration);
[9093]130            if (tt%2)
131                t = _duration - t;
132        }
133        break;
134    }
135
136    ChannelList::const_iterator chan;
[10344]137    for( chan=_channels.begin(); chan!=_channels.end(); ++chan)
[9093]138    {
[10556]139        (*chan)->update(t, _weight, priority);
[9093]140    }
141    return true;
142}
143
144void Animation::resetTargets()
145{
146    ChannelList::const_iterator chan;
147    for( chan=_channels.begin(); chan!=_channels.end(); ++chan)
148        (*chan)->reset();
149}
Note: See TracBrowser for help on using the browser.