-
Notifications
You must be signed in to change notification settings - Fork 28
/
Skeleton.cpp
58 lines (45 loc) · 1.48 KB
/
Skeleton.cpp
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
// Copyright 2019-2020 the donut authors. See AUTHORS.md
#include <P3D/P3D.generated.h>
#include <Render/SkinAnimation.h>
#include <Skeleton.h>
#include <cassert>
namespace Donut
{
Skeleton::Skeleton(const P3D::Skeleton& skeleton): _name(skeleton.GetName())
{
assert(skeleton.GetVersion() == 0);
uint32_t jointIndex = 0;
_joints.reserve(skeleton.GetNumJoints());
for (const auto& joint : skeleton.GetJoints())
{
_joints.emplace_back(joint->GetName(), joint->GetParent(), joint->GetRestPose());
_jointNameIndexMap[joint->GetName()] = jointIndex++;
}
// calculate global transforms through the hierarchy
for (auto i = 0; i < _joints.size(); i++)
_joints[i].restGlobalInverse = _joints[_joints[i].parent].restGlobalInverse * _joints[i].rest;
// inverse those globals afterwards
for (auto i = 0; i < _joints.size(); i++) _joints[i].restGlobalInverse = _joints[i].restGlobalInverse.Inverse();
}
void Skeleton::ResetPose()
{
for (auto& joint : _joints) joint.pose = Matrix4x4::Identity;
updateJoints();
}
void Skeleton::UpdatePose(SkinAnimation& animation, double time)
{
time *= animation.GetFrameRate();
_joints[0].pose = Matrix4x4::Identity;
for (auto i = 0; i < _joints.size(); i++)
_joints[i].pose = _joints[_joints[i].parent].pose * animation.Evaluate(i, static_cast<float>(time));
updateJoints();
}
void Skeleton::updateJoints()
{
for (auto i = 0; i < _joints.size(); i++)
{
Joint& j = _joints[i];
j.finalGlobal = j.pose * j.restGlobalInverse;
}
}
} // namespace Donut