-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat4.cpp
230 lines (217 loc) · 6.66 KB
/
mat4.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "mat4.h"
#include <iostream>
bool operator==(const mat4& a, const mat4& b) //comparing matrices
{
for (int i = 0; i < 16; ++i) {
if (fabsf(a.v[i] - b.v[i]) > MAT4_EPSILON) {
return false;
}
}
return true;
}
bool operator!=(const mat4& a, const mat4& b) //comparing matrices
{
return !(a == b);
}
mat4 operator+(const mat4& a, const mat4& b) //adding matrices sum their respective components and store the result in a new matrix.
{
return mat4(
a.xx + b.xx, a.xy + b.xy, a.xz + b.xz, a.xw + b.xw,
a.yx + b.yx, a.yy + b.yy, a.yz + b.yz, a.yw + b.yw,
a.zx + b.zx, a.zy + b.zy, a.zz + b.zz, a.zw + b.zw,
a.tx + b.tx, a.ty + b.ty, a.tz + b.tz, a.tw + b.tw
);
}
mat4 operator*(const mat4& m, float f) // matrices multiplication using a float number to scale respective components
{ // Two matrices can only be multiplied together if their inner dimensions are the same.
return mat4(
m.xx * f, m.xy * f, m.xz * f, m.xw * f,
m.yx * f, m.yy * f, m.yz * f, m.yw * f,
m.zx * f, m.zy * f, m.zz * f, m.zw * f,
m.tx * f, m.ty * f, m.tz * f, m.tw * f
);
}
// Helper macro: This macro will assume that there are two matrices, a and b. The macro will
//take two numbers, the row of a and the column of b, to dot together and the result will bet the dot product of the two.
#define M4D(aRow, bCol) ( \
a.v[0 * 4 + aRow] * b.v[bCol * 4 + 0] + \
a.v[1 * 4 + aRow] * b.v[bCol * 4 + 1] + \
a.v[2 * 4 + aRow] * b.v[bCol * 4 + 2] + \
a.v[3 * 4 + aRow] * b.v[bCol * 4 + 3] \
)
mat4 operator*(const mat4& a, const mat4& b) {
return mat4(
M4D(0, 0), M4D(1, 0), M4D(2, 0), M4D(3, 0),//Col 0
M4D(0, 1), M4D(1, 1), M4D(2, 1), M4D(3, 1),//Col 1
M4D(0, 2), M4D(1, 2), M4D(2, 2), M4D(3, 2),//Col 2
M4D(0, 3), M4D(1, 3), M4D(2, 3), M4D(3, 3) //Col 3
);
}
//This macro will take the row of a matrix and perform a
//dot product of that row against the provided column vector. M[4],[4]
#define M4V4D(mRow, x, y, z, w) \
x * m.v[0 * 4 + mRow] + \
y * m.v[1 * 4 + mRow] + \
z * m.v[2 * 4 + mRow] + \
w * m.v[3 * 4 + mRow]
vec4 operator*(const mat4& m, const vec4& v) {
return vec4(
M4V4D(0, v.x, v.y, v.z, v.w),
M4V4D(1, v.x, v.y, v.z, v.w),
M4V4D(2, v.x, v.y, v.z, v.w),
M4V4D(3, v.x, v.y, v.z, v.w)
);
}
//vec3 transform using refrenced matrix
vec3 transformVector(const mat4& m, const vec3& v) {
return vec3(
M4V4D(0, v.x, v.y, v.z, 0.0f),
M4V4D(1, v.x, v.y, v.z, 0.0f),
M4V4D(2, v.x, v.y, v.z, 0.0f)
);
}
//vec3 transform
vec3 transformPoint(const mat4& m, const vec3& v) {
return vec3(
M4V4D(0, v.x, v.y, v.z, 1.0f),
M4V4D(1, v.x, v.y, v.z, 1.0f),
M4V4D(2, v.x, v.y, v.z, 1.0f)
//W component = 1
);
}
vec3 transformPoint(const mat4& m, const vec3& v, float&
w) {
float _w = w;
w = M4V4D(3, v.x, v.y, v.z, _w);
return vec3(
M4V4D(0, v.x, v.y, v.z, _w),
M4V4D(1, v.x, v.y, v.z, _w),
M4V4D(2, v.x, v.y, v.z, _w)
);
}
//flip element diagonal
#define M4SWAP(x, y) \
{float t = x; x = y; y = t; }
void transpose(mat4& m) {
M4SWAP(m.yx, m.xy);
M4SWAP(m.zx, m.xz);
M4SWAP(m.tx, m.xw);
M4SWAP(m.zy, m.yz);
M4SWAP(m.ty, m.yw);
M4SWAP(m.tz, m.zw);
}
mat4 transposed(const mat4& m) {
return mat4(
m.xx, m.yx, m.zx, m.tx,
m.xy, m.yy, m.zy, m.ty,
m.xz, m.yz, m.zz, m.tz,
m.xw, m.yw, m.zw, m.tw
);
}
#define M4_3X3MINOR(c0, c1, c2, r0, r1, r2) \
(m.v[c0 * 4 + r0] * (m.v[c1 * 4 + r1] * m.v[c2 * 4 + r2] - m.v[c1 * 4 + r2] * m.v[c2 * 4 + r1]) - \
m.v[c1 * 4 + r0] * (m.v[c0 * 4 + r1] * m.v[c2 * 4 + r2] - m.v[c0 * 4 + r2] * m.v[c2 * 4 + r1]) + \
m.v[c2 * 4 + r0] * (m.v[c0 * 4 + r1] * m.v[c1 * 4 + r2] - m.v[c0 * 4 + r2] * m.v[c1 * 4 + r1]))
//multiply each element by the co factor 40/4/8/12
float determinant(const mat4& m) {
return m.v[0] * M4_3X3MINOR(1, 2, 3, 1, 2, 3)
- m.v[4] * M4_3X3MINOR(0, 2, 3, 1, 2, 3)
+ m.v[8] * M4_3X3MINOR(0, 1, 3, 1, 2, 3)
- m.v[12] * M4_3X3MINOR(0, 1, 2, 1, 2, 3);
}
mat4 adjugate(const mat4& m) {
// Cofactor(M[i, j]) = Minor(M[i, j]] * pow(-1, i + j)
mat4 cofactor;
cofactor.v[0] = M4_3X3MINOR(1, 2, 3, 1, 2, 3);
cofactor.v[1] = -M4_3X3MINOR(1, 2, 3, 0, 2, 3);
cofactor.v[2] = M4_3X3MINOR(1, 2, 3, 0, 1, 3);
cofactor.v[3] = -M4_3X3MINOR(1, 2, 3, 0, 1, 2);
cofactor.v[4] = -M4_3X3MINOR(0, 2, 3, 1, 2, 3);
cofactor.v[5] = M4_3X3MINOR(0, 2, 3, 0, 2, 3);
cofactor.v[6] = -M4_3X3MINOR(0, 2, 3, 0, 1, 3);
cofactor.v[7] = M4_3X3MINOR(0, 2, 3, 0, 1, 2);
cofactor.v[8] = M4_3X3MINOR(0, 1, 3, 1, 2, 3);
cofactor.v[9] = -M4_3X3MINOR(0, 1, 3, 0, 2, 3);
cofactor.v[10] = M4_3X3MINOR(0, 1, 3, 0, 1, 3);
cofactor.v[11] = -M4_3X3MINOR(0, 1, 3, 0, 1, 2);
cofactor.v[12] = -M4_3X3MINOR(0, 1, 2, 1, 2, 3);
cofactor.v[13] = M4_3X3MINOR(0, 1, 2, 0, 2, 3);
cofactor.v[14] = -M4_3X3MINOR(0, 1, 2, 0, 1, 3);
cofactor.v[15] = M4_3X3MINOR(0, 1, 2, 0, 1, 2);
return transposed(cofactor);
}
mat4 inverse(const mat4& m) {
float det = determinant(m);
if (det == 0.0f) {
std::cout << " Matrix determinant is 0\n";
return mat4();
}
mat4 adj = adjugate(m);
return adj * (1.0f / det);
}
void invert(mat4& m) {
float det = determinant(m);
if (det == 0.0f) {
std::cout << "Matrix determinant is 0\n";
m = mat4();
return;
}
m = adjugate(m) * (1.0f / det);
}
//create camera frustrum. (Camera Space)
//Left, Right, Bottom, Top, Near, Far.
mat4 frustum(float l, float r, float b,
float t, float n, float f) {
if (l == r || t == b || n == f) {
std::cout << "Invalid frustum\n";
return mat4(); // Error
}
return mat4(
(2.0f * n) / (r - l), 0, 0, 0,
0, (2.0f * n) / (t - b), 0, 0,
(r + l) / (r - l), (t + b) / (t - b), (-(f + n)) / (f - n), -1,
0, 0, (-2 * f * n) / (f - n), 0
);
}
//fov in degrees, aspect, Near, Far
mat4 perspective(float fov, float aspect, float n, float f) {
float ymax = n * tanf(fov * 3.14159265359f / 360.0f);
float xmax = ymax * aspect;
return frustum(-xmax, xmax, -ymax, ymax, n, f);
}
//Left, Right, Bottom, Top, Near, Far.
mat4 ortho(float l, float r, float b, float t,
float n, float f) {
if (l == r || t == b || n == f) {
return mat4(); // Error
}
return mat4(
2.0f / (r - l), 0, 0, 0,
0, 2.0f / (t - b), 0, 0,
0, 0, -2.0f / (f - n), 0,
-((r + l) / (r - l)), -((t + b) / (t - b)), -((f + n) / (f - n)), 1
);
}
//generate inverted matrix with pos, rot & scale for camera transform
mat4 lookAt(const vec3& position, const vec3& target,
const vec3& up) {
vec3 f = normalized(target - position) * -1.0f;
vec3 r = cross(up, f); // Right handed
if (r == vec3(0, 0, 0)) {
return mat4(); // Error
}
normalize(r);
vec3 u = normalized(cross(f, r)); // Right handed
vec3 t = vec3(
-dot(r, position),
-dot(u, position),
-dot(f, position)
);
return mat4(
// Transpose upper 3x3 matrix to invert it
r.x, u.x, f.x, 0,
r.y, u.y, f.y, 0,
r.z, u.z, f.z, 0,
t.x, t.y, t.z, 1
);
}