MeshSkin.cpp 6.3 KB

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