SkeletonPose.cpp 2.4 KB

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