Joint.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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)
  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. Node::cloneInto(copy, context);
  24. return copy;
  25. }
  26. Node::Type Joint::getType() const
  27. {
  28. return Node::JOINT;
  29. }
  30. Scene* Joint::getScene() const
  31. {
  32. // Overrides Node::getScene() to search the node our skins.
  33. for (const SkinReference* itr = &_skin; itr && itr->skin; itr = itr->next)
  34. {
  35. Model* model = itr->skin ? itr->skin->getModel() : NULL;
  36. if (model)
  37. {
  38. Node* node = model->getNode();
  39. if (node)
  40. {
  41. Scene* scene = node->getScene();
  42. if (scene)
  43. return scene;
  44. }
  45. }
  46. }
  47. return Node::getScene();
  48. }
  49. void Joint::transformChanged()
  50. {
  51. Node::transformChanged();
  52. _jointMatrixDirty = true;
  53. }
  54. void Joint::updateJointMatrix(const Matrix& bindShape, Vector4* matrixPalette)
  55. {
  56. // Note: If more than one MeshSkin influences this Joint, we need to skip
  57. // the _jointMatrixDirty optimization since updateJointMatrix() may be
  58. // called multiple times a frame with different bindShape matrices (and
  59. // different matrixPallete pointers).
  60. if (_skin.next || _jointMatrixDirty)
  61. {
  62. _jointMatrixDirty = false;
  63. static Matrix t;
  64. Matrix::multiply(Node::getWorldMatrix(), getInverseBindPose(), &t);
  65. Matrix::multiply(t, bindShape, &t);
  66. GP_ASSERT(matrixPalette);
  67. matrixPalette[0].set(t.m[0], t.m[4], t.m[8], t.m[12]);
  68. matrixPalette[1].set(t.m[1], t.m[5], t.m[9], t.m[13]);
  69. matrixPalette[2].set(t.m[2], t.m[6], t.m[10], t.m[14]);
  70. }
  71. }
  72. const Matrix& Joint::getInverseBindPose() const
  73. {
  74. return _bindPose;
  75. }
  76. void Joint::setInverseBindPose(const Matrix& m)
  77. {
  78. _bindPose = m;
  79. _jointMatrixDirty = true;
  80. }
  81. void Joint::addSkin(MeshSkin* skin)
  82. {
  83. if (!_skin.skin)
  84. {
  85. // Store skin in root reference
  86. _skin.skin = skin;
  87. }
  88. else
  89. {
  90. // Add a new SkinReference to the end of our list
  91. SkinReference* ref = &_skin;
  92. while (ref->next)
  93. {
  94. ref = ref->next;
  95. }
  96. ref->next = new SkinReference();
  97. ref->next->skin = skin;
  98. }
  99. }
  100. void Joint::removeSkin(MeshSkin* skin)
  101. {
  102. if (_skin.skin == skin)
  103. {
  104. // Skin is our root referenced skin
  105. _skin.skin = NULL;
  106. // Shift the next skin reference down to the root
  107. if (_skin.next)
  108. {
  109. SkinReference* tmp = _skin.next;
  110. _skin.skin = tmp->skin;
  111. _skin.next = tmp->next;
  112. tmp->next = NULL; // prevent deletion
  113. SAFE_DELETE(tmp);
  114. }
  115. }
  116. else
  117. {
  118. // Search for the entry referencing this skin
  119. SkinReference* ref = &_skin;
  120. while (SkinReference* tmp = ref->next)
  121. {
  122. if (tmp->skin == skin)
  123. {
  124. // Link this refernce out
  125. ref->next = tmp->next;
  126. tmp->next = NULL; // prevent deletion
  127. SAFE_DELETE(tmp);
  128. break;
  129. }
  130. ref = tmp;
  131. }
  132. }
  133. }
  134. Joint::SkinReference::SkinReference()
  135. : skin(NULL), next(NULL)
  136. {
  137. }
  138. Joint::SkinReference::~SkinReference()
  139. {
  140. SAFE_DELETE(next);
  141. }
  142. }