axmol/cocos/3d/CCMeshSkin.h

230 lines
5.9 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.
****************************************************************************/
#ifndef __CCMESHSKIN_H__
#define __CCMESHSKIN_H__
#include <unordered_map>
2014-06-16 19:17:07 +08:00
#include "CCBundle3DData.h"
2014-06-04 18:17:09 +08:00
#include "base/ccMacros.h"
#include "base/CCRef.h"
#include "base/CCVector.h"
#include "base/ccTypes.h"
#include "math/CCMath.h"
NS_CC_BEGIN
/**
* Defines a basic hierachial structure of transformation spaces.
*/
class Bone : public Ref
{
2014-06-09 18:37:58 +08:00
friend class MeshSkin;
2014-06-04 18:17:09 +08:00
public:
/**
* Returns the inverse bind pose matrix for this joint.
*
* @return Inverse bind pose matrix.
*/
const Mat4& getInverseBindPose();
//update own world matrix and children's
void updateWorldMat();
2014-06-16 00:01:38 +08:00
void setWorldMatDirty(bool dirty = true);
2014-06-04 18:17:09 +08:00
const Mat4& getWorldMat();
const std::string& getName() const { return _name; }
2014-06-13 19:20:19 +08:00
void setAnimationValue(float* trans, float* rot, float* scale, float weight = 1.0f);
2014-06-04 18:17:09 +08:00
2014-06-16 18:36:30 +08:00
void clearBoneBlendState();
2014-06-04 18:17:09 +08:00
/**
* Creates C3DBone.
*/
static Bone* create(const std::string& id);
/**
* Sets the inverse bind pose matrix.
*
* @param m C3DMatrix representing the inverse bind pose for this Bone.
*/
void setInverseBindPose(const Mat4& m);
/**
* Updates the joint matrix.
*
* @param bindShape The bind shape matrix.
* @param matrixPalette The matrix palette to update.
*/
void updateJointMatrix(const Mat4& bindShape, Vec4* matrixPalette);
//bone tree, we do not inherit from Node, Node has too many properties that we do not need. A clean Node is needed.
Bone* getParentBone();
int getChildBoneCount() const;
Bone* getChildBoneByIndex(int index);
void addChildBone(Bone* bone);
void removeChildBoneByIndex(int index);
void removeChildBone(Bone* bone);
void removeAllChildBone();
protected:
2014-06-13 09:12:29 +08:00
2014-06-12 18:32:47 +08:00
struct BoneBlendState
{
Vec3 localTranslate;
Quaternion localRot;
Vec3 localScale;
float weight;
2014-06-16 15:45:58 +08:00
BoneBlendState()
2014-06-16 16:45:43 +08:00
: localTranslate(Vec3::ZERO)
, localRot(Quaternion::identity())
, localScale(Vec3::ONE)
, weight(1.f)
2014-06-16 15:45:58 +08:00
{
}
2014-06-04 18:17:09 +08:00
};
/**
* Constructor.
*/
Bone(const std::string& id);
/**
* Destructor.
*/
virtual ~Bone();
/**
* Update local matrix
*/
void updateLocalMat();
std::string _name;
/**
* The Mat4 representation of the Joint's bind pose.
*/
Mat4 _bindPose;
Bone* _parent;
Vector<Bone*> _children;
2014-06-16 00:01:38 +08:00
bool _localDirty;
2014-06-05 18:21:25 +08:00
bool _worldDirty;
2014-06-04 18:17:09 +08:00
Mat4 _world;
Mat4 _local;
2014-06-12 18:32:47 +08:00
2014-06-13 09:12:29 +08:00
std::vector<BoneBlendState> _blendStates;
2014-06-13 19:20:19 +08:00
2014-06-04 18:17:09 +08:00
};
/////////////////////////////////////////////////////////////////////////////
class MeshSkin: public Ref
{
public:
//create a new meshskin if do not want to share meshskin
2014-06-06 19:12:08 +08:00
static MeshSkin* create(const std::string& filename, const std::string& name);
2014-06-04 18:17:09 +08:00
//get & set bind shape matrix
const Mat4& getBindShape() const;
void setBindShape(const float* matrix);
unsigned int getBoneCount() const;
void setBoneCount(int boneCount);
//get bone
Bone* getBoneByIndex(unsigned int index) const;
Bone* getBoneByName(const std::string& id) const;
//get & set root bone
Bone* getRootBone() const;
void setRootBone(Bone* joint);
int getBoneIndex(Bone* joint) const;
//compute matrix palette used by gpu skin
2014-06-09 18:37:58 +08:00
Vec4* getMatrixPalette();
2014-06-04 18:17:09 +08:00
//getBoneCount() * 3
unsigned int getMatrixPaletteSize() const;
//refresh bone world matrix
void updateBoneMatrix();
protected:
MeshSkin();
~MeshSkin();
2014-06-16 19:17:07 +08:00
bool initFromSkinData(const SkinData& skindata);
2014-06-04 18:17:09 +08:00
void removeAllBones();
void addBone(Bone* bone);
Mat4 _bindShape;
Vector<Bone*> _bones;
Bone* _rootBone;
// Pointer to the array of palette matrices.
// This array is passed to the vertex shader as a uniform.
// Each 4x3 row-wise matrix is represented as 3 Vec4's.
// The number of Vec4's is (_joints.size() * 3).
Vec4* _matrixPalette;
};
2014-06-16 19:17:07 +08:00
class MeshSkinDataCache
2014-06-04 18:17:09 +08:00
{
public:
2014-06-16 19:17:07 +08:00
static MeshSkinDataCache* getInstance();
2014-06-04 18:17:09 +08:00
static void purgeMeshSkinCache();
2014-06-16 19:17:07 +08:00
const SkinData* getMeshSkinData(const std::string& key) const;
2014-06-04 18:17:09 +08:00
2014-06-16 19:17:07 +08:00
bool addMeshSkinData(const std::string& key, const SkinData& skinData);
2014-06-04 18:17:09 +08:00
void removeAllMeshSkin();
void removeUnusedMeshSkin();
protected:
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
static MeshSkinDataCache* _cacheInstance;
2014-06-04 18:17:09 +08:00
2014-06-16 19:17:07 +08:00
std::unordered_map<std::string, SkinData> _skinDatas;
2014-06-04 18:17:09 +08:00
};
NS_CC_END
#endif // __CCSKIN_H__