| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of OGRE |
|---|
| 4 | (Object-oriented Graphics Rendering Engine) |
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
|---|
| 6 | |
|---|
| 7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
|---|
| 8 | |
|---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
|---|
| 11 | in the Software without restriction, including without limitation the rights |
|---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
|---|
| 14 | furnished to do so, subject to the following conditions: |
|---|
| 15 | |
|---|
| 16 | The above copyright notice and this permission notice shall be included in |
|---|
| 17 | all copies or substantial portions of the Software. |
|---|
| 18 | |
|---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 25 | THE SOFTWARE. |
|---|
| 26 | ----------------------------------------------------------------------------- |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #ifndef __AnimationSet_H__ |
|---|
| 30 | #define __AnimationSet_H__ |
|---|
| 31 | |
|---|
| 32 | #include "OgrePrerequisites.h" |
|---|
| 33 | |
|---|
| 34 | #include "OgreString.h" |
|---|
| 35 | #include "OgreController.h" |
|---|
| 36 | #include "OgreIteratorWrappers.h" |
|---|
| 37 | #include "Threading/OgreThreadHeaders.h" |
|---|
| 38 | #include "OgreHeaderPrefix.h" |
|---|
| 39 | |
|---|
| 40 | namespace Ogre { |
|---|
| 41 | |
|---|
| 42 | /** \addtogroup Core |
|---|
| 43 | * @{ |
|---|
| 44 | */ |
|---|
| 45 | /** \addtogroup Animation |
|---|
| 46 | * @{ |
|---|
| 47 | */ |
|---|
| 48 | |
|---|
| 49 | /** Represents the state of an animation and the weight of its influence. |
|---|
| 50 | @remarks |
|---|
| 51 | Other classes can hold instances of this class to store the state of any animations |
|---|
| 52 | they are using. |
|---|
| 53 | */ |
|---|
| 54 | class _OgreExport AnimationState : public AnimationAlloc |
|---|
| 55 | { |
|---|
| 56 | public: |
|---|
| 57 | |
|---|
| 58 | /// Typedef for an array of float values used as a bone blend mask |
|---|
| 59 | typedef vector<float>::type BoneBlendMask; |
|---|
| 60 | |
|---|
| 61 | /** Normal constructor with all params supplied |
|---|
| 62 | @param |
|---|
| 63 | animName The name of this state. |
|---|
| 64 | @param |
|---|
| 65 | parent The parent AnimationStateSet that this state will belong to. |
|---|
| 66 | @param |
|---|
| 67 | timePos The position, in seconds, where this state will begin. |
|---|
| 68 | @param |
|---|
| 69 | length The length, in seconds, of this animation state. |
|---|
| 70 | @param |
|---|
| 71 | weight Weight to apply the animation state with. |
|---|
| 72 | @param |
|---|
| 73 | enabled Whether the animation state is enabled. |
|---|
| 74 | */ |
|---|
| 75 | AnimationState(const String& animName, AnimationStateSet *parent, |
|---|
| 76 | Real timePos, Real length, Real weight = 1.0, bool enabled = false); |
|---|
| 77 | /// Constructor to copy from an existing state with new parent |
|---|
| 78 | AnimationState(AnimationStateSet* parent, const AnimationState &rhs); |
|---|
| 79 | virtual ~AnimationState(); |
|---|
| 80 | |
|---|
| 81 | /// Gets the name of the animation to which this state applies |
|---|
| 82 | const String& getAnimationName() const; |
|---|
| 83 | /// Gets the time position for this animation |
|---|
| 84 | Real getTimePosition(void) const; |
|---|
| 85 | /// Sets the time position for this animation |
|---|
| 86 | void setTimePosition(Real timePos); |
|---|
| 87 | /// Gets the total length of this animation (may be shorter than whole animation) |
|---|
| 88 | Real getLength() const; |
|---|
| 89 | /// Sets the total length of this animation (may be shorter than whole animation) |
|---|
| 90 | void setLength(Real len); |
|---|
| 91 | /// Gets the weight (influence) of this animation |
|---|
| 92 | Real getWeight(void) const; |
|---|
| 93 | /// Sets the weight (influence) of this animation |
|---|
| 94 | void setWeight(Real weight); |
|---|
| 95 | /** Modifies the time position, adjusting for animation length |
|---|
| 96 | @param offset The amount of time, in seconds, to extend the animation. |
|---|
| 97 | @remarks |
|---|
| 98 | This method loops at the edges if animation looping is enabled. |
|---|
| 99 | */ |
|---|
| 100 | void addTime(Real offset); |
|---|
| 101 | |
|---|
| 102 | /// Returns true if the animation has reached the end and is not looping |
|---|
| 103 | bool hasEnded(void) const; |
|---|
| 104 | |
|---|
| 105 | /// Returns true if this animation is currently enabled |
|---|
| 106 | bool getEnabled(void) const; |
|---|
| 107 | /// Sets whether this animation is enabled |
|---|
| 108 | void setEnabled(bool enabled); |
|---|
| 109 | |
|---|
| 110 | /// Equality operator |
|---|
| 111 | bool operator==(const AnimationState& rhs) const; |
|---|
| 112 | /// Inequality operator |
|---|
| 113 | bool operator!=(const AnimationState& rhs) const; |
|---|
| 114 | |
|---|
| 115 | /** Sets whether or not an animation loops at the start and end of |
|---|
| 116 | the animation if the time continues to be altered. |
|---|
| 117 | */ |
|---|
| 118 | void setLoop(bool loop) { mLoop = loop; } |
|---|
| 119 | /// Gets whether or not this animation loops |
|---|
| 120 | bool getLoop(void) const { return mLoop; } |
|---|
| 121 | |
|---|
| 122 | /** Copies the states from another animation state, preserving the animation name |
|---|
| 123 | (unlike operator=) but copying everything else. |
|---|
| 124 | @param animState Reference to animation state which will use as source. |
|---|
| 125 | */ |
|---|
| 126 | void copyStateFrom(const AnimationState& animState); |
|---|
| 127 | |
|---|
| 128 | /// Get the parent animation state set |
|---|
| 129 | AnimationStateSet* getParent(void) const { return mParent; } |
|---|
| 130 | |
|---|
| 131 | /** @brief Create a new blend mask with the given number of entries |
|---|
| 132 | * |
|---|
| 133 | * In addition to assigning a single weight value to a skeletal animation, |
|---|
| 134 | * it may be desirable to assign animation weights per bone using a 'blend mask'. |
|---|
| 135 | * |
|---|
| 136 | * @param blendMaskSizeHint |
|---|
| 137 | * The number of bones of the skeleton owning this AnimationState. |
|---|
| 138 | * @param initialWeight |
|---|
| 139 | * The value all the blend mask entries will be initialised with (negative to skip initialisation) |
|---|
| 140 | */ |
|---|
| 141 | void createBlendMask(size_t blendMaskSizeHint, float initialWeight = 1.0f); |
|---|
| 142 | /// Destroy the currently set blend mask |
|---|
| 143 | void destroyBlendMask(); |
|---|
| 144 | /** @brief Set the blend mask data (might be dangerous) |
|---|
| 145 | * |
|---|
| 146 | * @par The size of the array should match the number of entries the |
|---|
| 147 | * blend mask was created with. |
|---|
| 148 | * |
|---|
| 149 | * @par Stick to the setBlendMaskEntry method if you don't know exactly what you're doing. |
|---|
| 150 | */ |
|---|
| 151 | void _setBlendMaskData(const float* blendMaskData); |
|---|
| 152 | /** @brief Set the blend mask |
|---|
| 153 | * |
|---|
| 154 | * @par The size of the array should match the number of entries the |
|---|
| 155 | * blend mask was created with. |
|---|
| 156 | * |
|---|
| 157 | * @par Stick to the setBlendMaskEntry method if you don't know exactly what you're doing. |
|---|
| 158 | */ |
|---|
| 159 | void _setBlendMask(const BoneBlendMask* blendMask); |
|---|
| 160 | /// Get the current blend mask (const version, may be 0) |
|---|
| 161 | const BoneBlendMask* getBlendMask() const {return mBlendMask;} |
|---|
| 162 | /// Return whether there is currently a valid blend mask set |
|---|
| 163 | bool hasBlendMask() const {return mBlendMask != 0;} |
|---|
| 164 | /// Set the weight for the bone identified by the given handle |
|---|
| 165 | void setBlendMaskEntry(size_t boneHandle, float weight); |
|---|
| 166 | /// Get the weight for the bone identified by the given handle |
|---|
| 167 | inline float getBlendMaskEntry(size_t boneHandle) const |
|---|
| 168 | { |
|---|
| 169 | assert(mBlendMask && mBlendMask->size() > boneHandle); |
|---|
| 170 | return (*mBlendMask)[boneHandle]; |
|---|
| 171 | } |
|---|
| 172 | protected: |
|---|
| 173 | /// The blend mask (containing per bone weights) |
|---|
| 174 | BoneBlendMask* mBlendMask; |
|---|
| 175 | |
|---|
| 176 | String mAnimationName; |
|---|
| 177 | AnimationStateSet* mParent; |
|---|
| 178 | Real mTimePos; |
|---|
| 179 | Real mLength; |
|---|
| 180 | Real mWeight; |
|---|
| 181 | bool mEnabled; |
|---|
| 182 | bool mLoop; |
|---|
| 183 | |
|---|
| 184 | }; |
|---|
| 185 | |
|---|
| 186 | // A map of animation states |
|---|
| 187 | typedef map<String, AnimationState*>::type AnimationStateMap; |
|---|
| 188 | typedef MapIterator<AnimationStateMap> AnimationStateIterator; |
|---|
| 189 | typedef ConstMapIterator<AnimationStateMap> ConstAnimationStateIterator; |
|---|
| 190 | // A list of enabled animation states |
|---|
| 191 | typedef list<AnimationState*>::type EnabledAnimationStateList; |
|---|
| 192 | typedef ConstVectorIterator<EnabledAnimationStateList> ConstEnabledAnimationStateIterator; |
|---|
| 193 | |
|---|
| 194 | /** Class encapsulating a set of AnimationState objects. |
|---|
| 195 | */ |
|---|
| 196 | class _OgreExport AnimationStateSet : public AnimationAlloc |
|---|
| 197 | { |
|---|
| 198 | public: |
|---|
| 199 | /// Mutex, public for external locking if needed |
|---|
| 200 | OGRE_AUTO_MUTEX; |
|---|
| 201 | /// Create a blank animation state set |
|---|
| 202 | AnimationStateSet(); |
|---|
| 203 | /// Create an animation set by copying the contents of another |
|---|
| 204 | AnimationStateSet(const AnimationStateSet& rhs); |
|---|
| 205 | |
|---|
| 206 | ~AnimationStateSet(); |
|---|
| 207 | |
|---|
| 208 | /** Create a new AnimationState instance. |
|---|
| 209 | @param animName The name of the animation |
|---|
| 210 | @param timePos Starting time position |
|---|
| 211 | @param length Length of the animation to play |
|---|
| 212 | @param weight Weight to apply the animation with |
|---|
| 213 | @param enabled Whether the animation is enabled |
|---|
| 214 | */ |
|---|
| 215 | AnimationState* createAnimationState(const String& animName, |
|---|
| 216 | Real timePos, Real length, Real weight = 1.0, bool enabled = false); |
|---|
| 217 | /// Get an animation state by the name of the animation |
|---|
| 218 | AnimationState* getAnimationState(const String& name) const; |
|---|
| 219 | /// Tests if state for the named animation is present |
|---|
| 220 | bool hasAnimationState(const String& name) const; |
|---|
| 221 | /// Remove animation state with the given name |
|---|
| 222 | void removeAnimationState(const String& name); |
|---|
| 223 | /// Remove all animation states |
|---|
| 224 | void removeAllAnimationStates(void); |
|---|
| 225 | |
|---|
| 226 | /** Get an iterator over all the animation states in this set. |
|---|
| 227 | @note |
|---|
| 228 | The iterator returned from this method is not threadsafe, |
|---|
| 229 | you will need to manually lock the public mutex on this |
|---|
| 230 | class to ensure thread safety if you need it. |
|---|
| 231 | */ |
|---|
| 232 | AnimationStateIterator getAnimationStateIterator(void); |
|---|
| 233 | /** Get an iterator over all the animation states in this set. |
|---|
| 234 | @note |
|---|
| 235 | The iterator returned from this method is not threadsafe, |
|---|
| 236 | you will need to manually lock the public mutex on this |
|---|
| 237 | class to ensure thread safety if you need it. |
|---|
| 238 | */ |
|---|
| 239 | ConstAnimationStateIterator getAnimationStateIterator(void) const; |
|---|
| 240 | /// Copy the state of any matching animation states from this to another |
|---|
| 241 | void copyMatchingState(AnimationStateSet* target) const; |
|---|
| 242 | /// Set the dirty flag and dirty frame number on this state set |
|---|
| 243 | void _notifyDirty(void); |
|---|
| 244 | /// Get the latest animation state been altered frame number |
|---|
| 245 | unsigned long getDirtyFrameNumber(void) const { return mDirtyFrameNumber; } |
|---|
| 246 | |
|---|
| 247 | /// Internal method respond to enable/disable an animation state |
|---|
| 248 | void _notifyAnimationStateEnabled(AnimationState* target, bool enabled); |
|---|
| 249 | /// Tests if exists enabled animation state in this set |
|---|
| 250 | bool hasEnabledAnimationState(void) const { return !mEnabledAnimationStates.empty(); } |
|---|
| 251 | /** Get an iterator over all the enabled animation states in this set |
|---|
| 252 | @note |
|---|
| 253 | The iterator returned from this method is not threadsafe, |
|---|
| 254 | you will need to manually lock the public mutex on this |
|---|
| 255 | class to ensure thread safety if you need it. |
|---|
| 256 | */ |
|---|
| 257 | ConstEnabledAnimationStateIterator getEnabledAnimationStateIterator(void) const; |
|---|
| 258 | |
|---|
| 259 | protected: |
|---|
| 260 | unsigned long mDirtyFrameNumber; |
|---|
| 261 | AnimationStateMap mAnimationStates; |
|---|
| 262 | EnabledAnimationStateList mEnabledAnimationStates; |
|---|
| 263 | |
|---|
| 264 | }; |
|---|
| 265 | |
|---|
| 266 | /** ControllerValue wrapper class for AnimationState. |
|---|
| 267 | @remarks |
|---|
| 268 | In Azathoth and earlier, AnimationState was a ControllerValue but this |
|---|
| 269 | actually causes memory problems since Controllers delete their values |
|---|
| 270 | automatically when there are no further references to them, but AnimationState |
|---|
| 271 | is deleted explicitly elsewhere so this causes double-free problems. |
|---|
| 272 | This wrapper acts as a bridge and it is this which is destroyed automatically. |
|---|
| 273 | */ |
|---|
| 274 | class _OgreExport AnimationStateControllerValue : public ControllerValue<Real> |
|---|
| 275 | { |
|---|
| 276 | protected: |
|---|
| 277 | AnimationState* mTargetAnimationState; |
|---|
| 278 | public: |
|---|
| 279 | /** Constructor, pass in the target animation state. */ |
|---|
| 280 | AnimationStateControllerValue(AnimationState* targetAnimationState) |
|---|
| 281 | : mTargetAnimationState(targetAnimationState) {} |
|---|
| 282 | /// Destructor (parent already virtual) |
|---|
| 283 | ~AnimationStateControllerValue() {} |
|---|
| 284 | /** ControllerValue implementation. */ |
|---|
| 285 | Real getValue(void) const; |
|---|
| 286 | |
|---|
| 287 | /** ControllerValue implementation. */ |
|---|
| 288 | void setValue(Real value); |
|---|
| 289 | |
|---|
| 290 | }; |
|---|
| 291 | |
|---|
| 292 | /** @} */ |
|---|
| 293 | /** @} */ |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | #include "OgreHeaderSuffix.h" |
|---|
| 297 | |
|---|
| 298 | #endif |
|---|
| 299 | |
|---|