Skeleton.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef ANKI_RESOURCE_SKELETON_H
  2. #define ANKI_RESOURCE_SKELETON_H
  3. #include "anki/math/Math.h"
  4. #include "anki/util/Vector.h"
  5. #include <array>
  6. namespace anki {
  7. /// Skeleton bone
  8. ///
  9. /// @note The rotation and translation that transform the bone from bone space
  10. /// to armature space. Meaning that if MA = TRS(rotSkelSpace, tslSkelSpace)
  11. /// then head = MA * Vec3(0.0, length, 0.0) and tail = MA * Vec3(0.0, 0.0, 0.0).
  12. /// We need the MA because the animation rotations and translations are in bone
  13. /// space. We also keep the inverted ones for fast calculations.
  14. /// rotSkelSpaceInv = MA.Inverted().getRotationPart() and NOT
  15. /// rotSkelSpaceInv = rotSkelSpace.getInverted()
  16. struct Bone
  17. {
  18. friend class Skeleton; /// For loading
  19. public:
  20. /// @name Accessors
  21. /// @{
  22. const std::string& getName() const
  23. {
  24. return name;
  25. }
  26. const Vec3& getHead() const
  27. {
  28. return head;
  29. }
  30. const Vec3& getTail() const
  31. {
  32. return tail;
  33. }
  34. uint getId() const
  35. {
  36. return id;
  37. }
  38. const Bone* getParent() const
  39. {
  40. return parent;
  41. }
  42. const Bone& getChild(uint i) const
  43. {
  44. return *childs[i];
  45. }
  46. size_t getChildsNum() const
  47. {
  48. return childsNum;
  49. }
  50. const Mat3& getRotationSkeletonSpace() const
  51. {
  52. return rotSkelSpace;
  53. }
  54. const Vec3& getTranslationSkeletonSpace() const
  55. {
  56. return tslSkelSpace;
  57. }
  58. const Mat3& getRotationSkeletonSpaceInverted() const
  59. {
  60. return rotSkelSpaceInv;
  61. }
  62. const Vec3& getTranslationSkeletonSpaceInverted() const
  63. {
  64. return tslSkelSpaceInv;
  65. }
  66. /// @}
  67. private:
  68. std::string name; ///< The name of the bone
  69. Vec3 head; ///< Starting point of the bone
  70. Vec3 tail; ///< End point of the bone
  71. uint id; ///< Pos inside the @ref Skeleton::bones vector
  72. static const uint MAX_CHILDS_PER_BONE = 4; ///< Please dont change this
  73. Bone* parent;
  74. std::array<Bone*, MAX_CHILDS_PER_BONE> childs;
  75. ushort childsNum;
  76. // see the class notes
  77. Mat3 rotSkelSpace;
  78. Vec3 tslSkelSpace;
  79. Mat3 rotSkelSpaceInv;
  80. Vec3 tslSkelSpaceInv;
  81. };
  82. /// It contains the bones with their position and hierarchy
  83. ///
  84. /// Binary file format:
  85. ///
  86. /// @code
  87. /// magic: ANKISKEL
  88. /// uint: bones num
  89. /// n * bone: bones
  90. ///
  91. /// bone:
  92. /// string: name
  93. /// 3 * floats: head
  94. /// 3 * floats: tail
  95. /// 16 * floats: armature space matrix
  96. /// uint: parent id, if it has no parent then its 0xFFFFFFFF
  97. /// uint: children number
  98. /// n * child: children
  99. ///
  100. /// child:
  101. /// uint: bone id
  102. /// @endcode
  103. class Skeleton
  104. {
  105. public:
  106. /// Implements Resource::load
  107. void load(const char* filename);
  108. /// @name Accessors
  109. /// @{
  110. const Vector<Bone>& getBones() const
  111. {
  112. return bones;
  113. }
  114. /// @}
  115. private:
  116. Vector<Bone> bones;
  117. };
  118. } // end namespace
  119. #endif