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

Revision 13041, 3.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/*  -*-c++-*-
2 *  Copyright (C) 2008 Cedric Pinson <cedric.pinson@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& copyop): osg::Object(anim, copyop),
20    _duration(anim._duration),
21    _originalDuration(anim._originalDuration),
22    _weight(anim._weight),
23    _startTime(anim._startTime),
24    _playmode(anim._playmode)
25{
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    }
31}
32
33
34void Animation::addChannel(Channel* pChannel)
35{
36    _channels.push_back(pChannel);
37    if (_duration == _originalDuration)
38        computeDuration();
39    else
40        _originalDuration = computeDurationFromChannels();
41}
42
43double Animation::computeDurationFromChannels() const
44{
45    double tmin = 1e5;
46    double tmax = -1e5;
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    }
57    return tmax-tmin;
58}
59
60void Animation::computeDuration()
61{
62    _duration = computeDurationFromChannels();
63    _originalDuration = _duration;
64}
65
66osgAnimation::ChannelList& Animation::getChannels()
67{
68    return _channels;
69}
70
71const osgAnimation::ChannelList& Animation::getChannels() const
72{
73    return _channels;
74}
75
76
77void Animation::setDuration(double duration)
78{
79    _originalDuration = computeDurationFromChannels();
80    _duration = duration;
81}
82
83double Animation::getDuration() const
84{
85    return _duration;
86}
87
88float Animation::getWeight () const
89{
90    return _weight;
91}
92
93void Animation::setWeight (float weight)
94{
95    _weight = weight;
96}
97
98bool Animation::update (double time, int priority)
99{
100    if (!_duration) // if not initialized then do it
101        computeDuration();
102
103    double ratio = _originalDuration / _duration;
104
105    double t = (time - _startTime) * ratio;
106    switch (_playmode)
107    {
108    case ONCE:
109        if (t > _originalDuration)
110            return false;
111        break;
112    case STAY:
113        if (t > _originalDuration)
114            t = _originalDuration;
115        break;
116    case LOOP:
117        if (!_originalDuration)
118            t = _startTime;
119        else if (t > _originalDuration)
120            t = fmod(t, _originalDuration);
121        //      std::cout << "t " << t << " duration " << _duration << std::endl;
122        break;
123    case PPONG:
124        if (!_originalDuration)
125            t = _startTime;
126        else
127        {
128            int tt = (int) (t / _originalDuration);
129            t = fmod(t, _originalDuration);
130            if (tt%2)
131                t = _originalDuration - t;
132        }
133        break;
134    }
135
136    ChannelList::const_iterator chan;
137    for( chan=_channels.begin(); chan!=_channels.end(); ++chan)
138    {
139        (*chan)->update(t, _weight, priority);
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.