2
0

SkeletonResource.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <AnKi/Resource/SkeletonResource.h>
  6. #include <AnKi/Util/Xml.h>
  7. #include <AnKi/Util/StringList.h>
  8. namespace anki
  9. {
  10. SkeletonResource::~SkeletonResource()
  11. {
  12. for(Bone& b : m_bones)
  13. {
  14. b.destroy(getAllocator());
  15. }
  16. m_bones.destroy(getAllocator());
  17. }
  18. Error SkeletonResource::load(const ResourceFilename& filename, Bool async)
  19. {
  20. XmlDocument doc;
  21. ANKI_CHECK(openFileParseXml(filename, doc));
  22. XmlElement rootEl;
  23. ANKI_CHECK(doc.getChildElement("skeleton", rootEl));
  24. XmlElement bonesEl;
  25. ANKI_CHECK(rootEl.getChildElement("bones", bonesEl));
  26. // count the bones count
  27. XmlElement boneEl;
  28. U32 boneCount = 0;
  29. ANKI_CHECK(bonesEl.getChildElement("bone", boneEl));
  30. ANKI_CHECK(boneEl.getSiblingElementsCount(boneCount));
  31. ++boneCount;
  32. m_bones.create(getAllocator(), boneCount);
  33. StringListAuto boneParents(getAllocator());
  34. // Load every bone
  35. boneCount = 0;
  36. do
  37. {
  38. Bone& bone = m_bones[boneCount];
  39. bone.m_idx = boneCount;
  40. // name
  41. CString name;
  42. ANKI_CHECK(boneEl.getAttributeText("name", name));
  43. bone.m_name.create(getAllocator(), name);
  44. // transform
  45. ANKI_CHECK(boneEl.getAttributeNumbers("transform", bone.m_transform));
  46. // boneTransform
  47. ANKI_CHECK(boneEl.getAttributeNumbers("boneTransform", bone.m_vertTrf));
  48. // parent
  49. CString parent;
  50. Bool hasParent;
  51. ANKI_CHECK(boneEl.getAttributeTextOptional("parent", parent, hasParent));
  52. if(hasParent)
  53. {
  54. boneParents.pushBack(parent);
  55. }
  56. else
  57. {
  58. boneParents.pushBack("");
  59. if(m_rootBoneIdx != MAX_U32)
  60. {
  61. ANKI_RESOURCE_LOGE("Skeleton cannot have more than one root nodes");
  62. return Error::USER_DATA;
  63. }
  64. m_rootBoneIdx = boneCount;
  65. }
  66. // Advance
  67. ANKI_CHECK(boneEl.getNextSiblingElement("bone", boneEl));
  68. ++boneCount;
  69. } while(boneEl);
  70. // Resolve the parents
  71. auto it = boneParents.getBegin();
  72. for(U32 i = 0; i < m_bones.getSize(); ++i)
  73. {
  74. Bone& bone = m_bones[i];
  75. if(it->getLength() > 0)
  76. {
  77. for(U32 j = 0; j < m_bones.getSize(); ++j)
  78. {
  79. if(m_bones[j].m_name == *it)
  80. {
  81. bone.m_parent = &m_bones[j];
  82. break;
  83. }
  84. }
  85. if(bone.m_parent == nullptr)
  86. {
  87. ANKI_RESOURCE_LOGE("Bone \"%s\" is referencing an unknown parent \"%s\"", &bone.m_name[0],
  88. &it->toCString()[0]);
  89. return Error::USER_DATA;
  90. }
  91. if(bone.m_parent->m_childrenCount >= MAX_CHILDREN_PER_BONE)
  92. {
  93. ANKI_RESOURCE_LOGE("Bone \"%s\" cannot have more that %u children", &bone.m_parent->m_name[0],
  94. MAX_CHILDREN_PER_BONE);
  95. return Error::USER_DATA;
  96. }
  97. bone.m_parent->m_children[bone.m_parent->m_childrenCount++] = &bone;
  98. }
  99. ++it;
  100. }
  101. return Error::NONE;
  102. }
  103. } // end namespace anki