SkeletonResource.cpp 2.8 KB

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