axmol/cocos/3d/CCMeshSkin.cpp

161 lines
4.2 KiB
C++
Raw Normal View History

2014-06-04 18:17:09 +08:00
/****************************************************************************
Copyright (c) 2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2014-06-17 10:49:52 +08:00
#include "3d/CCMeshSkin.h"
2014-07-30 18:32:34 +08:00
#include "3d/CCSkeleton3D.h"
2014-06-17 10:49:52 +08:00
#include "3d/CCBundle3D.h"
2014-08-28 17:32:23 +08:00
#include "3d/CCSkeleton3D.h"
2014-06-04 18:17:09 +08:00
NS_CC_BEGIN
static int PALETTE_ROWS = 3;
MeshSkin::MeshSkin()
: _rootBone(nullptr)
, _matrixPalette(nullptr)
2014-08-15 17:41:04 +08:00
, _skeleton(nullptr)
2014-06-04 18:17:09 +08:00
{
}
MeshSkin::~MeshSkin()
{
removeAllBones();
2014-07-30 18:32:34 +08:00
CC_SAFE_RELEASE(_skeleton);
2014-06-04 18:17:09 +08:00
}
2014-08-15 17:41:04 +08:00
MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::vector<std::string>& boneNames, const std::vector<Mat4>& invBindPose)
{
auto skin = new (std::nothrow) MeshSkin();
2014-08-15 17:41:04 +08:00
skin->_skeleton = skeleton;
skeleton->retain();
CCASSERT(boneNames.size() == invBindPose.size(), "bone names' num should equals to invBindPose's num");
for (const auto& it : boneNames) {
auto bone = skeleton->getBoneByName(it);
skin->addSkinBone(bone);
}
skin->_invBindPoses = invBindPose;
skin->autorelease();
return skin;
}
2014-06-26 21:58:26 +08:00
ssize_t MeshSkin::getBoneCount() const
2014-06-04 18:17:09 +08:00
{
2014-08-18 11:13:08 +08:00
return _skinBones.size();
2014-06-04 18:17:09 +08:00
}
//get bone
2014-07-11 10:17:36 +08:00
Bone3D* MeshSkin::getBoneByIndex(unsigned int index) const
2014-06-04 18:17:09 +08:00
{
2014-06-19 12:01:03 +08:00
if (index < _skinBones.size())
return _skinBones.at(index);
return nullptr;
2014-06-04 18:17:09 +08:00
}
2014-07-11 10:17:36 +08:00
Bone3D* MeshSkin::getBoneByName(const std::string& id) const
2014-06-04 18:17:09 +08:00
{
2014-06-19 12:01:03 +08:00
//search from skin bones
2014-07-31 18:42:58 +08:00
for (const auto& it : _skinBones) {
2014-06-19 12:01:03 +08:00
if (it->getName() == id)
return it;
}
2014-06-04 18:17:09 +08:00
return nullptr;
}
2014-07-11 10:17:36 +08:00
int MeshSkin::getBoneIndex(Bone3D* bone) const
2014-06-04 18:17:09 +08:00
{
2014-06-19 12:01:03 +08:00
int i = 0;
for (; i < _skinBones.size(); i++) {
if (_skinBones.at(i) == bone)
return i;
2014-06-04 18:17:09 +08:00
}
2014-08-18 11:13:08 +08:00
2014-06-04 18:17:09 +08:00
return -1;
}
//compute matrix palette used by gpu skin
2014-06-09 18:37:58 +08:00
Vec4* MeshSkin::getMatrixPalette()
2014-06-04 18:17:09 +08:00
{
2014-06-19 12:01:03 +08:00
if (_matrixPalette == nullptr)
{
_matrixPalette = new (std::nothrow) Vec4[_skinBones.size() * PALETTE_ROWS];
2014-06-19 12:01:03 +08:00
}
2014-07-30 18:32:34 +08:00
int i = 0, paletteIndex = 0;
static Mat4 t;
2014-07-31 20:17:51 +08:00
for (auto it : _skinBones )
{
2014-07-30 18:32:34 +08:00
Mat4::multiply(it->getWorldMat(), _invBindPoses[i++], &t);
_matrixPalette[paletteIndex++].set(t.m[0], t.m[4], t.m[8], t.m[12]);
_matrixPalette[paletteIndex++].set(t.m[1], t.m[5], t.m[9], t.m[13]);
_matrixPalette[paletteIndex++].set(t.m[2], t.m[6], t.m[10], t.m[14]);
2014-07-31 20:17:51 +08:00
}
2014-06-04 18:17:09 +08:00
return _matrixPalette;
}
2014-06-19 12:01:03 +08:00
ssize_t MeshSkin::getMatrixPaletteSize() const
2014-06-04 18:17:09 +08:00
{
2014-06-19 12:01:03 +08:00
return _skinBones.size() * PALETTE_ROWS;
2014-06-04 18:17:09 +08:00
}
void MeshSkin::removeAllBones()
{
2014-06-19 12:01:03 +08:00
_skinBones.clear();
2014-06-04 18:17:09 +08:00
CC_SAFE_DELETE_ARRAY(_matrixPalette);
CC_SAFE_RELEASE(_rootBone);
}
2014-07-11 10:17:36 +08:00
void MeshSkin::addSkinBone(Bone3D* bone)
2014-06-19 12:01:03 +08:00
{
_skinBones.pushBack(bone);
}
2014-08-18 17:02:13 +08:00
Bone3D* MeshSkin::getRootBone() const
{
Bone3D* root = nullptr;
if (_skinBones.size())
{
root = _skinBones.at(0);
while (root->getParentBone()) {
root = root->getParentBone();
}
}
return root;
}
2014-10-20 22:53:24 +08:00
const Mat4& MeshSkin::getInvBindPose(const Bone3D* bone)
{
for (ssize_t i = 0; i < _skinBones.size(); i++) {
if (_skinBones.at(i) == bone)
{
return _invBindPoses.at(i);
}
}
return Mat4::IDENTITY;
}
2014-06-04 18:17:09 +08:00
NS_CC_END