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
|
#pragma once
#include "Common.hpp"
namespace Math::MVP {
template<typename T = Real>
Matrix<4, 4, T> model(Vector<3> position, Vector<3> size, Rotation angles) {
auto transformation = Matrix<4, 4, T>::transformation(position);
auto scale = Matrix<4, 4, T>::scale(size);
auto rotation = Matrix<4, 4, T>::rotation(angles);
return transformation * rotation * scale;
}
template<typename T = Real>
Matrix<4, 4, T> view(Vector<3> position, Rotation angles) {
auto rotation = Matrix<4, 4, T>::rotation(angles);
auto transformation = Matrix<4, 4, T>::transformation(-position);
return rotation * transformation;
}
template<typename T = Real>
Matrix<4, 4, T> perspective_projection(Real aspect, Real fov, Real near, Real far) {
auto fov_radians = radians(fov);
Real x_scale = 1.0 / (tan(fov_radians / 2.0) * aspect);
Real y_scale = 1.0 / tan(fov_radians / 2.0);
Real frustum_length = far - near;
Real z_near = -(far + near) / frustum_length;
Real z_far = -(2 * far * near) / frustum_length;
Matrix<4, 4, T> projection{
x_scale, 0.0f, 0.0f, 0.0f,
0.0f, y_scale, 0.0f, 0.0f,
0.0f, 0.0f, z_near, z_far,
0.0f, 0.0f, -1.0f, 0.0f,
};
return projection;
}
template<typename T = Real>
Matrix<4, 4, T> orthographic_projection(Real width, Real height, Real near, Real far) {
Real x_scale = 2.0f / width;
Real y_scale = 2.0f / -height;
Real frustum_length = far - near;
Real z_near = -(far + near) / frustum_length;
Real z_far = -2 / frustum_length;
Matrix<4, 4, T> projection{
x_scale, 0.0f, 0.0f, -1.0f,
0.0f, y_scale, 0.0f, 1.0f,
0.0f, 0.0f, z_near, z_far,
0.0f, 0.0f, 0.0f, 1.0f,
};
return projection;
}
}
|