| 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& 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 | |
|---|
| 29 | void Animation::addChannel(Channel* pChannel) |
|---|
| 30 | { |
|---|
| 31 | _channels.push_back(pChannel); |
|---|
| 32 | if (!_duration) |
|---|
| 33 | computeDuration(); |
|---|
| 34 | else |
|---|
| 35 | _originalDuration = computeDurationFromChannels(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | double 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 | |
|---|
| 55 | void Animation::computeDuration() |
|---|
| 56 | { |
|---|
| 57 | _duration = computeDurationFromChannels(); |
|---|
| 58 | _originalDuration = _duration; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | osgAnimation::ChannelList& Animation::getChannels() |
|---|
| 62 | { |
|---|
| 63 | return _channels; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | const osgAnimation::ChannelList& Animation::getChannels() const |
|---|
| 67 | { |
|---|
| 68 | return _channels; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | void Animation::setDuration(double duration) |
|---|
| 73 | { |
|---|
| 74 | _originalDuration = computeDurationFromChannels(); |
|---|
| 75 | _duration = duration; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | float Animation::getDuration() const |
|---|
| 79 | { |
|---|
| 80 | return _duration; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | float Animation::getWeight () const |
|---|
| 84 | { |
|---|
| 85 | return _weight; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void Animation::setWeight (float weight) |
|---|
| 89 | { |
|---|
| 90 | _weight = weight; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | bool Animation::update (float time) |
|---|
| 94 | { |
|---|
| 95 | if (!_duration) |
|---|
| 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 = fmodf(t, _duration); |
|---|
| 116 | |
|---|
| 117 | break; |
|---|
| 118 | case PPONG: |
|---|
| 119 | if (!_duration) |
|---|
| 120 | t = _startTime; |
|---|
| 121 | else |
|---|
| 122 | { |
|---|
| 123 | int tt = (int) (t / _duration); |
|---|
| 124 | t = fmodf(t, _duration); |
|---|
| 125 | if (tt%2) |
|---|
| 126 | t = _duration - t; |
|---|
| 127 | } |
|---|
| 128 | break; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 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 | |
|---|
| 142 | void Animation::resetTargets() |
|---|
| 143 | { |
|---|
| 144 | ChannelList::const_iterator chan; |
|---|
| 145 | for( chan=_channels.begin(); chan!=_channels.end(); ++chan) |
|---|
| 146 | (*chan)->reset(); |
|---|
| 147 | } |
|---|