-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkinnedData.cpp
More file actions
executable file
·100 lines (80 loc) · 2.67 KB
/
SkinnedData.cpp
File metadata and controls
executable file
·100 lines (80 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "SkinnedData.h"
#include "MeshGeometry.h"
float AnimationClip::GetClipStartTime()const
{
//找到所有骨骼中最小的开始时间
float t = MathHelper::Infinity;
for (UINT i = 0; i < BoneAnimations.size(); i++)
{
t = MathHelper::Min(t, BoneAnimations[i].GetStartTime());
}
return t;
}
float AnimationClip::GetClipEndTime()const
{
// 找到所有骨骼中最大的结束时间
float t = 0.0f;
for (UINT i = 0; i < BoneAnimations.size(); i++)
{
t = MathHelper::Max(t,BoneAnimations[i].GetEndTime());
}
return t;
}
void AnimationClip::Interpolate(float t, std::vector<XMFLOAT4X4>& boneTransforms)const
{
for (UINT i = 0; i < BoneAnimations.size(); i++)
{
BoneAnimations[i].Interpolate(t,boneTransforms[i]);
}
}
float SkinnedData::GetClipStartTime(const std::string& clipName)const
{
auto clip = mAnimations.find(clipName);
return clip->second.GetClipStartTime();
}
float SkinnedData::GetClipEndTime(const std::string& clipName)const
{
auto clip = mAnimations.find(clipName);
return clip->second.GetClipEndTime();
}
UINT SkinnedData::BoneCount()const
{
return mBoneHierachy.size();
}
void SkinnedData::Set(std::vector<int>& boneHierachy,
std::vector<XMFLOAT4X4>& boneOffsets,
std::map<std::string, AnimationClip>& animations)
{
mBoneHierachy = boneHierachy;
mBoneOffsets = boneOffsets;
mAnimations = animations;
}
void SkinnedData::GetFinalTransform(const std::string& clipName,float timePos, std::vector<XMFLOAT4X4>& finalTransform)const
{
UINT numBones = mBoneOffsets.size();
std::vector<XMFLOAT4X4> toParentTransforms(numBones);
//计算clip中给定时间的所有骨骼的transform。
auto clip = mAnimations.find(clipName);
clip->second.Interpolate(timePos,toParentTransforms);
//遍历整个层级关系,将所有的骨骼移到root空间
std::vector<XMFLOAT4X4> toRootTransforms(numBones);
//根骨骼索引为0,根骨骼没有父级,所以它的toParentTransform就是它本身的变换
toRootTransforms[0] = toParentTransforms[0];
//找到子级的toRootTransform
for (UINT i = 1; i < numBones; i++)
{
XMMATRIX toParent = XMLoadFloat4x4(&toParentTransforms[i]);
//这里要保证mBoneHierachy中的索引值从小到大,祖先元素比子元素先计算
int parentIndex = mBoneHierachy[i];
XMMATRIX parentToRoot = XMLoadFloat4x4(&toRootTransforms[parentIndex]);
XMMATRIX toRoot = XMMatrixMultiply(toParent,parentToRoot);
XMStoreFloat4x4(&toRootTransforms[i],toRoot);
}
//左乘bone的offset得到finalTransform。
for (UINT i = 0; i < numBones; i++)
{
XMMATRIX offset = XMLoadFloat4x4(&mBoneOffsets[i]);
XMMATRIX toRoot = XMLoadFloat4x4(&toRootTransforms[i]);
XMStoreFloat4x4(&finalTransform[i],XMMatrixMultiply(offset,toRoot));
}
}