-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.h
129 lines (106 loc) · 3.61 KB
/
transform.h
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>
#include <iostream>
#include "mathutil.h"
#include "aabb.h"
enum class Visibility {
SHOW_SELF, // show yourself
HIDE_SELF, // hide just yourself
HIDE_ALL // hide yourself and your children
};
enum class Shape {
CUBE_SOLID,
CUBE_GRID,
PYRAMID
};
class Transform {
public:
Transform(
glm::vec3 pos = glm::vec3(0.0f),
glm::quat rot = glm::quat(),
glm::vec3 scale = glm::vec3(1.0f),
glm::vec3 color = glm::vec3(1.0f),
Visibility visibility = Visibility::SHOW_SELF,
Shape shape = Shape::CUBE_GRID,
Transform* parent = nullptr
);
~Transform();
// set local position of transform
void setPos(glm::vec3 pos);
// set local position of transform
void setPos(float x, float y, float z);
// adds to position of transform
void addPos(glm::vec3 add);
// set local scale of transform
void setScale(glm::vec3 scale);
// set local scale of transform
void setScale(float x, float y, float z);
// set local rotation of transform by euler vector
void setRot(glm::vec3 euler);
// set local rotation of transform by euler values
void setRot(float x, float y, float z);
// set local rotation of transform by quaternion
void setRot(glm::quat q);
// sets up position and scale from provided AABB
void setByBounds(AABB bounds);
// rotate around axis by angle in degrees (local)
void rotate(float angle, glm::vec3 axis);
// returns world pos of transform (transformed by parent matrices)
glm::vec3 getWorldPos() const;
// returns world scale of transform
glm::vec3 getWorldScale() const;
// returns world rotation of transform
glm::quat getWorldRot() const;
// get local position
inline glm::vec3 getPos() const { return pos; }
// get local scale
inline glm::vec3 getScale() const { return scale; }
// get local rotation
inline glm::quat getRot() const { return rot; }
// get model matrix for rendering
glm::mat4 getModelMatrix();
bool shouldDraw() const;
void setVisibility(Visibility vis);
// variadic template mass parenting function
// kind of annoying that you need two functions, maybe theres better way
// modifies local position/scale based on scale of parent
// so you can define everything in world space
template<typename T>
void parentAll(T* first) { // base case
first->parent = this;
first->pos /= getWorldScale();
first->scale /= getWorldScale();
}
template<typename T, typename... R>
void parentAll(T* first, const R&... rest) { // recursive
first->parent = this;
first->pos /= getWorldScale();
first->scale /= getWorldScale();
parentAll(rest...);
}
// another parenting function that also assigns color to children
template<typename T>
void parentAllWithColor(T* first) { // base case
parentAll(first);
first->color = color;
}
template<typename T, typename... R>
void parentAllWithColor(T* first, const R&... rest) { // recursive
parentAll(first);
first->color = color;
parentAllWithColor(rest...);
}
glm::vec3 color;
Shape shape;
private:
glm::mat4 model; // local model matrix
glm::quat rot;
glm::vec3 pos;
glm::vec3 scale;
Transform* parent = nullptr;
Visibility visibility;
bool needUpdate = true; // set to true whenever model matrix needs to be recalculated
};