| [9093] | 1 | |
|---|
| [10556] | 2 | |
|---|
| [9093] | 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgAnimation/Animation> |
|---|
| 16 | |
|---|
| 17 | using namespace osgAnimation; |
|---|
| 18 | |
|---|
| [10393] | 19 | Animation::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 | |
|---|
| 34 | void 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] | 43 | double 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] | 60 | void Animation::computeDuration() |
|---|
| 61 | { |
|---|
| 62 | _duration = computeDurationFromChannels(); |
|---|
| 63 | _originalDuration = _duration; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| [9093] | 66 | osgAnimation::ChannelList& Animation::getChannels() |
|---|
| 67 | { |
|---|
| 68 | return _channels; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | const osgAnimation::ChannelList& Animation::getChannels() const |
|---|
| 72 | { |
|---|
| 73 | return _channels; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| [9151] | 77 | void Animation::setDuration(double duration) |
|---|
| [9093] | 78 | { |
|---|
| [9151] | 79 | _originalDuration = computeDurationFromChannels(); |
|---|
| 80 | _duration = duration; |
|---|
| [9093] | 81 | } |
|---|
| 82 | |
|---|
| [9151] | 83 | float Animation::getDuration() const |
|---|
| [9093] | 84 | { |
|---|
| [9151] | 85 | return _duration; |
|---|
| [9093] | 86 | } |
|---|
| 87 | |
|---|
| 88 | float Animation::getWeight () const |
|---|
| 89 | { |
|---|
| 90 | return _weight; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | void Animation::setWeight (float weight) |
|---|
| 94 | { |
|---|
| 95 | _weight = weight; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| [10556] | 98 | bool Animation::update (float time, int priority) |
|---|
| [9093] | 99 | { |
|---|
| [9151] | 100 | if (!_duration) |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 144 | void Animation::resetTargets() |
|---|
| 145 | { |
|---|
| 146 | ChannelList::const_iterator chan; |
|---|
| 147 | for( chan=_channels.begin(); chan!=_channels.end(); ++chan) |
|---|
| 148 | (*chan)->reset(); |
|---|
| 149 | } |
|---|