SkeletonResource.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Resource/ResourceObject.h>
  7. #include <AnKi/Math.h>
  8. #include <AnKi/Util/WeakArray.h>
  9. namespace anki
  10. {
  11. /// @addtogroup resource
  12. /// @{
  13. const U32 MAX_CHILDREN_PER_BONE = 8;
  14. /// Skeleton bone
  15. class Bone
  16. {
  17. friend class SkeletonResource; ///< For loading
  18. public:
  19. Bone() = default;
  20. ~Bone() = default;
  21. const String& getName() const
  22. {
  23. return m_name;
  24. }
  25. const Mat4& getTransform() const
  26. {
  27. return m_transform;
  28. }
  29. const Mat4& getVertexTransform() const
  30. {
  31. return m_vertTrf;
  32. }
  33. U32 getIndex() const
  34. {
  35. return m_idx;
  36. }
  37. ConstWeakArray<Bone*> getChildren() const
  38. {
  39. return ConstWeakArray<Bone*>((m_childrenCount) ? &m_children[0] : nullptr, m_childrenCount);
  40. }
  41. const Bone* getParent() const
  42. {
  43. return m_parent;
  44. }
  45. private:
  46. String m_name; ///< The name of the bone
  47. Mat4 m_transform; ///< See the class notes.
  48. Mat4 m_vertTrf;
  49. U32 m_idx;
  50. Bone* m_parent = nullptr;
  51. Array<Bone*, MAX_CHILDREN_PER_BONE> m_children = {};
  52. U8 m_childrenCount = 0;
  53. void destroy(ResourceAllocator<U8> alloc)
  54. {
  55. m_name.destroy(alloc);
  56. }
  57. };
  58. /// It contains the bones with their position and hierarchy
  59. ///
  60. /// XML file format:
  61. ///
  62. /// @code
  63. /// <skeleton>
  64. /// <bones>
  65. /// <bone>
  66. /// <name>X</name>
  67. /// <transform>16 floats</transform>
  68. /// <boneTransform>16 floats</boneTransform>
  69. /// [<parent>bone_name</parent>]
  70. /// <bone>
  71. /// ...
  72. /// </bones>
  73. /// </skeleton>
  74. /// @endcode
  75. class SkeletonResource : public ResourceObject
  76. {
  77. public:
  78. SkeletonResource(ResourceManager* manager)
  79. : ResourceObject(manager)
  80. {
  81. }
  82. ~SkeletonResource();
  83. /// Load file
  84. ANKI_USE_RESULT Error load(const ResourceFilename& filename, Bool async);
  85. const DynamicArray<Bone>& getBones() const
  86. {
  87. return m_bones;
  88. }
  89. const Bone* tryFindBone(CString name) const
  90. {
  91. // TODO Optimize
  92. for(const Bone& b : m_bones)
  93. {
  94. if(b.m_name == name)
  95. {
  96. return &b;
  97. }
  98. }
  99. return nullptr;
  100. }
  101. const Bone& getRootBone() const
  102. {
  103. return m_bones[m_rootBoneIdx];
  104. }
  105. private:
  106. DynamicArray<Bone> m_bones;
  107. U32 m_rootBoneIdx = MAX_U32;
  108. };
  109. /// @}
  110. } // end namespace anki