| [9093] | 1 | /* -*-c++-*- |
|---|
| [10527] | 2 | * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net> |
|---|
| [9093] | 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. |
|---|
| [10527] | 13 | * |
|---|
| 14 | * Authors: |
|---|
| 15 | * Cedric Pinson <cedric.pinson@plopbyte.net> |
|---|
| 16 | * Michael Platings <mplatings@pixelpower.com> |
|---|
| 17 | */ |
|---|
| [9093] | 18 | |
|---|
| 19 | #ifndef OSGANIMATION_CHANNEL_H |
|---|
| 20 | #define OSGANIMATION_CHANNEL_H |
|---|
| 21 | |
|---|
| [9094] | 22 | #include <osgAnimation/Export> |
|---|
| [9093] | 23 | #include <osgAnimation/Sampler> |
|---|
| 24 | #include <osgAnimation/Target> |
|---|
| 25 | #include <osg/Referenced> |
|---|
| 26 | #include <string> |
|---|
| 27 | |
|---|
| 28 | namespace osgAnimation |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| [9094] | 31 | class OSGANIMATION_EXPORT Channel : public osg::Referenced |
|---|
| [9093] | 32 | { |
|---|
| 33 | public: |
|---|
| 34 | |
|---|
| 35 | Channel(); |
|---|
| [10386] | 36 | Channel(const Channel& channel); |
|---|
| [9093] | 37 | virtual ~Channel(); |
|---|
| [10386] | 38 | virtual Channel* clone() const = 0; |
|---|
| [9093] | 39 | |
|---|
| [10556] | 40 | virtual void update(float time, float weight, int priority) = 0; |
|---|
| [9093] | 41 | virtual void reset() = 0; |
|---|
| 42 | virtual Target* getTarget() = 0; |
|---|
| [10527] | 43 | virtual bool setTarget(Target*) = 0; |
|---|
| [9093] | 44 | |
|---|
| 45 | const std::string& getName() const; |
|---|
| 46 | void setName(const std::string& name); |
|---|
| 47 | |
|---|
| 48 | virtual float getStartTime() const = 0; |
|---|
| 49 | virtual float getEndTime() const = 0; |
|---|
| 50 | |
|---|
| 51 | const std::string& getTargetName() const; |
|---|
| 52 | void setTargetName(const std::string& name); |
|---|
| 53 | |
|---|
| 54 | virtual Sampler* getSampler() = 0; |
|---|
| 55 | virtual const Sampler* getSampler() const = 0; |
|---|
| 56 | |
|---|
| [10576] | 57 | // create a keyframe container from current target value |
|---|
| 58 | // with one key only, can be used for debug or to create |
|---|
| 59 | // easily a default channel from an existing one |
|---|
| 60 | virtual bool createKeyframeContainerFromTargetValue() = 0; |
|---|
| 61 | |
|---|
| [9093] | 62 | protected: |
|---|
| 63 | |
|---|
| 64 | std::string _targetName; |
|---|
| 65 | std::string _name; |
|---|
| 66 | }; |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | template <typename SamplerType> |
|---|
| 70 | class TemplateChannel : public Channel |
|---|
| 71 | { |
|---|
| 72 | public: |
|---|
| 73 | |
|---|
| 74 | typedef typename SamplerType::UsingType UsingType; |
|---|
| 75 | typedef TemplateTarget<UsingType> TargetType; |
|---|
| 76 | typedef TemplateKeyframeContainer<typename SamplerType::KeyframeType> KeyframeContainerType; |
|---|
| [10386] | 77 | Channel* clone() const { return new TemplateChannel<SamplerType>(*this); } |
|---|
| [9093] | 78 | |
|---|
| [10576] | 79 | TemplateChannel (const TemplateChannel& channel) : |
|---|
| [10656] | 80 | Channel(channel) |
|---|
| [9093] | 81 | { |
|---|
| [10656] | 82 | if (channel.getTargetTyped()) |
|---|
| 83 | _target = new TargetType(*channel.getTargetTyped()); |
|---|
| 84 | |
|---|
| 85 | if (channel.getSamplerTyped()) |
|---|
| 86 | _sampler = new SamplerType(*channel.getSamplerTyped()); |
|---|
| [10386] | 87 | } |
|---|
| 88 | |
|---|
| 89 | TemplateChannel (SamplerType* s = 0,TargetType* target = 0) |
|---|
| 90 | { |
|---|
| [9093] | 91 | if (target) |
|---|
| 92 | _target = target; |
|---|
| 93 | else |
|---|
| 94 | _target = new TargetType; |
|---|
| 95 | _sampler = s; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| [10576] | 98 | virtual bool createKeyframeContainerFromTargetValue() |
|---|
| 99 | { |
|---|
| 100 | if (!_target.valid()) // no target it does not make sense to do it |
|---|
| 101 | { |
|---|
| 102 | return false; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // create a key from current target value |
|---|
| 106 | typename KeyframeContainerType::KeyType key(0, _target->getValue()); |
|---|
| 107 | // recreate the keyframe container |
|---|
| [10656] | 108 | getOrCreateSampler()->setKeyframeContainer(0); |
|---|
| [10576] | 109 | getOrCreateSampler()->getOrCreateKeyframeContainer(); |
|---|
| 110 | // add the key |
|---|
| 111 | _sampler->getKeyframeContainerTyped()->push_back(key); |
|---|
| 112 | return true; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| [9093] | 115 | virtual ~TemplateChannel() {} |
|---|
| [10556] | 116 | virtual void update(float time, float weight, int priority) |
|---|
| [9093] | 117 | { |
|---|
| [9119] | 118 | // skip if weight == 0 |
|---|
| [10556] | 119 | if (weight < 1e-4) |
|---|
| [9119] | 120 | return; |
|---|
| [9093] | 121 | typename SamplerType::UsingType value; |
|---|
| 122 | _sampler->getValueAt(time, value); |
|---|
| [10556] | 123 | _target->update(weight, value, priority); |
|---|
| [9093] | 124 | } |
|---|
| 125 | virtual void reset() { _target->reset(); } |
|---|
| 126 | virtual Target* getTarget() { return _target.get();} |
|---|
| [10527] | 127 | virtual bool setTarget(Target* target) |
|---|
| 128 | { |
|---|
| 129 | _target = dynamic_cast<TargetType*>(target); |
|---|
| 130 | return _target.get() == target; |
|---|
| 131 | } |
|---|
| [9093] | 132 | |
|---|
| [10527] | 133 | SamplerType* getOrCreateSampler() |
|---|
| [9093] | 134 | { |
|---|
| 135 | if (!_sampler.valid()) |
|---|
| 136 | _sampler = new SamplerType; |
|---|
| 137 | return _sampler.get(); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | Sampler* getSampler() { return _sampler.get(); } |
|---|
| 141 | const Sampler* getSampler() const { return _sampler.get(); } |
|---|
| 142 | |
|---|
| 143 | SamplerType* getSamplerTyped() { return _sampler.get();} |
|---|
| 144 | const SamplerType* getSamplerTyped() const { return _sampler.get();} |
|---|
| 145 | void setSampler(SamplerType* sampler) { _sampler = sampler; } |
|---|
| 146 | |
|---|
| 147 | TargetType* getTargetTyped() { return _target.get(); } |
|---|
| [10576] | 148 | const TargetType* getTargetTyped() const { return _target.get(); } |
|---|
| [9093] | 149 | void setTarget(TargetType* target) { _target = target; } |
|---|
| 150 | |
|---|
| [9456] | 151 | virtual float getStartTime() const { return _sampler->getStartTime(); } |
|---|
| 152 | virtual float getEndTime() const { return _sampler->getEndTime(); } |
|---|
| [9093] | 153 | |
|---|
| 154 | protected: |
|---|
| 155 | osg::ref_ptr<TargetType> _target; |
|---|
| 156 | osg::ref_ptr<SamplerType> _sampler; |
|---|
| 157 | }; |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | typedef std::vector<osg::ref_ptr<osgAnimation::Channel> > ChannelList; |
|---|
| [10527] | 161 | |
|---|
| 162 | typedef TemplateChannel<DoubleStepSampler> DoubleStepChannel; |
|---|
| 163 | typedef TemplateChannel<FloatStepSampler> FloatStepChannel; |
|---|
| 164 | typedef TemplateChannel<Vec2StepSampler> Vec2StepChannel; |
|---|
| 165 | typedef TemplateChannel<Vec3StepSampler> Vec3StepChannel; |
|---|
| 166 | typedef TemplateChannel<Vec4StepSampler> Vec4StepChannel; |
|---|
| 167 | typedef TemplateChannel<QuatStepSampler> QuatStepChannel; |
|---|
| 168 | |
|---|
| [9093] | 169 | typedef TemplateChannel<DoubleLinearSampler> DoubleLinearChannel; |
|---|
| 170 | typedef TemplateChannel<FloatLinearSampler> FloatLinearChannel; |
|---|
| 171 | typedef TemplateChannel<Vec2LinearSampler> Vec2LinearChannel; |
|---|
| 172 | typedef TemplateChannel<Vec3LinearSampler> Vec3LinearChannel; |
|---|
| 173 | typedef TemplateChannel<Vec4LinearSampler> Vec4LinearChannel; |
|---|
| 174 | typedef TemplateChannel<QuatSphericalLinearSampler> QuatSphericalLinearChannel; |
|---|
| [11009] | 175 | typedef TemplateChannel<MatrixLinearSampler> MatrixLinearChannel; |
|---|
| [9093] | 176 | |
|---|
| 177 | typedef TemplateChannel<FloatCubicBezierSampler> FloatCubicBezierChannel; |
|---|
| 178 | typedef TemplateChannel<DoubleCubicBezierSampler> DoubleCubicBezierChannel; |
|---|
| 179 | typedef TemplateChannel<Vec2CubicBezierSampler> Vec2CubicBezierChannel; |
|---|
| 180 | typedef TemplateChannel<Vec3CubicBezierSampler> Vec3CubicBezierChannel; |
|---|
| 181 | typedef TemplateChannel<Vec4CubicBezierSampler> Vec4CubicBezierChannel; |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | #endif |
|---|