Animation.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Quaternion.h"
  25. #include "Resource.h"
  26. #include "Ptr.h"
  27. #include "Vector3.h"
  28. /// Skeletal animation keyframe.
  29. struct AnimationKeyFrame
  30. {
  31. /// Keyframe time.
  32. float time_;
  33. /// Bone position.
  34. Vector3 position_;
  35. /// Bone rotation.
  36. Quaternion rotation_;
  37. /// Bone scale.
  38. Vector3 scale_;
  39. };
  40. /// Skeletal animation track, stores keyframes of a single bone.
  41. struct AnimationTrack
  42. {
  43. /// Return keyframe index based on time and previous index.
  44. void GetKeyFrameIndex(float time, unsigned& index) const;
  45. /// Bone name.
  46. String name_;
  47. /// Bone name hash.
  48. StringHash nameHash_;
  49. /// Bitmask of included data (position, rotation, scale.)
  50. unsigned char channelMask_;
  51. /// Keyframes.
  52. Vector<AnimationKeyFrame> keyFrames_;
  53. };
  54. static const unsigned char CHANNEL_POSITION = 0x1;
  55. static const unsigned char CHANNEL_ROTATION = 0x2;
  56. static const unsigned char CHANNEL_SCALE = 0x4;
  57. /// Skeletal animation resource.
  58. class Animation : public Resource
  59. {
  60. OBJECT(Animation);
  61. public:
  62. /// Construct.
  63. Animation(Context* context);
  64. /// Destruct.
  65. virtual ~Animation();
  66. /// Register object factory.
  67. static void RegisterObject(Context* context);
  68. /// Load resource. Return true if successful.
  69. virtual bool Load(Deserializer& source);
  70. /// Save resource. Return true if successful.
  71. virtual bool Save(Serializer& dest);
  72. /// %Set animation name.
  73. void SetAnimationName(const String& name);
  74. /// %Set animation length.
  75. void SetLength(float length);
  76. /// %Set all animation tracks.
  77. void SetTracks(const Vector<AnimationTrack>& tracks);
  78. /// Return animation name.
  79. const String& GetAnimationName() const { return animationName_; }
  80. /// Return animation name hash.
  81. StringHash GetAnimationNameHash() const { return animationNameHash_; }
  82. /// Return animation length.
  83. float GetLength() const { return length_; }
  84. /// Return all animation tracks.
  85. const Vector<AnimationTrack>& GetTracks() const { return tracks_; }
  86. /// Return number of animation tracks.
  87. unsigned GetNumTracks() const;
  88. /// Return animation track by index.
  89. const AnimationTrack* GetTrack(unsigned index) const;
  90. /// Return animation track by bone name.
  91. const AnimationTrack* GetTrack(const String& name) const;
  92. /// Return animation track by bone name hash.
  93. const AnimationTrack* GetTrack(StringHash nameHash) const;
  94. private:
  95. /// Animation name.
  96. String animationName_;
  97. /// Animation name hash.
  98. StringHash animationNameHash_;
  99. /// Animation length.
  100. float length_;
  101. /// Animation tracks.
  102. Vector<AnimationTrack> tracks_;
  103. };