forked from chaoticbob/tinyrenderers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.h
205 lines (176 loc) · 5.06 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef __cplusplus
#error "C++ is required"
#endif
#ifndef TR_TRANSFORM_H
#define TR_TRANSFORM_H
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
namespace tr {
using float2 = glm::vec2;
using float3 = glm::vec3;
using float4 = glm::vec4;
using float2x2 = glm::mat2x2;
using float2x3 = glm::mat2x3;
using float2x4 = glm::mat2x4;
using float3x2 = glm::mat3x2;
using float3x3 = glm::mat3x3;
using float3x4 = glm::mat3x4;
using float4x2 = glm::mat4x2;
using float4x3 = glm::mat4x3;
using float4x4 = glm::mat4x4;
/*! @class Transform
Angles are always in radians unless otherwise stated.
*/
class Transform {
public:
enum RotationOrder {
ROTATION_ORDER_ZYX, // Default
ROTATION_ORDER_ZXY,
ROTATION_ORDER_YZX,
ROTATION_ORDER_YXZ,
ROTATION_ORDER_XZY,
ROTATION_ORDER_XYZ,
};
Transform() {}
~Transform() {}
void Translate(float3 pos) {
m_translate = pos;
m_translate_dirty = true;
m_model_dirty = true;
}
void Rotate(float angle, float3 axis) {
m_rotate_axis_angle.w = angle;
m_rotate_axis_angle.x = axis.x;
m_rotate_axis_angle.y = axis.y;
m_rotate_axis_angle.z = axis.z;
m_rotation_mode = ROTATION_MODE_AXIS_ANGLE;
m_rotate_dirty = true;
m_model_dirty = true;
}
void RotateOrder(RotationOrder rotation_order) {
m_rotation_order = rotation_order;
m_rotate_dirty = true;
m_model_dirty = true;
}
void Rotate(float3 angles) {
m_rotate_euler = angles;
m_rotation_mode = ROTATION_MODE_EULER;
m_rotate_dirty = true;
m_model_dirty = true;
}
void Rotate(float x_angle, float y_angle, float z_angle) {
m_rotate_euler = float3(x_angle, y_angle, z_angle);
m_rotation_mode = ROTATION_MODE_EULER;
m_rotate_dirty = true;
m_model_dirty = true;
}
void RotateX(float angle) {
m_rotate_euler.x = angle;
m_rotation_mode = ROTATION_MODE_EULER;
m_rotate_dirty = true;
m_model_dirty = true;
}
void RotateY(float angle) {
m_rotate_euler.y = angle;
m_rotation_mode = ROTATION_MODE_EULER;
m_rotate_dirty = true;
m_model_dirty = true;
}
void RotateZ(float angle) {
m_rotate_euler.z = angle;
m_rotation_mode = ROTATION_MODE_EULER;
m_rotate_dirty = true;
m_model_dirty = true;
}
void Scale(float s) {
m_scale = float3(s);
m_scale_dirty = true;
m_model_dirty = true;
}
void Scale(float3 s) {
m_scale = s;
m_scale_dirty = true;
m_model_dirty = true;
}
void ScaleX(float sx) {
m_scale.x = sx;
m_scale_dirty = true;
m_model_dirty = true;
}
void ScaleY(float sy) {
m_scale.y = sy;
m_scale_dirty = true;
m_model_dirty = true;
}
void ScaleZ(float sz) {
m_scale.z = sz;
m_scale_dirty = true;
m_model_dirty = true;
}
const float4x4& GetTranslateMatrix() const {
if (m_translate_dirty) {
m_translate_matrix = glm::translate(m_translate);
m_translate_dirty = false;
}
return m_translate_matrix;
}
const float4x4& GetRotateMatrix() const {
if (m_rotate_dirty) {
if (m_rotation_mode == ROTATION_MODE_EULER) {
float4x4 rot_x = glm::rotate(m_rotate_euler.x, float3(1, 0, 0));
float4x4 rot_y = glm::rotate(m_rotate_euler.y, float3(0, 1, 0));
float4x4 rot_z = glm::rotate(m_rotate_euler.z, float3(0, 0, 1));
m_rotate_matrix = rot_z * rot_y * rot_x;
}
else {
float angle = m_rotate_axis_angle.w;
float3 axis = float3(m_rotate_axis_angle.x, m_rotate_axis_angle.y, m_rotate_axis_angle.z);
m_rotate_matrix = glm::rotate(angle, axis);
}
m_rotate_dirty = false;
}
return m_rotate_matrix;
}
const float4x4& GetScaleMatrix() const {
if (m_scale_dirty) {
m_scale_matrix = glm::scale(m_scale);
m_scale_dirty = false;
}
return m_scale_matrix;
}
const float4x4& GetModelMatrix() const {
if (m_model_dirty) {
const float4x4& translate = GetTranslateMatrix();
const float4x4& rotate = GetRotateMatrix();
const float4x4& scale = GetScaleMatrix();
m_model_matrix = translate * rotate * scale;
m_model_dirty = false;
}
return m_model_matrix;
}
private:
enum RotationMode {
ROTATION_MODE_EULER, // Default
ROTATION_MODE_AXIS_ANGLE,
};
RotationMode m_rotation_mode = ROTATION_MODE_EULER;
RotationOrder m_rotation_order = ROTATION_ORDER_ZYX;
float3 m_translate = float3(0, 0, 0);
float3 m_rotate_euler = float3(0, 0, 0);
float4 m_rotate_axis_angle = float4(0, 0, 1, 0);
float3 m_scale = float3(1, 1, 1);
mutable bool m_translate_dirty = true;
mutable bool m_rotate_dirty = true;
mutable bool m_scale_dirty = true;
mutable bool m_model_dirty = true;
mutable float4x4 m_translate_matrix;
mutable float4x4 m_rotate_matrix;
mutable float4x4 m_scale_matrix;
mutable float4x4 m_model_matrix;
};
} // namespace tr
#endif // TR_TRANSFORM_H