Animation.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_RESOURCE_ANIMATION_H
  6. #define ANKI_RESOURCE_ANIMATION_H
  7. #include "anki/Math.h"
  8. #include "anki/resource/Common.h"
  9. #include "anki/util/String.h"
  10. namespace anki {
  11. class XmlElement;
  12. /// @addtogroup resource
  13. /// @{
  14. /// A keyframe
  15. template<typename T>
  16. class Key
  17. {
  18. friend class Animation;
  19. public:
  20. F32 getTime() const
  21. {
  22. return m_time;
  23. }
  24. const T& getValue() const
  25. {
  26. return m_value;
  27. }
  28. private:
  29. F32 m_time;
  30. T m_value;
  31. };
  32. /// Animation channel
  33. class AnimationChannel
  34. {
  35. public:
  36. ResourceString m_name;
  37. I32 m_boneIndex = -1; ///< For skeletal animations
  38. ResourceDArray<Key<Vec3>> m_positions;
  39. ResourceDArray<Key<Quat>> m_rotations;
  40. ResourceDArray<Key<F32>> m_scales;
  41. ResourceDArray<Key<F32>> m_cameraFovs;
  42. void destroy(ResourceAllocator<U8>& alloc)
  43. {
  44. m_name.destroy(alloc);
  45. m_positions.destroy(alloc);
  46. m_rotations.destroy(alloc);
  47. m_scales.destroy(alloc);
  48. m_cameraFovs.destroy(alloc);
  49. }
  50. };
  51. /// Animation consists of keyframe data
  52. class Animation
  53. {
  54. public:
  55. Animation(ResourceAllocator<U8>& alloc);
  56. ~Animation();
  57. ANKI_USE_RESULT Error load(
  58. const CString& filename, ResourceInitializer& init);
  59. /// Get a vector of all animation channels
  60. const ResourceDArray<AnimationChannel>& getChannels() const
  61. {
  62. return m_channels;
  63. }
  64. /// Get the duration of the animation in seconds
  65. F32 getDuration() const
  66. {
  67. return m_duration;
  68. }
  69. /// Get the time (in seconds) the animation should start
  70. F32 getStartingTime() const
  71. {
  72. return m_startTime;
  73. }
  74. /// The animation repeats
  75. Bool getRepeat() const
  76. {
  77. return m_repeat;
  78. }
  79. /// Get the interpolated data
  80. void interpolate(U channelIndex, F32 time,
  81. Vec3& position, Quat& rotation, F32& scale) const;
  82. private:
  83. ResourceAllocator<U8> m_alloc;
  84. ResourceDArray<AnimationChannel> m_channels;
  85. F32 m_duration;
  86. F32 m_startTime;
  87. Bool8 m_repeat;
  88. void loadInternal(const XmlElement& el, ResourceInitializer& init);
  89. };
  90. /// @}
  91. } // end namespace anki
  92. #endif