MeshSkin.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "Base.h"
  2. #include "MeshSkin.h"
  3. #include "Joint.h"
  4. // The number of rows in each palette matrix.
  5. #define PALETTE_ROWS 3
  6. namespace gameplay
  7. {
  8. MeshSkin::MeshSkin()
  9. : _rootJoint(NULL), _rootNode(NULL), _matrixPalette(NULL), _model(NULL)
  10. {
  11. }
  12. MeshSkin::~MeshSkin()
  13. {
  14. clearJoints();
  15. SAFE_DELETE_ARRAY(_matrixPalette);
  16. }
  17. const Matrix& MeshSkin::getBindShape() const
  18. {
  19. return _bindShape;
  20. }
  21. void MeshSkin::setBindShape(const float* matrix)
  22. {
  23. _bindShape.set(matrix);
  24. }
  25. unsigned int MeshSkin::getJointCount() const
  26. {
  27. return _joints.size();
  28. }
  29. Joint* MeshSkin::getJoint(unsigned int index) const
  30. {
  31. assert(index < _joints.size());
  32. return _joints[index];
  33. }
  34. Joint* MeshSkin::getJoint(const char* id) const
  35. {
  36. assert(id);
  37. for (unsigned int i = 0, count = _joints.size(); i < count; ++i)
  38. {
  39. Joint* j = _joints[i];
  40. if (j && j->getId() != NULL && strcmp(j->getId(), id) == 0)
  41. {
  42. return j;
  43. }
  44. }
  45. return NULL;
  46. }
  47. MeshSkin* MeshSkin::clone() const
  48. {
  49. MeshSkin* skin = new MeshSkin();
  50. skin->_bindShape = _bindShape;
  51. if (_rootNode && _rootJoint)
  52. {
  53. const unsigned int jointCount = getJointCount();
  54. skin->setJointCount(jointCount);
  55. assert(skin->_rootNode == NULL);
  56. skin->_rootNode = _rootNode->clone();
  57. Node* node = skin->_rootNode->findNode(_rootJoint->getId());
  58. assert(node);
  59. skin->_rootJoint = static_cast<Joint*>(node);
  60. for (unsigned int i = 0; i < jointCount; ++i)
  61. {
  62. Joint* oldJoint = getJoint(i);
  63. Joint* newJoint = static_cast<Joint*>(skin->_rootJoint->findNode(oldJoint->getId()));
  64. if (!newJoint)
  65. {
  66. if (strcmp(skin->_rootJoint->getId(), oldJoint->getId()) == 0)
  67. newJoint = static_cast<Joint*>(skin->_rootJoint);
  68. }
  69. assert(newJoint);
  70. skin->setJoint(newJoint, i);
  71. }
  72. }
  73. return skin;
  74. }
  75. void MeshSkin::setJointCount(unsigned int jointCount)
  76. {
  77. // Erase the joints vector and release all joints
  78. clearJoints();
  79. // Resize the joints vector and initialize to NULL
  80. _joints.resize(jointCount);
  81. for (unsigned int i = 0; i < jointCount; i++)
  82. {
  83. _joints[i] = NULL;
  84. }
  85. // Rebuild the matrix palette. Each matrix is 3 rows of Vector4.
  86. SAFE_DELETE_ARRAY(_matrixPalette);
  87. if (jointCount > 0)
  88. {
  89. _matrixPalette = new Vector4[jointCount * PALETTE_ROWS];
  90. for (unsigned int i = 0; i < jointCount * PALETTE_ROWS; i+=PALETTE_ROWS)
  91. {
  92. _matrixPalette[i+0].set(1.0f, 0.0f, 0.0f, 0.0f);
  93. _matrixPalette[i+1].set(0.0f, 1.0f, 0.0f, 0.0f);
  94. _matrixPalette[i+2].set(0.0f, 0.0f, 1.0f, 0.0f);
  95. }
  96. }
  97. }
  98. void MeshSkin::setJoint(Joint* joint, unsigned int index)
  99. {
  100. assert(index < _joints.size());
  101. if (_joints[index])
  102. {
  103. _joints[index]->_skinCount--;
  104. SAFE_RELEASE(_joints[index]);
  105. }
  106. _joints[index] = joint;
  107. if (joint)
  108. {
  109. joint->addRef();
  110. joint->_skinCount++;
  111. }
  112. }
  113. Vector4* MeshSkin::getMatrixPalette() const
  114. {
  115. unsigned int count = _joints.size();
  116. for (unsigned int i = 0; i < count; i++)
  117. {
  118. _joints[i]->updateJointMatrix(getBindShape(), &_matrixPalette[i * PALETTE_ROWS]);
  119. }
  120. return _matrixPalette;
  121. }
  122. unsigned int MeshSkin::getMatrixPaletteSize() const
  123. {
  124. return _joints.size() * PALETTE_ROWS;
  125. }
  126. Model* MeshSkin::getModel() const
  127. {
  128. return _model;
  129. }
  130. Joint* MeshSkin::getRootJoint() const
  131. {
  132. return _rootJoint;
  133. }
  134. void MeshSkin::setRootJoint(Joint* joint)
  135. {
  136. if (_rootJoint)
  137. {
  138. if (_rootJoint->getParent())
  139. {
  140. _rootJoint->getParent()->removeListener(this);
  141. }
  142. }
  143. _rootJoint = joint;
  144. // If the root joint has a parent node, register for its transformChanged event
  145. if (_rootJoint && _rootJoint->getParent())
  146. {
  147. _rootJoint->getParent()->addListener(this, 1);
  148. }
  149. Node* newRootNode = _rootJoint;
  150. if (newRootNode)
  151. {
  152. // Find the top level parent node of the root joint
  153. for (Node* node = newRootNode->getParent(); node != NULL; node = node->getParent())
  154. {
  155. if (node->getParent() == NULL)
  156. {
  157. newRootNode = node;
  158. break;
  159. }
  160. }
  161. }
  162. setRootNode(newRootNode);
  163. }
  164. void MeshSkin::transformChanged(Transform* transform, long cookie)
  165. {
  166. switch (cookie)
  167. {
  168. case 1:
  169. // The direct parent of our joint hierarchy has changed.
  170. // Dirty the bounding volume for our model's node. This special
  171. // case allows us to have much tighter bounding volumes for
  172. // skinned meshes by only considering local skin/joint transformations
  173. // during bounding volume computation instead of fully resolved
  174. // joint transformations.
  175. if (_model && _model->getNode())
  176. {
  177. _model->getNode()->setBoundsDirty();
  178. }
  179. break;
  180. }
  181. }
  182. int MeshSkin::getJointIndex(Joint* joint) const
  183. {
  184. for (unsigned int i = 0, count = _joints.size(); i < count; ++i)
  185. {
  186. if (_joints[i] == joint)
  187. {
  188. return (int)i;
  189. }
  190. }
  191. return -1;
  192. }
  193. void MeshSkin::setRootNode(Node* node)
  194. {
  195. if (_rootNode != node)
  196. {
  197. SAFE_RELEASE(_rootNode);
  198. _rootNode = node;
  199. if (_rootNode)
  200. {
  201. _rootNode->addRef();
  202. }
  203. }
  204. }
  205. void MeshSkin::clearJoints()
  206. {
  207. setRootJoint(NULL);
  208. for (unsigned int i = 0, count = _joints.size(); i < count; ++i)
  209. {
  210. SAFE_RELEASE(_joints[i]);
  211. }
  212. _joints.clear();
  213. }
  214. }