-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMat4.h
185 lines (167 loc) · 3.75 KB
/
Mat4.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
#pragma once
#define PI 3.1428
#include "Vec3.h"
class Mat4f
{
public:
float elements[4][4];
Mat4f& operator=(const Mat4f& rhs)
{
memcpy(elements, rhs.elements, sizeof(elements));
return *this;
}
/*Mat4& operator*=(T rhs)
{
for (auto& row : elements)
{
for (T& e : row)
{
e *= rhs;
}
}
return *this;
}*/
Mat4f operator*(float rhs) const
{
Mat4f result = *this;
return result = result * rhs;
}
Mat4f operator*(const Mat4f& rhs) const
{
Mat4f result;
for (size_t i = 0; i < 4; i++)
{
for (size_t j = 0; j < 4; j++)
{
float sum = 0.0;
for (size_t k = 0; k < 4; k++)
{
sum += elements[i][k] * rhs.elements[k][j];
}
result.elements[i][j] = sum;
}
}
return result;
}
constexpr static Mat4f Identity()
{
return {
1.0,0.0,0.0,0.0,
0.0,1.0,0.0,0.0,
0.0,0.0,1.0,0.0,
0.0,0.0,0.0,1.0,
};
}
constexpr static Mat4f Scaling(float factor)
{
return {
factor,0.0,0.0,0.0,
0.0,factor,0.0,0.0,
0.0,0.0,factor,0.0,
0.0,0.0,0.0,1.0,
};
}
static Mat4f RotationZ(float theta)
{
const float sinTheta = sin(theta);
const float cosTheta = cos(theta);
return {
cosTheta, -sinTheta,0.0,0.0,
sinTheta, cosTheta,0.0,0.0,
0.0, 0.0, 1.0,0.0,
0.0, 0.0, 0.0,1.0,
};
}
static Mat4f RotationY(float theta)
{
const float sinTheta = sin(theta);
const float cosTheta = cos(theta);
return {
cosTheta, 0.0,sinTheta,0.0,
0.0, 1.0,0.0,0.0,
-sinTheta, 0.0, cosTheta,0.0,
0.0, 0.0, 0.0,1.0,
};
}
static Mat4f RotationX(float theta)
{
const float sinTheta = sin(theta);
const float cosTheta = cos(theta);
return {
1.0, 0.0, 0.0, 0.0,
0.0, cosTheta, -sinTheta,0.0,
0.0, sinTheta, cosTheta,0.0,
0.0, 0.0, 0.0, 1.0,
};
}
/*template<class V>
constexpr static Mat4 Translation(const V& tl)
{
return Translation(tl.x, tl.y, tl.z);
}*/
constexpr static Mat4f Translation(float tx, float ty, float tz)
{
return {
1.0,0.0,0.0,tx,
0.0,1.0,0.0,ty,
0.0,0.0,1.0,tz,
0.0,0.0,0.0,1.0,
};
}
static Mat4f RotView(Vec3f right, Vec3f up, Vec3f dir)
{
return {
right.x,right.y,right.z,0.0f,
up.x,up.y,up.z,0.0f,
dir.x,dir.y,dir.z,0.0f,
0.0f,0.0f,0.0f,1.0f,
};
}
static Mat4f TranslateView(Vec3f pos)
{
return {
1.0f,0.0f,0.0f,-pos.x,
0.0f,1.0f,0.0f,-pos.y,
0.0f,0.0f,1.0f,-pos.z,
0.0f,0.0f,0.0f,1.0f,
};
}
static Mat4f PerspectiveFOV(float fov, float ar, float fNear, float fFar)
{
float fovRad = 1.0f / tanf(fov * 0.5f / 180.0f * 3.14159f);
return {
ar * fovRad, 0.0, 0.0, 0.0,
0.0, fovRad, 0.0, 0.0,
0.0, 0.0, fFar / (fFar - fNear), (-fFar * fNear) / (fFar - fNear),
0.0, 0.0, -1.0, 0.0,
};
}
Vec3f operator*(const Vec3f& lhs)
{
return{
lhs.x * elements[0][0] + lhs.y * elements[0][1] + lhs.z * elements[0][2] + lhs.w * elements[0][3],
lhs.x * elements[1][0] + lhs.y * elements[1][1] + lhs.z * elements[1][2] + lhs.w * elements[1][3],
lhs.x * elements[2][0] + lhs.y * elements[2][1] + lhs.z * elements[2][2] + lhs.w * elements[2][3],
lhs.x * elements[3][0] + lhs.y * elements[3][1] + lhs.z * elements[3][2] + lhs.w * elements[3][3],
};
}
};
//template<typename T>
//_Vec3<T>& operator*=(_Vec3<T>& lhs, const Mat4<T, 3>& rhs)
//{
// return lhs = lhs * rhs;
//}
//template<typename T>
//Vec3<T> operator*(const _Vec3<T>& lhs, const Mat4<T, 3>& rhs)
//{
// return{
// lhs.x * rhs.elements[0][0] + lhs.y * rhs.elements[1][0] + lhs.z * rhs.elements[2][0],
// lhs.x * rhs.elements[0][1] + lhs.y * rhs.elements[1][1] + lhs.z * rhs.elements[2][1],
// lhs.x * rhs.elements[0][2] + lhs.y * rhs.elements[1][2] + lhs.z * rhs.elements[2][2]
// };
//}
//template<typename T>
//_Vec4<T>& operator*=(_Vec4<T>& lhs, const Mat4<T, 4>& rhs)
//{
// return lhs = lhs * rhs;
//}