-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMat4.hpp
122 lines (92 loc) · 2.92 KB
/
Mat4.hpp
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
#pragma once
#include <cmath>
#include "config.hpp"
#include "Vec3.hpp"
struct Mat4 {
float elements[4][4];
Mat4(const float e[4][4]) {
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
elements[i][j] = e[i][j];
}
Mat4() { }
Mat4 multiply(Mat4 mat) const {
Mat4 res;
for(int i = 0; i < 4; ++i) {
for(int j = 0; j < 4; ++j) {
float sum = 0;
for(int k = 0; k < 4; ++k) {
sum += elements[i][k] * mat.elements[k][j];
}
res.elements[i][j] = sum;
}
}
return res;
}
Mat4 rotateAroundY(float angle) const {
float sinAngle = sin(degToRadians(angle));
float cosAngle = cos(degToRadians(angle));
float mat[4][4] = {
{ cosAngle, 0, -sinAngle, 0 },
{ 0, 1, 0, 0 },
{ sinAngle, 0, cosAngle, 0 },
{ 0, 0, 0, 1 }
};
return multiply(Mat4(mat));
}
Mat4 rotateAroundX(float angle) const {
float sinAngle = sin(degToRadians(angle));
float cosAngle = cos(degToRadians(angle));
float mat[4][4] = {
{ 1, 0, 0, 0 },
{ 0, cosAngle, -sinAngle, 0 },
{ 0, sinAngle, cosAngle, 0 },
{ 0, 0, 0, 1 }
};
return multiply(Mat4(mat));
}
Mat4 translate(const Vec3 v) const {
float mat[4][4] = {
{ 1, 0, 0, v.x },
{ 0, 1, 0, v.y },
{ 0, 0, 1, v.z },
{ 0, 0, 0, 1 }
};
return multiply(Mat4(mat));
}
Vec3 transformVec4(const float* vv) const {
float rot[4];
for(int i = 0; i < 4; ++i) {
rot[i] = 0;
for(int j = 0; j < 4; ++j) {
rot[i] += vv[j] * elements[i][j];
}
}
float w = rot[3];
if(w != 0)
return Vec3(rot[0] / w, rot[1] / w, rot[2] / w);
else
return Vec3(rot[0], rot[1], rot[2]);
}
Vec3 rotateVec3(const Vec3 v) const {
float vv[4] = { v.x, v.y, v.z, 1 };
return transformVec4(vv);
}
Vec3 rotateVec3Normal(const Vec3 v) const {
Mat4 mat = identity();
for(int i = 0; i < 3; ++i)
for(int j = 0; j < 3; ++j)
mat.elements[i][j] = elements[j][i];
float vv[4] = { v.x, v.y, v.z, 0 };
return transformVec4(vv);
}
static Mat4 identity() {
const float mat[4][4] = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
return Mat4(mat);
}
};