| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | #include <osgAnimation/Animation> |
|---|
| 16 | |
|---|
| 17 | using namespace osgAnimation; |
|---|
| 18 | |
|---|
| 19 | Animation::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 | |
|---|
| 34 | void Animation::addChannel(Channel* pChannel) |
|---|
| 35 | { |
|---|
| 36 | _channels.push_back(pChannel); |
|---|
| 37 | if (_duration == _originalDuration) |
|---|
| 38 | computeDuration(); |
|---|
| 39 | else |
|---|
| 40 | _originalDuration = computeDurationFromChannels(); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | double 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 | |
|---|
| 60 | void Animation::computeDuration() |
|---|
| 61 | { |
|---|
| 62 | _duration = computeDurationFromChannels(); |
|---|
| 63 | _originalDuration = _duration; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 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 | |
|---|
| 77 | void Animation::setDuration(double duration) |
|---|
| 78 | { |
|---|
| 79 | _originalDuration = computeDurationFromChannels(); |
|---|
| 80 | _duration = duration; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | double Animation::getDuration() const |
|---|
| 84 | { |
|---|
| 85 | return _duration; |
|---|
| 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 | |
|---|
| 98 | bool Animation::update (double time, int priority) |
|---|
| 99 | { |
|---|
| 100 | if (!_duration) |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 144 | void Animation::resetTargets() |
|---|
| 145 | { |
|---|
| 146 | ChannelList::const_iterator chan; |
|---|
| 147 | for( chan=_channels.begin(); chan!=_channels.end(); ++chan) |
|---|
| 148 | (*chan)->reset(); |
|---|
| 149 | } |
|---|