Files
2025-05-13 02:46:21 +03:00

117 lines
2.4 KiB
C++

#ifndef Skeleton_h__
#define Skeleton_h__
#include <map>
#include "Bone.h"
#include "AnimationTrack.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
namespace LifeGraphics
{
class Skeleton
{
public:
int maxBoneID;
int BonesCount;
std::map<int, Bone*> Bones;
Skeleton()
:maxBoneID(0)
{
}
Bone* AddBone(Bone* bone)
{
Bones[bone->ID] = bone;
return bone;
}
void rebuildLinks()
{
//clear all dependencies
for(std::map<int, Bone*>::iterator it = Bones.begin(); it != Bones.end(); it++)
{
it->second->clearChilds();
it->second->Parent = 0;
}
//rebuild hierarchy links
for(std::map<int, Bone*>::iterator it = Bones.begin(); it != Bones.end(); it++)
{
if(it->second->ParentId != -2)
{
it->second->Parent = GetBone(it->second->ParentId);
it->second->Parent->AddChild(it->second);
}
}
}
void UpdateTransform()
{
for (int i = 0; i < BonesCount; i++)
{
LifeMath::float4x4 BoneLocal;
LifeMath::float4x4 BoneBase;
Bone* bone = Bones[i];
BoneLocal = bone->LocalTransform.GetMatrix();
BoneBase = bone->BaseTransform.GetMatrix();
BoneBase = Mat4x4Invert(&BoneBase);
//BoneLocal = BoneBase * BoneLocal;
if (bone->Parent)
{
//bone->TransformMatrix = BoneLocal * bone->Parent->AbsoluteMatrix;
bone->TransformMatrix = BoneLocal * bone->Parent->TransformMatrix;
}
else
{
bone->TransformMatrix = BoneLocal;
}
//bone->AbsoluteMatrix = BoneBase * bone->TransformMatrix;
bone->AbsoluteMatrix = BoneBase* bone->TransformMatrix;
bone->Update();
}
}
LifeMath::float4x4 BuildMatrix(BoneTransform* transform)
{
LifeMath::float4x4 rot;
LifeMath::float4x4 trans;
Mat4x4RotationQuaternion(transform->Rotation, rot);
Mat4x4Translation(transform->Translation, trans);
return rot*trans;
}
void GetMatrices(int* boneIndices, int indexCount, LifeMath::float4x4* result)
{
for (int i = 0; i < indexCount; i++)
result[i] = GetBone(boneIndices[i])->AbsoluteMatrix;
}
void GetMatrices(LifeMath::float4x4* result)
{
for (int i = 0; i < BonesCount; i++)
result[i] = GetBone(i)->AbsoluteMatrix;
}
Bone* GetBone(int boneID)
{
return Bones[boneID];
}
Bone* GetBone(String& name)
{
for(std::map<int, Bone*>::iterator it = Bones.begin(); it != Bones.end(); it++)
{
if (it->second->BoneName == name)
return it->second;
}
}
};
}
#endif // Skeleton_h__