-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.cpp
More file actions
59 lines (48 loc) · 1.25 KB
/
transform.cpp
File metadata and controls
59 lines (48 loc) · 1.25 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
#include "transform.h"
/* general rotation matrix around an axis */
glm::mat3 Transform::rotate(const float degrees, const glm::vec3& axis)
{
float angle = glm::radians(degrees);
float cos_theta = glm::cos(angle);
float sin_theta = glm::sin(angle);
glm::vec3 norm_axis = glm::normalize(axis);
glm::mat3 along = glm::mat3(
norm_axis.x * norm_axis.x, norm_axis.x * norm_axis.y, norm_axis.x * norm_axis.z,
norm_axis.x * norm_axis.y, norm_axis.y * norm_axis.y, norm_axis.y * norm_axis.z,
norm_axis.x * norm_axis.z, norm_axis.y * norm_axis.z, norm_axis.z * norm_axis.z
);
glm::mat3 perpendicular = glm::mat3(
0.0, norm_axis.z, -norm_axis.y,
-norm_axis.z, 0.0, norm_axis.x,
norm_axis.y, -norm_axis.x, 0.0
);
glm::mat3 rot =
(cos_theta * glm::mat3(1.0)) +
((1.0f - cos_theta) * along) +
(sin_theta * perpendicular);
return rot;
}
glm::mat4 Transform::scale(const float &sx, const float &sy, const float &sz)
{
return glm::mat4(
sx, 0, 0, 0,
0, sy, 0, 0,
0, 0, sz, 0,
0, 0, 0, 1
);
}
glm::mat4 Transform::translate(const float &tx, const float &ty, const float &tz)
{
return glm::mat4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
tx, ty, tz, 1
);
}
Transform::Transform()
{
}
Transform::~Transform()
{
}