axmol/core/3d/CCSkeleton3D.h

230 lines
5.7 KiB
C
Raw Normal View History

2014-07-30 18:32:34 +08:00
/****************************************************************************
Copyright (c) 2014-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2014-07-30 18:32:34 +08:00
https://axis-project.github.io/
2014-07-30 18:32:34 +08:00
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 __CCSKELETON3D_H__
#define __CCSKELETON3D_H__
#include "3d/CCBundle3DData.h"
#include "base/CCRef.h"
#include "base/CCVector.h"
2014-08-29 15:39:52 +08:00
NS_AX_BEGIN
2014-07-30 18:32:34 +08:00
/**
2015-03-27 14:10:41 +08:00
* @addtogroup _3d
* @{
*/
/**
2015-09-09 11:37:41 +08:00
* @brief Defines a basic hierarchical structure of transformation spaces.
2015-03-30 15:47:47 +08:00
* @lua NA
2014-07-30 18:32:34 +08:00
*/
2014-10-09 09:30:23 +08:00
class CC_DLL Bone3D : public Ref
2014-07-30 18:32:34 +08:00
{
friend class Skeleton3D;
friend class MeshSkin;
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
public:
/**
* Returns the inverse bind pose matrix for this joint.
*
* @return Inverse bind pose matrix.
*/
const Mat4& getInverseBindPose();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**update own world matrix and children's*/
void updateWorldMat();
2021-12-25 10:04:45 +08:00
2015-09-09 11:37:41 +08:00
/**get world matrix*/
2014-07-30 18:32:34 +08:00
const Mat4& getWorldMat();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**get bone name*/
2021-12-26 23:26:34 +08:00
std::string_view getName() const { return _name; }
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +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
2014-07-30 18:32:34 +08:00
*/
void setAnimationValue(float* trans, float* rot, float* scale, void* tag = nullptr, float weight = 1.0f);
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**clear bone blend states*/
void clearBoneBlendState();
/**
* Creates C3DBone.
*/
2021-12-26 23:26:34 +08:00
static Bone3D* create(std::string_view id);
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**
* Sets the inverse bind pose matrix.
*
* @param m Mat4 representing the inverse bind pose for this Bone.
*/
void setInverseBindPose(const Mat4& m);
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**
* Sets the bone's original pose.
*
* @param m Mat4 representing the original pose for this Bone.
*/
void setOriPose(const Mat4& m);
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**
* reset pose to origin
*/
void resetPose();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**
* Updates the joint matrix.
*
* @param matrixPalette The matrix palette to update.
*/
void updateJointMatrix(Vec4* matrixPalette);
2021-12-25 10:04:45 +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-07-30 18:32:34 +08:00
Bone3D* getParentBone();
/**get child bone count*/
ssize_t getChildBoneCount() const;
/**get child bone by index*/
2014-07-31 18:14:01 +08:00
Bone3D* getChildBoneByIndex(int index) const;
2014-07-30 18:32:34 +08:00
/**add child bone*/
void addChildBone(Bone3D* bone);
/**remove child bone by index*/
void removeChildBoneByIndex(int index);
/**remove child bone*/
void removeChildBone(Bone3D* bone);
/**remove all child bone*/
void removeAllChildBone();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
protected:
2015-03-20 17:20:22 +08:00
/**
* the BoneBlendState struct
* @brief store the transformation and weight for bone blending
*/
2014-07-30 18:32:34 +08:00
struct BoneBlendState
{
2021-12-25 10:04:45 +08:00
Vec3 localTranslate;
Quaternion localRot;
Vec3 localScale;
float weight;
void* tag; //
BoneBlendState() : localRot(Quaternion::identity()), localScale(Vec3::ONE), weight(1.f), tag(nullptr) {}
2014-07-30 18:32:34 +08:00
};
2021-12-25 10:04:45 +08:00
/**
2014-07-30 18:32:34 +08:00
* Constructor.
*/
2021-12-26 23:26:34 +08:00
Bone3D(std::string_view id);
2021-12-25 10:04:45 +08:00
/**
2014-07-30 18:32:34 +08:00
* Destructor.
*/
virtual ~Bone3D();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**
* Update local matrix
*/
void updateLocalMat();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**set world matrix dirty flag*/
void setWorldMatDirty(bool dirty = true);
2021-12-25 10:04:45 +08:00
std::string _name; // bone name
2014-07-30 18:32:34 +08:00
/**
* The Mat4 representation of the Joint's bind pose.
*/
Mat4 _invBindPose;
2021-12-25 10:04:45 +08:00
Mat4 _oriPose; // original bone pose
Bone3D* _parent; // parent bone
2014-07-30 18:32:34 +08:00
Vector<Bone3D*> _children;
2021-12-25 10:04:45 +08:00
bool _worldDirty;
Mat4 _world;
Mat4 _local;
2014-07-30 18:32:34 +08:00
std::vector<BoneBlendState> _blendStates;
};
/**
* Skeleton
*
*/
2021-12-25 10:04:45 +08:00
class CC_DLL Skeleton3D : public Ref
2014-07-30 18:32:34 +08:00
{
public:
2015-03-30 15:47:47 +08:00
/**
* @lua NA
*/
2014-08-15 17:41:04 +08:00
static Skeleton3D* create(const std::vector<NodeData*>& skeletondata);
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**get total bone count*/
ssize_t getBoneCount() const;
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**get bone*/
Bone3D* getBoneByIndex(unsigned int index) const;
2021-12-26 23:26:34 +08:00
Bone3D* getBoneByName(std::string_view id) const;
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**get & set root bone*/
2014-08-15 15:55:21 +08:00
ssize_t getRootCount() const;
Bone3D* getRootBone(int index) const;
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**get bone index*/
int getBoneIndex(Bone3D* bone) const;
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**refresh bone world matrix*/
void updateBoneMatrix();
2021-12-25 10:04:45 +08:00
Skeleton3D();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
~Skeleton3D();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**remove all bones*/
void removeAllBones();
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
/**add bone*/
void addBone(Bone3D* bone);
2021-12-25 10:04:45 +08:00
2014-08-15 17:41:04 +08:00
/** create Bone3D from NodeData */
2014-08-16 16:56:04 +08:00
Bone3D* createBone3D(const NodeData& nodedata);
2021-12-25 10:04:45 +08:00
2014-07-30 18:32:34 +08:00
protected:
2021-12-25 10:04:45 +08:00
Vector<Bone3D*> _bones; // bones
2014-07-30 18:32:34 +08:00
2014-08-15 17:41:04 +08:00
Vector<Bone3D*> _rootBones;
2014-07-30 18:32:34 +08:00
};
2015-04-01 10:23:53 +08:00
// end of 3d group
2015-03-27 14:10:41 +08:00
/// @}
NS_AX_END
2014-07-30 18:32:34 +08:00
2021-12-25 10:04:45 +08:00
#endif // __CCSKELETON3D_H__