SkeletonPose.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Skeleton/SkeletonPose.h>
  5. #ifdef JPH_DEBUG_RENDERER
  6. #include <Jolt/Renderer/DebugRenderer.h>
  7. #endif // JPH_DEBUG_RENDERER
  8. JPH_NAMESPACE_BEGIN
  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. mJointMatrices[i] = mJoints[i].ToMatrix();
  20. int parent = mSkeleton->GetJoint(i).mParentJointIndex;
  21. if (parent >= 0)
  22. {
  23. JPH_ASSERT(parent < i, "Joints must be ordered: parents first");
  24. mJointMatrices[i] = mJointMatrices[parent] * mJointMatrices[i];
  25. }
  26. }
  27. }
  28. void SkeletonPose::CalculateJointStates()
  29. {
  30. for (int i = 0; i < (int)mJoints.size(); ++i)
  31. {
  32. Mat44 local_transform;
  33. int parent = mSkeleton->GetJoint(i).mParentJointIndex;
  34. if (parent >= 0)
  35. local_transform = mJointMatrices[parent].Inversed() * mJointMatrices[i];
  36. else
  37. local_transform = mJointMatrices[i];
  38. JointState &joint = mJoints[i];
  39. joint.mTranslation = local_transform.GetTranslation();
  40. joint.mRotation = local_transform.GetRotation().GetQuaternion();
  41. }
  42. }
  43. void SkeletonPose::CalculateLocalSpaceJointMatrices(Mat44 *outMatrices) const
  44. {
  45. for (int i = 0; i < (int)mJoints.size(); ++i)
  46. outMatrices[i] = mJoints[i].ToMatrix();
  47. }
  48. #ifdef JPH_DEBUG_RENDERER
  49. void SkeletonPose::Draw(const DrawSettings &inDrawSettings, DebugRenderer *inRenderer, RMat44Arg inOffset) const
  50. {
  51. RMat44 offset = inOffset * RMat44::sTranslation(mRootOffset);
  52. const Skeleton::JointVector &joints = mSkeleton->GetJoints();
  53. for (int b = 0; b < mSkeleton->GetJointCount(); ++b)
  54. {
  55. RMat44 joint_transform = offset * mJointMatrices[b];
  56. if (inDrawSettings.mDrawJoints)
  57. {
  58. int parent = joints[b].mParentJointIndex;
  59. if (parent >= 0)
  60. inRenderer->DrawLine(offset * mJointMatrices[parent].GetTranslation(), joint_transform.GetTranslation(), Color::sGreen);
  61. }
  62. if (inDrawSettings.mDrawJointOrientations)
  63. inRenderer->DrawCoordinateSystem(joint_transform, 0.05f);
  64. if (inDrawSettings.mDrawJointNames)
  65. inRenderer->DrawText3D(joint_transform.GetTranslation(), joints[b].mName, Color::sWhite, 0.05f);
  66. }
  67. }
  68. #endif // JPH_DEBUG_RENDERER
  69. JPH_NAMESPACE_END