SkeletonResource.h 2.2 KB

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