-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMtx4x4.h
183 lines (148 loc) · 4.51 KB
/
Mtx4x4.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
#pragma once
#include <cmath>
#include <cassert>
#include <array>
#include <numbers>
#include <algorithm>
#include "Vec4D.h"
template <typename T>
class Mtx4x4
{
private:
static constexpr size_t s_size = 4;
std::array<Vec4D<T>, s_size> m_mtx;
public:
constexpr Mtx4x4() : m_mtx{} {}
constexpr explicit Mtx4x4(T n) : m_mtx{}
{
m_mtx.fill(n);
}
~Mtx4x4() = default;
constexpr Mtx4x4(const Mtx4x4<T>& other)
{
m_mtx = other.m_mtx;
}
constexpr Vec4D<T>& operator[](int index)
{
assert(index >= 0 && index < s_size);
return m_mtx[index];
}
constexpr const Vec4D<T>& operator[](int index) const
{
assert(index >= 0 && index < s_size);
return m_mtx[index];
}
constexpr Mtx4x4<T>& operator=(const Mtx4x4<T>& other)
{
if(this != &other)
m_mtx = other.m_mtx;
return *this;
}
constexpr Mtx4x4<T>& operator=(const T& scalar)
{
m_mtx.fill(scalar);
return *this;
}
constexpr Mtx4x4<T> operator+(const Mtx4x4<T>& other) const
{
Mtx4x4<T> sum;
for(auto r = 0; r < s_size; ++r)
for(auto c = 0; c < s_size; ++c)
sum.m_mtx[r][c] = m_mtx[r][c] + other.m_mtx[r][c];
return sum;
}
constexpr Mtx4x4<T>& operator+=(const Mtx4x4<T>& other)
{
for(auto r = 0; r < s_size; ++r)
for(auto c = 0; c < s_size; ++c)
m_mtx[r][c] += other.m_mtx[r][c];
return *this;
}
constexpr Mtx4x4<T> operator-(const Mtx4x4<T>& other) const
{
Mtx4x4<T> diff;
for(auto r = 0; r < s_size; ++r)
for(auto c = 0; c < s_size; ++c)
diff.m_mtx[r][c] = m_mtx[r][c] - other.m_mtx[r][c];
return diff;
}
constexpr Mtx4x4<T>& operator-=(const Mtx4x4<T>& other)
{
for(auto r = 0; r < s_size; ++r)
for(auto c = 0; c < s_size; ++c)
m_mtx[r][c] += other.m_mtx[r][c];
return *this;
}
constexpr Mtx4x4<T> operator*(const T& scalar) const
{
Mtx4x4<T> prod;
for(auto r = 0; r < s_size; ++r)
for(auto c = 0; c < s_size; ++c)
prod.m_mtx[r][c] = m_mtx[r][c] * scalar;
return prod;
}
constexpr Mtx4x4<T>& operator*=(const T& scalar)
{
for(auto r = 0; r < s_size; ++r)
for(auto c = 0; c < s_size; ++c)
m_mtx[r][c] * scalar;
return *this;
}
constexpr Mtx4x4<T> operator*(const Mtx4x4<T>& other) const
{
Mtx4x4<T> prod;
for(auto k = 0; k < s_size; ++k)
for(auto i = 0; i < s_size; ++i)
for(auto j = 0; j < s_size; ++j)
prod.m_mtx[i][k] += m_mtx[i][j] * other.m_mtx[j][k];
return prod;
}
constexpr Mtx4x4<T>& operator*=(const Mtx4x4<T>& other) const
{
std::array<Vec4D<T>, s_size> prod;
for(auto k = 0; k < s_size; ++k)
for(auto i = 0; i < s_size; ++i)
for(auto j = 0; j < s_size; ++j)
prod[i][k] += m_mtx[i][j] * other.m_mtx[j][k];
m_mtx = prod;
return *this;
}
constexpr Vec4D<T>& operator*(const Vec4D<T>& vec) const
{
Vec4D<T> prod;
for(auto i = 0; i < s_size; ++i)
for(auto j = 0; j < s_size; ++j)
prod[i] += m_mtx[i][j] * vec[j];
return prod;
}
constexpr Vec4D<T> GetRow(int index) const
{
assert(index >= 0 && index < s_size);
return m_mtx[index];
}
constexpr Vec4D<T> GetCol(int index) const
{
assert(index >= 0 && index < s_size);
return {m_mtx[index].X, m_mtx[index].Y, m_mtx[index].Z, m_mtx[index].W};
}
constexpr static Mtx4x4<T> Identity()
{
Mtx4x4<T> mtx;
for(auto i = 0; i < s_size; ++i)
mtx[i][i] = static_cast<T>(1.f);
return mtx;
}
constexpr static Mtx4x4<T> PerspectiveProjection(T fov, T aspect, T near, T far, Math::Angle unit = Math::Angle::Deg)
{
Mtx4x4<T> mtx;
T fovFactor = unit == Math::Angle::Deg
? static_cast<T>(1.f / tanf(static_cast<float>(fov) * .5f))
: static_cast<T>(1.f / tanf(static_cast<float>(fov) * .5f / 180.f * std::numbers::pi_v<float>));
mtx[0][0] = aspect * fovFactor;
mtx[1][1] = fovFactor;
mtx[2][2] = far / (far - near);
mtx[2][3] = (-far * near) / (far - near);
mtx[3][2] = static_cast<T>(1.f);
return mtx;
}
};