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

Revision 9569, 3.5 kB (checked in by robert, 6 years ago)

From Mathias Froehlich, "I have today built svn trunk on HP-UX and Solaris8.
I had to do two small changes to src/osgWidget/Input.cpp and
src/osgAnimation/Animation.cpp mainly because of name lookup problems.
"

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