axmol/cocos/3d/CCMeshSkin.h

278 lines
7.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.
****************************************************************************/
#ifndef __CCMESHSKIN_H__
#define __CCMESHSKIN_H__
#include <unordered_map>
2014-06-17 10:49:52 +08:00
#include "3d/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();
2014-06-19 14:11:14 +08:00
/**update own world matrix and children's*/
2014-06-04 18:17:09 +08:00
void updateWorldMat();
2014-06-19 14:11:14 +08:00
/**get wrod matrix*/
2014-06-04 18:17:09 +08:00
const Mat4& getWorldMat();
2014-06-19 14:11:14 +08:00
/**get bone name*/
2014-06-04 18:17:09 +08:00
const std::string& getName() const { return _name; }
2014-06-20 17:55:25 +08:00
/**
* set animation value
* @param trans translate vec3
* @param rot rotation quaternion
* @param scale scale vec3
* @param tag, unique tag, only blend animation between different tags
* @param weight, blend weight
*/
void setAnimationValue(float* trans, float* rot, float* scale, void* tag = nullptr, float weight = 1.0f);
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
/**clear bone blend states*/
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.
*
2014-06-18 17:16:54 +08:00
* @param m Mat4 representing the inverse bind pose for this Bone.
2014-06-04 18:17:09 +08:00
*/
void setInverseBindPose(const Mat4& m);
2014-06-18 17:16:54 +08:00
/**
* Sets the bone's original pose.
*
* @param m Mat4 representing the original pose for this Bone.
*/
void setOriPose(const Mat4& m);
/**
* reset pose to origin
*/
void resetPose();
2014-06-04 18:17:09 +08:00
/**
* Updates the joint matrix.
*
* @param matrixPalette The matrix palette to update.
*/
2014-06-17 10:49:52 +08:00
void updateJointMatrix(Vec4* matrixPalette);
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
/**bone tree, we do not inherit from Node, Node has too many properties that we do not need. A clean Node is needed.*/
2014-06-04 18:17:09 +08:00
Bone* getParentBone();
2014-06-19 14:11:14 +08:00
/**get child bone count*/
2014-06-20 12:01:54 +08:00
ssize_t getChildBoneCount() const;
2014-06-19 14:11:14 +08:00
/**get child bone by index*/
2014-06-04 18:17:09 +08:00
Bone* getChildBoneByIndex(int index);
2014-06-19 14:11:14 +08:00
/**add child bone*/
2014-06-04 18:17:09 +08:00
void addChildBone(Bone* bone);
2014-06-19 14:11:14 +08:00
/**remove child bone by index*/
2014-06-04 18:17:09 +08:00
void removeChildBoneByIndex(int index);
2014-06-19 14:11:14 +08:00
/**remove child bone*/
2014-06-04 18:17:09 +08:00
void removeChildBone(Bone* bone);
2014-06-19 14:11:14 +08:00
/**remove all child bone*/
2014-06-04 18:17:09 +08:00
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-20 17:55:25 +08:00
void* tag; //
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-20 17:55:25 +08:00
, tag(nullptr)
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();
2014-06-19 14:11:14 +08:00
/**set world matrix dirty flag*/
void setWorldMatDirty(bool dirty = true);
std::string _name; // bone name
2014-06-04 18:17:09 +08:00
/**
* The Mat4 representation of the Joint's bind pose.
*/
2014-06-17 10:49:52 +08:00
Mat4 _invBindPose;
2014-06-04 18:17:09 +08:00
2014-06-18 17:16:54 +08:00
Mat4 _oriPose; //original bone pose
2014-06-19 14:11:14 +08:00
Bone* _parent; //parent bone
2014-06-04 18:17:09 +08:00
Vector<Bone*> _children;
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
};
2014-06-19 14:11:14 +08:00
/**
* MeshSkin, A class maintain a collection of bones that affect Mesh vertex.
* And it is responsible for computing matrix palletes that used by skin mesh rendering.
*/
2014-06-04 18:17:09 +08:00
class MeshSkin: public Ref
{
public:
2014-06-19 14:11:14 +08:00
/**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
2014-06-26 21:58:26 +08:00
/**get total bone count, skin bone + node bone*/
ssize_t getBoneCount() const;
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
/**get bone*/
2014-06-04 18:17:09 +08:00
Bone* getBoneByIndex(unsigned int index) const;
Bone* getBoneByName(const std::string& id) const;
2014-06-19 14:11:14 +08:00
/**get & set root bone*/
2014-06-04 18:17:09 +08:00
Bone* getRootBone() const;
2014-06-19 12:01:03 +08:00
void setRootBone(Bone* bone);
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
/**get bone index*/
2014-06-19 12:01:03 +08:00
int getBoneIndex(Bone* bone) const;
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
/**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
2014-06-19 14:11:14 +08:00
/**getSkinBoneCount() * 3*/
2014-06-19 12:01:03 +08:00
ssize_t getMatrixPaletteSize() const;
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
/**refresh bone world matrix*/
2014-06-04 18:17:09 +08:00
void updateBoneMatrix();
2014-06-17 19:18:56 +08:00
CC_CONSTRUCTOR_ACCESS:
2014-06-04 18:17:09 +08:00
MeshSkin();
~MeshSkin();
2014-06-19 14:11:14 +08:00
/**init from skin data*/
2014-06-16 19:17:07 +08:00
bool initFromSkinData(const SkinData& skindata);
2014-06-19 14:11:14 +08:00
/**remove all bones*/
2014-06-04 18:17:09 +08:00
void removeAllBones();
2014-06-19 14:11:14 +08:00
/**add skin bone*/
2014-06-19 12:01:03 +08:00
void addSkinBone(Bone* bone);
2014-06-19 14:11:14 +08:00
/**add Node bone*/
2014-06-19 12:01:03 +08:00
void addNodeBone(Bone* bone);
2014-06-04 18:17:09 +08:00
2014-06-17 19:18:56 +08:00
protected:
2014-06-19 12:01:03 +08:00
Vector<Bone*> _skinBones; // bones with skin
Vector<Bone*> _nodeBones; //bones without skin, only used to compute transform of children
2014-06-04 18:17:09 +08:00
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.
2014-06-19 12:01:03 +08:00
// The number of Vec4's is (_skinBones.size() * 3).
2014-06-04 18:17:09 +08:00
Vec4* _matrixPalette;
};
2014-06-19 14:11:14 +08:00
/**
* MeshSkinData Cache
*/
2014-06-16 19:17:07 +08:00
class MeshSkinDataCache
2014-06-04 18:17:09 +08:00
{
public:
2014-06-19 14:11:14 +08:00
/**get & destroy*/
2014-06-16 19:17:07 +08:00
static MeshSkinDataCache* getInstance();
2014-06-17 19:18:56 +08:00
static void destroyInstance();
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
/**get mesh skin data from cache*/
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-19 14:11:14 +08:00
/**add mesh skin data to cache*/
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
2014-06-19 14:11:14 +08:00
/**remove all mesh skin data*/
2014-06-16 20:58:13 +08:00
void removeAllMeshSkinData();
2014-06-04 18:17:09 +08:00
protected:
2014-06-16 19:17:07 +08:00
MeshSkinDataCache();
~MeshSkinDataCache();
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
static MeshSkinDataCache* _cacheInstance; // instance
2014-06-04 18:17:09 +08:00
2014-06-19 14:11:14 +08:00
std::unordered_map<std::string, SkinData> _skinDatas; //cached skindatas
2014-06-04 18:17:09 +08:00
};
NS_CC_END
#endif // __CCSKIN_H__