axmol/cocos/3d/CCMeshSkin.cpp

483 lines
11 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"
#include "3d/CCBundle3D.h"
2014-06-04 18:17:09 +08:00
#include "base/ccMacros.h"
#include "base/CCPlatformMacros.h"
#include "platform/CCFileUtils.h"
NS_CC_BEGIN
/**
* Sets the inverse bind pose matrix.
*
* @param m C3DMatrix representing the inverse bind pose for this Bone.
*/
2014-07-11 10:17:36 +08:00
void Bone3D::setInverseBindPose(const Mat4& m)
2014-06-04 18:17:09 +08:00
{
2014-06-17 10:49:52 +08:00
_invBindPose = m;
2014-06-04 18:17:09 +08:00
}
2014-07-11 10:17:36 +08:00
const Mat4& Bone3D::getInverseBindPose()
2014-06-04 18:17:09 +08:00
{
2014-06-17 10:49:52 +08:00
return _invBindPose;
2014-06-04 18:17:09 +08:00
}
2014-07-11 10:17:36 +08:00
void Bone3D::setOriPose(const Mat4& m)
2014-06-18 17:16:54 +08:00
{
_oriPose = m;
}
2014-07-11 10:17:36 +08:00
void Bone3D::resetPose()
2014-06-18 17:16:54 +08:00
{
2014-06-25 11:35:55 +08:00
_local =_oriPose;
2014-06-18 17:16:54 +08:00
for (auto it : _children) {
it->resetPose();
}
}
2014-07-11 10:17:36 +08:00
void Bone3D::setWorldMatDirty(bool dirty)
2014-06-05 18:21:25 +08:00
{
2014-06-16 00:01:38 +08:00
_worldDirty = dirty;
for (auto it : _children) {
it->setWorldMatDirty(dirty);
2014-06-05 18:21:25 +08:00
}
}
2014-06-04 18:17:09 +08:00
//update own world matrix and children's
2014-07-11 10:17:36 +08:00
void Bone3D::updateWorldMat()
2014-06-04 18:17:09 +08:00
{
getWorldMat();
for (auto itor : _children) {
2014-06-15 22:45:56 +08:00
itor->updateWorldMat();
2014-06-04 18:17:09 +08:00
}
}
2014-07-11 10:17:36 +08:00
const Mat4& Bone3D::getWorldMat()
2014-06-04 18:17:09 +08:00
{
2014-06-16 16:45:43 +08:00
if (_worldDirty)
2014-06-04 18:17:09 +08:00
{
2014-06-16 00:26:31 +08:00
updateLocalMat();
2014-06-05 18:21:25 +08:00
if (_parent)
{
_world = _parent->getWorldMat() * _local;
}
else
_world = _local;
2014-06-16 00:26:31 +08:00
_worldDirty = false;
2014-06-04 18:17:09 +08:00
}
2014-06-05 18:21:25 +08:00
2014-06-04 18:17:09 +08:00
return _world;
}
2014-07-11 10:17:36 +08:00
void Bone3D::setAnimationValue(float* trans, float* rot, float* scale, void* tag, float weight)
2014-06-04 18:17:09 +08:00
{
2014-06-20 17:55:25 +08:00
for (auto& it : _blendStates) {
if (it.tag == tag)
{
if (trans)
it.localTranslate.set(trans);
if (rot)
it.localRot.set(rot);
if (scale)
it.localScale.set(scale);
it.weight = weight;
return;
}
}
2014-06-13 19:20:19 +08:00
BoneBlendState state;
if (trans)
state.localTranslate.set(trans);
if (rot)
state.localRot.set(rot);
if (scale)
state.localScale.set(scale);
2014-06-16 18:22:32 +08:00
2014-06-13 19:20:19 +08:00
state.weight = weight;
2014-06-20 17:55:25 +08:00
state.tag = tag;
2014-06-13 19:20:19 +08:00
_blendStates.push_back(state);
2014-06-04 18:17:09 +08:00
}
2014-07-11 10:17:36 +08:00
void Bone3D::clearBoneBlendState()
2014-06-16 18:36:30 +08:00
{
_blendStates.clear();
for (auto it : _children) {
it->clearBoneBlendState();
}
}
2014-06-04 18:17:09 +08:00
/**
* Creates C3DBone.
*/
2014-07-11 10:17:36 +08:00
Bone3D* Bone3D::create(const std::string& id)
2014-06-04 18:17:09 +08:00
{
2014-07-11 10:17:36 +08:00
auto bone = new Bone3D(id);
2014-06-04 18:17:09 +08:00
bone->autorelease();
return bone;
}
2014-07-11 10:17:36 +08:00
void Bone3D::updateJointMatrix(Vec4* matrixPalette)
2014-06-04 18:17:09 +08:00
{
{
static Mat4 t;
Mat4::multiply(_world, getInverseBindPose(), &t);
2014-06-16 18:36:30 +08:00
2014-06-04 18:17:09 +08:00
matrixPalette[0].set(t.m[0], t.m[4], t.m[8], t.m[12]);
matrixPalette[1].set(t.m[1], t.m[5], t.m[9], t.m[13]);
matrixPalette[2].set(t.m[2], t.m[6], t.m[10], t.m[14]);
}
}
2014-07-11 10:17:36 +08:00
Bone3D* Bone3D::getParentBone()
2014-06-04 18:17:09 +08:00
{
return _parent;
}
2014-07-11 10:17:36 +08:00
ssize_t Bone3D::getChildBoneCount() const
2014-06-04 18:17:09 +08:00
{
return _children.size();
}
2014-07-11 10:17:36 +08:00
Bone3D* Bone3D::getChildBoneByIndex(int index)
2014-06-04 18:17:09 +08:00
{
return _children.at(index);
}
2014-07-11 10:17:36 +08:00
void Bone3D::addChildBone(Bone3D* bone)
2014-06-04 18:17:09 +08:00
{
if (_children.find(bone) == _children.end())
_children.pushBack(bone);
}
2014-07-11 10:17:36 +08:00
void Bone3D::removeChildBoneByIndex(int index)
2014-06-04 18:17:09 +08:00
{
_children.erase(index);
}
2014-07-11 10:17:36 +08:00
void Bone3D::removeChildBone(Bone3D* bone)
2014-06-04 18:17:09 +08:00
{
_children.eraseObject(bone);
}
2014-07-11 10:17:36 +08:00
void Bone3D::removeAllChildBone()
2014-06-04 18:17:09 +08:00
{
_children.clear();
}
2014-07-11 10:17:36 +08:00
Bone3D::Bone3D(const std::string& id)
2014-06-04 18:17:09 +08:00
: _name(id)
, _parent(nullptr)
2014-06-05 18:21:25 +08:00
, _worldDirty(true)
2014-06-04 18:17:09 +08:00
{
}
2014-07-11 10:17:36 +08:00
Bone3D::~Bone3D()
2014-06-04 18:17:09 +08:00
{
removeAllChildBone();
}
2014-07-11 10:17:36 +08:00
void Bone3D::updateLocalMat()
2014-06-04 18:17:09 +08:00
{
2014-06-13 19:20:19 +08:00
if (_blendStates.size())
2014-06-04 18:17:09 +08:00
{
2014-06-20 17:55:25 +08:00
Vec3 translate(Vec3::ZERO), scale(Vec3::ZERO);
Quaternion quat(Quaternion::zero());
2014-06-16 16:45:43 +08:00
2014-06-13 19:20:19 +08:00
float total = 0.f;
for (auto it: _blendStates) {
total += it.weight;
}
if (total)
{
2014-06-20 17:55:25 +08:00
if (_blendStates.size() == 1)
2014-06-13 19:20:19 +08:00
{
2014-06-20 17:55:25 +08:00
auto& state = _blendStates[0];
translate = state.localTranslate;
scale = state.localScale;
quat = state.localRot;
2014-06-13 19:20:19 +08:00
}
else
{
float invTotal = 1.f / total;
for (auto it : _blendStates) {
float weight = (it.weight * invTotal);
translate += it.localTranslate * weight;
2014-06-20 17:55:25 +08:00
scale.x += it.localScale.x * weight;
scale.y += it.localScale.y * weight;
scale.z += it.localScale.z * weight;
if (!quat.isZero())
2014-06-13 19:20:19 +08:00
{
2014-06-20 17:55:25 +08:00
Quaternion& q = _blendStates[0].localRot;
if (q.x * quat.x + q.y * quat.y + q.z * quat.z + q.w * quat.w < 0)
2014-06-13 19:20:19 +08:00
weight = -weight;
}
2014-06-20 17:55:25 +08:00
quat = Quaternion(it.localRot.x * weight + quat.x, it.localRot.y * weight + quat.y, it.localRot.z * weight + quat.z, it.localRot.w * weight + quat.w);
2014-06-13 19:20:19 +08:00
}
2014-06-20 17:55:25 +08:00
}
2014-06-13 19:20:19 +08:00
}
2014-06-16 00:26:31 +08:00
2014-06-16 16:45:43 +08:00
Mat4::createTranslation(translate, &_local);
_local.rotate(quat);
_local.scale(scale);
2014-06-16 00:26:31 +08:00
_blendStates.clear();
2014-06-04 18:17:09 +08:00
}
2014-06-13 09:12:29 +08:00
}
2014-06-04 18:17:09 +08:00
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int PALETTE_ROWS = 3;
MeshSkin::MeshSkin()
: _rootBone(nullptr)
, _matrixPalette(nullptr)
{
}
MeshSkin::~MeshSkin()
{
removeAllBones();
}
//create a new meshskin if do not want to share meshskin
MeshSkin* MeshSkin::create(const std::string& filename, const std::string& name)
{
//load skin here;
2014-06-09 18:37:58 +08:00
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
2014-06-16 19:17:07 +08:00
std::string key = fullPath + "#" + name;
const auto skindata = MeshSkinDataCache::getInstance()->getMeshSkinData(key);
if (skindata)
{
auto skin = new MeshSkin();
skin->initFromSkinData(*skindata);
skin->autorelease();
return skin;
}
else
2014-06-09 18:37:58 +08:00
{
2014-06-16 19:17:07 +08:00
auto instance = Bundle3D::getInstance();
bool ret = instance->load(fullPath);
if (ret)
2014-06-09 18:37:58 +08:00
{
2014-06-16 19:17:07 +08:00
SkinData data;
if (instance->loadSkinData(name, &data))
{
auto skin = new MeshSkin();
skin->initFromSkinData(data);
skin->autorelease();
2014-06-16 23:43:07 +08:00
MeshSkinDataCache::getInstance()->addMeshSkinData(key, data);
2014-06-16 19:17:07 +08:00
return skin;
2014-06-09 18:37:58 +08:00
}
}
}
2014-06-04 18:17:09 +08:00
2014-06-09 18:37:58 +08:00
return nullptr;
2014-06-04 18:17:09 +08:00
}
2014-06-16 19:17:07 +08:00
bool MeshSkin::initFromSkinData(const SkinData& skindata)
{
2014-06-19 12:01:03 +08:00
ssize_t i = 0;
2014-06-20 22:38:09 +08:00
for (; i < skindata.skinBoneNames.size(); i++) {
2014-07-11 10:17:36 +08:00
auto bone = Bone3D::create(skindata.skinBoneNames[i]);
2014-06-17 10:49:52 +08:00
bone->_invBindPose = skindata.inverseBindPoseMatrices[i];
2014-06-24 14:10:03 +08:00
bone->setOriPose(skindata.skinBoneOriginMatrices[i]);
2014-06-19 12:01:03 +08:00
addSkinBone(bone);
}
2014-06-20 22:38:09 +08:00
for (i = 0; i < skindata.nodeBoneNames.size(); i++) {
2014-07-11 10:17:36 +08:00
auto bone = Bone3D::create(skindata.nodeBoneNames[i]);
2014-06-24 14:10:03 +08:00
bone->setOriPose(skindata.nodeBoneOriginMatrices[i]);
2014-06-19 12:01:03 +08:00
addNodeBone(bone);
2014-06-16 19:17:07 +08:00
}
for (auto it : skindata.boneChild) {
auto parent = getBoneByIndex(it.first);
for (auto childIt : it.second) {
auto child = getBoneByIndex(childIt);
child->_parent = parent;
parent->_children.pushBack(child);
}
}
setRootBone(getBoneByIndex(skindata.rootBoneIndex));
2014-06-19 12:01:03 +08:00
_rootBone->resetPose();
2014-06-16 19:17:07 +08:00
return true;
}
2014-06-26 21:58:26 +08:00
ssize_t MeshSkin::getBoneCount() const
2014-06-04 18:17:09 +08:00
{
2014-06-26 21:58:26 +08:00
return _skinBones.size() + _nodeBones.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);
index -= _skinBones.size();
if (index < _nodeBones.size())
return _nodeBones.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
for (auto it : _skinBones) {
if (it->getName() == id)
return it;
}
//search from node bones
for (auto it : _nodeBones) {
2014-06-04 18:17:09 +08:00
if (it->getName() == id )
return it;
}
return nullptr;
}
2014-07-11 10:17:36 +08:00
Bone3D* MeshSkin::getRootBone() const
2014-06-04 18:17:09 +08:00
{
return _rootBone;
}
2014-07-11 10:17:36 +08:00
void MeshSkin::setRootBone(Bone3D* joint)
2014-06-04 18:17:09 +08:00
{
2014-06-09 18:37:58 +08:00
CC_SAFE_RETAIN(joint);
CC_SAFE_RELEASE(_rootBone);
2014-06-04 18:17:09 +08:00
_rootBone = joint;
}
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-06-19 12:01:03 +08:00
int index = 0;
for (; index < _nodeBones.size(); index++, i++) {
if (_nodeBones.at(index) == bone)
2014-06-04 18:17:09 +08:00
return i;
}
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-09 18:37:58 +08:00
updateBoneMatrix();
2014-06-19 12:01:03 +08:00
if (_matrixPalette == nullptr)
{
_matrixPalette = new Vec4[_skinBones.size() * PALETTE_ROWS];
}
2014-06-04 18:17:09 +08:00
int i = 0;
2014-06-19 12:01:03 +08:00
for (auto it : _skinBones )
2014-06-04 18:17:09 +08:00
{
2014-06-17 10:49:52 +08:00
it->updateJointMatrix(&_matrixPalette[i++ * PALETTE_ROWS]);
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
}
//refresh bone world matrix
void MeshSkin::updateBoneMatrix()
{
2014-06-16 00:01:38 +08:00
_rootBone->setWorldMatDirty(true);
2014-06-04 18:17:09 +08:00
_rootBone->updateWorldMat();
}
void MeshSkin::removeAllBones()
{
2014-06-19 12:01:03 +08:00
_skinBones.clear();
_nodeBones.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-07-11 10:17:36 +08:00
void MeshSkin::addNodeBone(Bone3D* bone)
2014-06-04 18:17:09 +08:00
{
2014-06-19 12:01:03 +08:00
_nodeBones.pushBack(bone);
2014-06-04 18:17:09 +08:00
}
////////////////////////////////////////////////////////////////////////
2014-06-16 19:17:07 +08:00
MeshSkinDataCache* MeshSkinDataCache::_cacheInstance = nullptr;
2014-06-04 18:17:09 +08:00
2014-06-16 19:17:07 +08:00
MeshSkinDataCache* MeshSkinDataCache::getInstance()
2014-06-04 18:17:09 +08:00
{
if (_cacheInstance == nullptr)
2014-06-16 19:17:07 +08:00
_cacheInstance = new MeshSkinDataCache();
2014-06-04 18:17:09 +08:00
return _cacheInstance;
}
2014-06-17 19:18:56 +08:00
void MeshSkinDataCache::destroyInstance()
2014-06-04 18:17:09 +08:00
{
if (_cacheInstance)
{
CC_SAFE_DELETE(_cacheInstance);
}
}
2014-06-16 19:17:07 +08:00
const SkinData* MeshSkinDataCache::getMeshSkinData(const std::string& key) const
2014-06-04 18:17:09 +08:00
{
2014-06-16 19:17:07 +08:00
auto it = _skinDatas.find(key);
if (it != _skinDatas.end())
return &it->second;
2014-06-04 18:17:09 +08:00
return nullptr;
}
2014-06-16 19:17:07 +08:00
bool MeshSkinDataCache::addMeshSkinData(const std::string& key, const SkinData& skin)
2014-06-04 18:17:09 +08:00
{
2014-06-16 19:17:07 +08:00
if (_skinDatas.find(key) != _skinDatas.end())
2014-06-04 18:17:09 +08:00
return false; // already have this key
2014-06-16 19:17:07 +08:00
_skinDatas[key] = skin;
2014-06-04 18:17:09 +08:00
return true;
}
2014-06-16 20:58:13 +08:00
void MeshSkinDataCache::removeAllMeshSkinData()
2014-06-04 18:17:09 +08:00
{
2014-06-16 19:17:07 +08:00
_skinDatas.clear();
2014-06-04 18:17:09 +08:00
}
2014-06-16 19:17:07 +08:00
MeshSkinDataCache::MeshSkinDataCache()
2014-06-04 18:17:09 +08:00
{
}
2014-06-16 19:17:07 +08:00
MeshSkinDataCache::~MeshSkinDataCache()
2014-06-04 18:17:09 +08:00
{
}
NS_CC_END