2
0

Skeleton.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Skeleton.h"
  9. #include <rapidjson/document.h>
  10. #include <SDL/SDL_log.h>
  11. #include "MatrixPalette.h"
  12. #include "LevelLoader.h"
  13. bool Skeleton::Load(const std::string& fileName)
  14. {
  15. mFileName = fileName;
  16. rapidjson::Document doc;
  17. if (!LevelLoader::LoadJSON(fileName, doc))
  18. {
  19. SDL_Log("Failed to load skeleton %s", fileName.c_str());
  20. return false;
  21. }
  22. int ver = doc["version"].GetInt();
  23. // Check the metadata
  24. if (ver != 1)
  25. {
  26. SDL_Log("Skeleton %s unknown format", fileName.c_str());
  27. return false;
  28. }
  29. const rapidjson::Value& bonecount = doc["bonecount"];
  30. if (!bonecount.IsUint())
  31. {
  32. SDL_Log("Skeleton %s doesn't have a bone count.", fileName.c_str());
  33. return false;
  34. }
  35. size_t count = bonecount.GetUint();
  36. if (count > MAX_SKELETON_BONES)
  37. {
  38. SDL_Log("Skeleton %s exceeds maximum bone count.", fileName.c_str());
  39. return false;
  40. }
  41. mBones.reserve(count);
  42. const rapidjson::Value& bones = doc["bones"];
  43. if (!bones.IsArray())
  44. {
  45. SDL_Log("Skeleton %s doesn't have a bone array?", fileName.c_str());
  46. return false;
  47. }
  48. if (bones.Size() != count)
  49. {
  50. SDL_Log("Skeleton %s has a mismatch between the bone count and number of bones", fileName.c_str());
  51. return false;
  52. }
  53. Bone temp;
  54. for (rapidjson::SizeType i = 0; i < count; i++)
  55. {
  56. if (!bones[i].IsObject())
  57. {
  58. SDL_Log("Skeleton %s: Bone %d is invalid.", fileName.c_str(), i);
  59. return false;
  60. }
  61. const rapidjson::Value& name = bones[i]["name"];
  62. temp.mName = name.GetString();
  63. const rapidjson::Value& parent = bones[i]["parent"];
  64. temp.mParent = parent.GetInt();
  65. const rapidjson::Value& bindpose = bones[i]["bindpose"];
  66. if (!bindpose.IsObject())
  67. {
  68. SDL_Log("Skeleton %s: Bone %d is invalid.", fileName.c_str(), i);
  69. return false;
  70. }
  71. const rapidjson::Value& rot = bindpose["rot"];
  72. const rapidjson::Value& trans = bindpose["trans"];
  73. if (!rot.IsArray() || !trans.IsArray())
  74. {
  75. SDL_Log("Skeleton %s: Bone %d is invalid.", fileName.c_str(), i);
  76. return false;
  77. }
  78. temp.mLocalBindPose.mRotation.x = rot[0].GetDouble();
  79. temp.mLocalBindPose.mRotation.y = rot[1].GetDouble();
  80. temp.mLocalBindPose.mRotation.z = rot[2].GetDouble();
  81. temp.mLocalBindPose.mRotation.w = rot[3].GetDouble();
  82. temp.mLocalBindPose.mTranslation.x = trans[0].GetDouble();
  83. temp.mLocalBindPose.mTranslation.y = trans[1].GetDouble();
  84. temp.mLocalBindPose.mTranslation.z = trans[2].GetDouble();
  85. mBones.emplace_back(temp);
  86. }
  87. // Now that we have the bones
  88. ComputeGlobalInvBindPose();
  89. return true;
  90. }
  91. void Skeleton::ComputeGlobalInvBindPose()
  92. {
  93. // Resize to number of bones, which automatically fills identity
  94. mGlobalInvBindPoses.resize(GetNumBones());
  95. // Step 1: Compute global bind pose for each bone
  96. // The global bind pose for root is just the local bind pose
  97. mGlobalInvBindPoses[0] = mBones[0].mLocalBindPose.ToMatrix();
  98. // Each remaining bone's global bind pose is its local pose
  99. // multiplied by the parent's global bind pose
  100. for (size_t i = 1; i < mGlobalInvBindPoses.size(); i++)
  101. {
  102. Matrix4 localMat = mBones[i].mLocalBindPose.ToMatrix();
  103. mGlobalInvBindPoses[i] = localMat * mGlobalInvBindPoses[mBones[i].mParent];
  104. }
  105. // Step 2: Invert
  106. for (size_t i = 0; i < mGlobalInvBindPoses.size(); i++)
  107. {
  108. mGlobalInvBindPoses[i].Invert();
  109. }
  110. }