SkeletonPose.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Skeleton/SkeletonPose.h>
  5. #ifdef JPH_DEBUG_RENDERER
  6. #include <Renderer/DebugRenderer.h>
  7. #endif // JPH_DEBUG_RENDERER
  8. namespace JPH {
  9. void SkeletonPose::SetSkeleton(const Skeleton *inSkeleton)
  10. {
  11. mSkeleton = inSkeleton;
  12. mJoints.resize(mSkeleton->GetJointCount());
  13. mJointMatrices.resize(mSkeleton->GetJointCount());
  14. }
  15. void SkeletonPose::CalculateJointMatrices()
  16. {
  17. for (int i = 0; i < (int)mJoints.size(); ++i)
  18. {
  19. mJoints[i].ToMatrix(mJointMatrices[i]);
  20. int parent = mSkeleton->GetJoint(i).mParentJointIndex;
  21. if (parent >= 0)
  22. {
  23. JPH_ASSERT(parent < i, "Bones must be ordered: parents first");
  24. mJointMatrices[i] = mJointMatrices[parent] * mJointMatrices[i];
  25. }
  26. }
  27. }
  28. #ifdef JPH_DEBUG_RENDERER
  29. void SkeletonPose::Draw(const DrawSettings &inDrawSettings, DebugRenderer *inRenderer) const
  30. {
  31. const Skeleton::JointVector &joints = mSkeleton->GetJoints();
  32. for (int b = 0; b < mSkeleton->GetJointCount(); ++b)
  33. {
  34. if (inDrawSettings.mDrawJoints)
  35. {
  36. int parent = joints[b].mParentJointIndex;
  37. if (parent >= 0)
  38. inRenderer->DrawLine(mJointMatrices[parent].GetTranslation(), mJointMatrices[b].GetTranslation(), Color::sGreen);
  39. }
  40. if (inDrawSettings.mDrawJointOrientations)
  41. inRenderer->DrawCoordinateSystem(mJointMatrices[b], 0.05f);
  42. if (inDrawSettings.mDrawJointNames)
  43. inRenderer->DrawText3D(mJointMatrices[b].GetTranslation(), joints[b].mName, Color::sWhite, 0.05f);
  44. }
  45. }
  46. #endif // JPH_DEBUG_RENDERER
  47. } // JPH