1
0

CCMeshSkin.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "3d/CCMeshSkin.h"
  21. #include "3d/CCBundle3D.h"
  22. #include "3d/CCSkeleton3D.h"
  23. NS_CC_BEGIN
  24. static int PALETTE_ROWS = 3;
  25. MeshSkin::MeshSkin()
  26. : _rootBone(nullptr)
  27. , _skeleton(nullptr)
  28. , _matrixPalette(nullptr)
  29. {
  30. }
  31. MeshSkin::~MeshSkin()
  32. {
  33. removeAllBones();
  34. CC_SAFE_RELEASE(_skeleton);
  35. }
  36. MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::vector<std::string>& boneNames, const std::vector<Mat4>& invBindPose)
  37. {
  38. auto skin = new (std::nothrow) MeshSkin();
  39. skin->_skeleton = skeleton;
  40. skeleton->retain();
  41. CCASSERT(boneNames.size() == invBindPose.size(), "bone names' num should equals to invBindPose's num");
  42. for (const auto& it : boneNames) {
  43. auto bone = skeleton->getBoneByName(it);
  44. if (bone)
  45. {
  46. skin->addSkinBone(bone);
  47. }
  48. }
  49. skin->_invBindPoses = invBindPose;
  50. skin->autorelease();
  51. return skin;
  52. }
  53. ssize_t MeshSkin::getBoneCount() const
  54. {
  55. return _skinBones.size();
  56. }
  57. //get bone
  58. Bone3D* MeshSkin::getBoneByIndex(unsigned int index) const
  59. {
  60. if (static_cast<int>(index) < _skinBones.size())
  61. return _skinBones.at(index);
  62. return nullptr;
  63. }
  64. Bone3D* MeshSkin::getBoneByName(const std::string& id) const
  65. {
  66. //search from skin bones
  67. for (const auto& it : _skinBones) {
  68. if (it->getName() == id)
  69. return it;
  70. }
  71. return nullptr;
  72. }
  73. int MeshSkin::getBoneIndex(Bone3D* bone) const
  74. {
  75. for (ssize_t i = 0, size = _skinBones.size(); i < size; ++i) {
  76. if (_skinBones.at(i) == bone)
  77. return static_cast<int>(i);
  78. }
  79. return -1;
  80. }
  81. //compute matrix palette used by gpu skin
  82. Vec4* MeshSkin::getMatrixPalette()
  83. {
  84. if (_matrixPalette == nullptr)
  85. {
  86. _matrixPalette = new (std::nothrow) Vec4[_skinBones.size() * PALETTE_ROWS];
  87. }
  88. int i = 0, paletteIndex = 0;
  89. static Mat4 t;
  90. for (auto it : _skinBones )
  91. {
  92. Mat4::multiply(it->getWorldMat(), _invBindPoses[i++], &t);
  93. _matrixPalette[paletteIndex++].set(t.m[0], t.m[4], t.m[8], t.m[12]);
  94. _matrixPalette[paletteIndex++].set(t.m[1], t.m[5], t.m[9], t.m[13]);
  95. _matrixPalette[paletteIndex++].set(t.m[2], t.m[6], t.m[10], t.m[14]);
  96. }
  97. return _matrixPalette;
  98. }
  99. ssize_t MeshSkin::getMatrixPaletteSize() const
  100. {
  101. return _skinBones.size() * PALETTE_ROWS;
  102. }
  103. void MeshSkin::removeAllBones()
  104. {
  105. _skinBones.clear();
  106. CC_SAFE_DELETE_ARRAY(_matrixPalette);
  107. CC_SAFE_RELEASE(_rootBone);
  108. }
  109. void MeshSkin::addSkinBone(Bone3D* bone)
  110. {
  111. _skinBones.pushBack(bone);
  112. }
  113. Bone3D* MeshSkin::getRootBone() const
  114. {
  115. Bone3D* root = nullptr;
  116. if (_skinBones.size())
  117. {
  118. root = _skinBones.at(0);
  119. while (root->getParentBone()) {
  120. root = root->getParentBone();
  121. }
  122. }
  123. return root;
  124. }
  125. const Mat4& MeshSkin::getInvBindPose(const Bone3D* bone)
  126. {
  127. for (ssize_t i = 0, size = _skinBones.size(); i < size; ++i) {
  128. if (_skinBones.at(i) == bone)
  129. {
  130. return _invBindPoses.at(i);
  131. }
  132. }
  133. return Mat4::IDENTITY;
  134. }
  135. NS_CC_END