Joint.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "Base.h"
  2. #include "Joint.h"
  3. #include "MeshSkin.h"
  4. namespace gameplay
  5. {
  6. Joint::Joint(const char* id)
  7. : Node(id), _jointMatrixDirty(true), _skinCount(0)
  8. {
  9. }
  10. Joint::~Joint()
  11. {
  12. }
  13. Joint* Joint::create(const char* id)
  14. {
  15. return new Joint(id);
  16. }
  17. Node* Joint::cloneSingleNode(NodeCloneContext &context) const
  18. {
  19. Joint* copy = Joint::create(getId());
  20. GP_ASSERT(copy);
  21. context.registerClonedNode(this, copy);
  22. copy->_bindPose = _bindPose;
  23. copy->_skinCount = _skinCount;
  24. Node::cloneInto(copy, context);
  25. return copy;
  26. }
  27. Node::Type Joint::getType() const
  28. {
  29. return Node::JOINT;
  30. }
  31. void Joint::transformChanged()
  32. {
  33. Node::transformChanged();
  34. _jointMatrixDirty = true;
  35. }
  36. void Joint::updateJointMatrix(const Matrix& bindShape, Vector4* matrixPalette)
  37. {
  38. // Note: If more than one MeshSkin influences this Joint, we need to skip
  39. // the _jointMatrixDirty optimization since updateJointMatrix() may be
  40. // called multiple times a frame with different bindShape matrices (and
  41. // different matrixPallete pointers).
  42. if (_skinCount > 1 || _jointMatrixDirty)
  43. {
  44. _jointMatrixDirty = false;
  45. static Matrix t;
  46. Matrix::multiply(Node::getWorldMatrix(), getInverseBindPose(), &t);
  47. Matrix::multiply(t, bindShape, &t);
  48. GP_ASSERT(matrixPalette);
  49. matrixPalette[0].set(t.m[0], t.m[4], t.m[8], t.m[12]);
  50. matrixPalette[1].set(t.m[1], t.m[5], t.m[9], t.m[13]);
  51. matrixPalette[2].set(t.m[2], t.m[6], t.m[10], t.m[14]);
  52. }
  53. }
  54. const Matrix& Joint::getInverseBindPose() const
  55. {
  56. return _bindPose;
  57. }
  58. void Joint::setInverseBindPose(const Matrix& m)
  59. {
  60. _bindPose = m;
  61. _jointMatrixDirty = true;
  62. }
  63. }