Repository navigation
Expand file tree
/
Copy pathskeleton.cpp
More file actions
83 lines (71 loc) · 2.43 KB
/
Copy pathskeleton.cpp
File metadata and controls
83 lines (71 loc) · 2.43 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
#include "skeleton.h"
#include "model.h"
#include <glm/gtc/matrix_transform.hpp>
void Joint::updateWorldTransformation() {
if (parent == NULL) // root
{
jointWorldTransformation = jointLocalTransformation;
} else {
jointWorldTransformation =
parent->jointWorldTransformation * jointLocalTransformation;
}
}
Body::~Body() {
for (Drawable* d : drawables) {
delete d;
}
}
void Body::draw(
const GLuint& modelMatrixLocation,
const GLuint& viewMatrixLocation,
const GLuint& projectionMatrixLocation,
const glm::mat4 & viewMatrix, const glm::mat4 & projectionMatrix) {
joint->updateWorldTransformation();
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE,
&joint->jointWorldTransformation[0][0]);
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]);
glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE,
&projectionMatrix[0][0]);
for (Drawable* d : drawables) {
d->bind();
d->draw();
}
}
Skeleton::Skeleton(
GLuint modelMatrixLocation,
GLuint viewMatrixLocation,
GLuint projectionMatrixLocation) :
modelMatrixLocation(modelMatrixLocation),
viewMatrixLocation(viewMatrixLocation),
projectionMatrixLocation(projectionMatrixLocation) {
}
Skeleton::~Skeleton() {
for (auto body : bodies) {
delete body.second;
}
for (auto joint : joints) {
delete joint.second;
}
}
void Skeleton::setPose(const std::map<int, glm::mat4>& jointTransformations) {
for (const auto& tran : jointTransformations) {
joints[tran.first]->jointLocalTransformation = tran.second;
}
}
void Skeleton::draw(const glm::mat4 & viewMatrix, const glm::mat4 & projectionMatrix) {
for (auto& body : bodies) {
body.second->draw(modelMatrixLocation, viewMatrixLocation,
projectionMatrixLocation, viewMatrix, projectionMatrix);
}
}
std::map<int, glm::mat4> Skeleton::getJointWorldTransformations() {
std::map<int, glm::mat4> jointWorldTransformations;
// update before computing
for (auto joint : joints) {
joint.second->updateWorldTransformation();
}
for (auto joint : joints) {
jointWorldTransformations[joint.first] = joint.second->jointWorldTransformation;
}
return jointWorldTransformations;
}