MeshSkin.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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(NodeCloneContext &context) 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. // Check if the root node has already been cloned.
  57. if (Node* rootNode = context.findClonedNode(_rootNode))
  58. {
  59. skin->_rootNode = rootNode;
  60. rootNode->addRef();
  61. }
  62. else
  63. {
  64. skin->_rootNode = _rootNode->cloneRecursive(context);
  65. }
  66. Node* node = skin->_rootNode->findNode(_rootJoint->getId());
  67. assert(node);
  68. skin->_rootJoint = static_cast<Joint*>(node);
  69. for (unsigned int i = 0; i < jointCount; ++i)
  70. {
  71. Joint* oldJoint = getJoint(i);
  72. Joint* newJoint = static_cast<Joint*>(skin->_rootJoint->findNode(oldJoint->getId()));
  73. if (!newJoint)
  74. {
  75. if (strcmp(skin->_rootJoint->getId(), oldJoint->getId()) == 0)
  76. newJoint = static_cast<Joint*>(skin->_rootJoint);
  77. }
  78. assert(newJoint);
  79. skin->setJoint(newJoint, i);
  80. }
  81. }
  82. return skin;
  83. }
  84. void MeshSkin::setJointCount(unsigned int jointCount)
  85. {
  86. // Erase the joints vector and release all joints
  87. clearJoints();
  88. // Resize the joints vector and initialize to NULL
  89. _joints.resize(jointCount);
  90. for (unsigned int i = 0; i < jointCount; i++)
  91. {
  92. _joints[i] = NULL;
  93. }
  94. // Rebuild the matrix palette. Each matrix is 3 rows of Vector4.
  95. SAFE_DELETE_ARRAY(_matrixPalette);
  96. if (jointCount > 0)
  97. {
  98. _matrixPalette = new Vector4[jointCount * PALETTE_ROWS];
  99. for (unsigned int i = 0; i < jointCount * PALETTE_ROWS; i+=PALETTE_ROWS)
  100. {
  101. _matrixPalette[i+0].set(1.0f, 0.0f, 0.0f, 0.0f);
  102. _matrixPalette[i+1].set(0.0f, 1.0f, 0.0f, 0.0f);
  103. _matrixPalette[i+2].set(0.0f, 0.0f, 1.0f, 0.0f);
  104. }
  105. }
  106. }
  107. void MeshSkin::setJoint(Joint* joint, unsigned int index)
  108. {
  109. assert(index < _joints.size());
  110. if (_joints[index])
  111. {
  112. _joints[index]->_skinCount--;
  113. SAFE_RELEASE(_joints[index]);
  114. }
  115. _joints[index] = joint;
  116. if (joint)
  117. {
  118. joint->addRef();
  119. joint->_skinCount++;
  120. }
  121. }
  122. Vector4* MeshSkin::getMatrixPalette() const
  123. {
  124. unsigned int count = _joints.size();
  125. for (unsigned int i = 0; i < count; i++)
  126. {
  127. _joints[i]->updateJointMatrix(getBindShape(), &_matrixPalette[i * PALETTE_ROWS]);
  128. }
  129. return _matrixPalette;
  130. }
  131. unsigned int MeshSkin::getMatrixPaletteSize() const
  132. {
  133. return _joints.size() * PALETTE_ROWS;
  134. }
  135. Model* MeshSkin::getModel() const
  136. {
  137. return _model;
  138. }
  139. Joint* MeshSkin::getRootJoint() const
  140. {
  141. return _rootJoint;
  142. }
  143. void MeshSkin::setRootJoint(Joint* joint)
  144. {
  145. if (_rootJoint)
  146. {
  147. if (_rootJoint->getParent())
  148. {
  149. _rootJoint->getParent()->removeListener(this);
  150. }
  151. }
  152. _rootJoint = joint;
  153. // If the root joint has a parent node, register for its transformChanged event
  154. if (_rootJoint && _rootJoint->getParent())
  155. {
  156. _rootJoint->getParent()->addListener(this, 1);
  157. }
  158. Node* newRootNode = _rootJoint;
  159. if (newRootNode)
  160. {
  161. // Find the top level parent node of the root joint
  162. for (Node* node = newRootNode->getParent(); node != NULL; node = node->getParent())
  163. {
  164. if (node->getParent() == NULL)
  165. {
  166. newRootNode = node;
  167. break;
  168. }
  169. }
  170. }
  171. setRootNode(newRootNode);
  172. }
  173. void MeshSkin::transformChanged(Transform* transform, long cookie)
  174. {
  175. switch (cookie)
  176. {
  177. case 1:
  178. // The direct parent of our joint hierarchy has changed.
  179. // Dirty the bounding volume for our model's node. This special
  180. // case allows us to have much tighter bounding volumes for
  181. // skinned meshes by only considering local skin/joint transformations
  182. // during bounding volume computation instead of fully resolved
  183. // joint transformations.
  184. if (_model && _model->getNode())
  185. {
  186. _model->getNode()->setBoundsDirty();
  187. }
  188. break;
  189. }
  190. }
  191. int MeshSkin::getJointIndex(Joint* joint) const
  192. {
  193. for (unsigned int i = 0, count = _joints.size(); i < count; ++i)
  194. {
  195. if (_joints[i] == joint)
  196. {
  197. return (int)i;
  198. }
  199. }
  200. return -1;
  201. }
  202. void MeshSkin::setRootNode(Node* node)
  203. {
  204. if (_rootNode != node)
  205. {
  206. SAFE_RELEASE(_rootNode);
  207. _rootNode = node;
  208. if (_rootNode)
  209. {
  210. _rootNode->addRef();
  211. }
  212. }
  213. }
  214. void MeshSkin::clearJoints()
  215. {
  216. setRootJoint(NULL);
  217. for (unsigned int i = 0, count = _joints.size(); i < count; ++i)
  218. {
  219. SAFE_RELEASE(_joints[i]);
  220. }
  221. _joints.clear();
  222. }
  223. }