SkeletonResource.cpp 2.6 KB

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