-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.cpp
112 lines (97 loc) · 2.7 KB
/
transform.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
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
101
102
103
104
105
106
107
108
109
110
111
112
#include "transform.h"
Transform::Transform(glm::vec3 pos, glm::quat rot, glm::vec3 scale,
glm::vec3 color, Visibility visibility, Shape shape, Transform* parent) :
pos(pos), rot(rot), scale(scale), color(color), visibility(visibility), shape(shape), parent(parent) {
}
Transform::~Transform() {
}
void Transform::setPos(glm::vec3 pos) {
this->pos = pos;
needUpdate = true;
}
void Transform::setPos(float x, float y, float z) {
pos = glm::vec3(x, y, z);
needUpdate = true;
}
void Transform::addPos(glm::vec3 add) {
pos += add;
needUpdate = true;
}
void Transform::setScale(glm::vec3 scale) {
this->scale = scale;
needUpdate = true;
}
void Transform::setScale(float x, float y, float z) {
scale = glm::vec3(x, y, z);
needUpdate = true;
}
void Transform::setRot(glm::vec3 euler) {
rot = glm::quat(euler*DEGREESTORADS);
needUpdate = true;
}
void Transform::setRot(float x, float y, float z) {
rot = glm::quat(glm::vec3(x, y, z)*DEGREESTORADS);
needUpdate = true;
}
void Transform::setRot(glm::quat q) {
rot = q;
needUpdate = true;
}
void Transform::setByBounds(AABB bounds) {
pos = bounds.getCenter();
scale = bounds.getSize();
needUpdate = true;
}
// if you dont normalize things rotating fast will starting stretching so weird haha
void Transform::rotate(float angle, glm::vec3 axis) {
rot *= glm::normalize(glm::angleAxis(angle*DEGREESTORADS, axis));
needUpdate = true;
}
glm::vec3 Transform::getWorldPos() const {
if (parent != nullptr) {
return glm::vec3(parent->getModelMatrix() * glm::vec4(pos, 1.0f));
}
return pos;
}
glm::vec3 Transform::getWorldScale() const { // world scale?
if (parent != nullptr) {
return parent->getWorldScale() * scale;
}
return scale;
}
glm::quat Transform::getWorldRot() const {
if (parent != nullptr) {
return parent->getWorldRot() * rot;
}
return rot;
}
glm::mat4 Transform::getModelMatrix() {
if (needUpdate) { // recalculate model matrix if needs update
model = glm::mat4();
model = glm::translate(model, pos);
model *= glm::toMat4(rot);
model = glm::scale(model, scale);
needUpdate = false;
}
if (parent != nullptr) {
return parent->getModelMatrix() * model;
}
return model;
}
bool Transform::shouldDraw() const {
if (visibility != Visibility::SHOW_SELF) {
return false;
}
Transform* p = parent;
while (p != nullptr) {
if (p->visibility != Visibility::HIDE_ALL) {
p = p->parent;
} else {
return false;
}
}
return true;
}
void Transform::setVisibility(Visibility visibility) {
this->visibility = visibility;
}