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

Revision 10656, 3.6 kB (checked in by cedricpinson, 4 years ago)

From Cedric Pinson, this commit contains the following change:
* Change ref_ptr to observer_ptr to avoid cross reference and leak in Skinning
* Set invalidate to true to re run the check visitor in Skeleton
* Shallow copy Sampler in channel copy constructor
* Add accessor in VertexInfluence?
* Remove dead code in Timeline.cpp
* Dont force linking in Bone::UpdateBone?, the decision is done by the user or the manager
* Add offset in timeline stats to display each manager on the screen
* Add a flag in animation manager base to enable or not automatic link when modifying the manager

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();
27    for (ChannelList::const_iterator it = cl.begin(); it != cl.end(); it++)
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.