-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cpp
More file actions
308 lines (252 loc) · 5.9 KB
/
math.cpp
File metadata and controls
308 lines (252 loc) · 5.9 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "math.h"
namespace Math
{
///////////
inline Vector4 operator *(const Vector4& v1, const Vector4& v2)
{
return (Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.n * v2.n));
}
inline Vector4 operator +(const Vector4& v1, const Vector4& v2)
{
return (Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.n + v2.n));
}
inline Vector4 operator -(const Vector4& v1, const Vector4& v2)
{
return (Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.n - v2.n));
}
inline Vector4 operator /(const Vector4& v1, const Vector4& v2)
{
return (Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.n / v2.n));
}
///////////
inline Vector3 operator *(const Vector3& v1, const Vector3& v2)
{
return (Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z));
}
inline Vector3 operator +(const Vector3& v1, const Vector3& v2)
{
return (Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z));
}
inline Vector3 operator -(const Vector3& v1, const Vector3& v2)
{
return (Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z));
}
inline Vector3 operator /(const Vector3& v1, const Vector3& v2)
{
return (Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z));
}
///////////
inline Vector2 operator *(const Vector2& v1, const Vector2& v2)
{
return (Vector2(v1.x * v2.x, v1.y * v2.y));
}
inline Vector2 operator +(const Vector2& v1, const Vector2& v2)
{
return (Vector2(v1.x + v2.x, v1.y + v2.y));
}
inline Vector2 operator -(const Vector2& v1, const Vector2& v2)
{
return (Vector2(v1.x - v2.x, v1.y - v2.y));
}
inline Vector2 operator /(const Vector2& v1, const Vector2& v2)
{
return (Vector2(v1.x / v2.x, v1.y / v2.y));
}
///////////
Vector3 Normalize(const Vector3 a)
{
return (Vector3(a.x / Magnitude(a), a.y / Magnitude(a), a.z / Magnitude(a)));;
}
Vector3 Crossproduct(const Vector3 a, const Vector3 b)
{
return (Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x));
}
///////////
Vector3 V4toV3(const Vector4& v1) // Convert a v3 to v4
{
return (Vector3(v1.x, v1.y, v1.z));
}
///////////
Vector4 MultByScalar(Vector4 v, float s) // Multiply a vectorX by a scalar
{
return (Vector4(v.x * s, v.y * s, v.z * s, v.n * s));
}
Vector3 MultByScalar(Vector3 v, float s)
{
return (Vector3(v.x * s, v.y * s, v.z * s));
}
Vector2 MultByScalar(Vector2 v, float s)
{
return (Vector2(v.x * s, v.y * s));
}
/*
Random
*/
float Magnitude(Vector3 a)
{
return sqrt(pow(a.x, 2) + pow(a.y, 2) + pow(a.z, 2));
}
float Dotproduct(const Vector3 a, const Vector3 b)
{
return (a.x * b.x + a.y * b.y + a.z * b.z);
}
float ToRadians(float deg)
{
return deg * PI / 180.0f;
}
float Cotangens(float deg)
{
return 1 / tan(deg);
}
/*
Matrix4
*/
Matrix4 Transpose(Matrix4 m)
{
return(Matrix4
(
m.m[0][0], m.m[0][1], m.m[0][2], m.m[0][3],
m.m[1][0], m.m[1][1], m.m[1][2], m.m[1][3],
m.m[2][0], m.m[2][1], m.m[2][2], m.m[2][3],
m.m[3][0], m.m[3][1], m.m[3][2], m.m[3][3]
));
}
Matrix4 operator *(Matrix4 a, Matrix4 b)
{
Matrix4 result;
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 += a.m[k][j] * b.m[i][k];
}
result.m[i][j] = sum;
}
}
return result;
}
/////////// MAT4 Rotations
Matrix4 RotateX(float t)
{
return(Matrix4
(
1, 0, 0, 0,
0, cos(t), sin(t), 0,
0, -sin(t), cos(t), 0,
0, 0, 0, 1
));
}
Matrix4 RotateY(float t)
{
return(Matrix4
(
cos(t), 0, -sin(t), 0,
0, 1, 0, 0,
sin(t), 0, cos(t), 0,
0, 0, 0, 1
));
}
Matrix4 RotateZ(float t)
{
return(Matrix4
(
cos(t), sin(t), 0, 0,
-sin(t), cos(t), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
));
}
/////////// Translations & scale
Matrix4 Translate(Vector3 t)
{
return(Matrix4
(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
t.x, t.y, t.z, 1
));
}
Matrix4 Scale(Vector3 s)
{
return(Matrix4
(
s.x, 0, 0, 0,
0, s.y, 0, 0,
0, 0, s.z, 0,
0, 0, 0, 1
));
}
///////////
Matrix4 Perspective(float fov, float aspect_ratio, float near, float far)
{
float fovRad = ToRadians(fov);
float f = Cotangens(fovRad / 2.0f);
float ar = aspect_ratio;
float nd = near, fd = far;
return Matrix4
(
f / ar, 0, 0, 0,
0, f, 0, 0,
0, 0, (fd + nd) / (nd - fd), (2 * fd * nd) / (nd - fd),
0, 0, -1, 0
);
}
///////////
Matrix4 To3x3(Matrix4 i) //Convert a mat4 to a mat3
{
return Matrix4
(
i.m[0][0], i.m[1][0], i.m[2][0], 0,
i.m[0][1], i.m[1][1], i.m[2][1], 0,
i.m[0][2], i.m[1][2], i.m[2][2], 0,
0, 0, 0, 1
);
}
///////////
Matrix4 lookAt(Vector3 from, Vector3 to, Vector3 up)
{
Vector3 z = Normalize((to - from) *= -1);
Vector3 x = Normalize(Crossproduct(up, z));
Vector3 y = Crossproduct(z, x);
return Matrix4
(
x.x, x.y, x.z, -Dotproduct(from, x),
y.x, y.y, y.z, -Dotproduct(from, y),
z.x, z.y, z.z, -Dotproduct(from, z),
0, 0, 0, 1
);
}
///////////
Matrix4 Inverse(const Matrix4& i)
{
Vector3 a = reinterpret_cast<const Vector3&>(i.m[0][0]);
Vector3 b = reinterpret_cast<const Vector3&>(i.m[1][0]);
Vector3 c = reinterpret_cast<const Vector3&>(i.m[2][0]);
Vector3 d = reinterpret_cast<const Vector3&>(i.m[3][0]);
const float& x = i.m[3][0];
const float& y = i.m[3][1];
const float& z = i.m[3][2];
const float& n = i.m[3][3];
Vector3 s = Crossproduct(a, b);
Vector3 t = Crossproduct(c, d);
Vector3 u = (a *= y) - (b *= x);
Vector3 v = (c *= n) - (d *= z);
float invDeterminant = 1.0f / Dotproduct(s, v) + Dotproduct(t, u);
s *= invDeterminant;
t *= invDeterminant;
u *= invDeterminant;
v *= invDeterminant;
Vector3 r0 = Crossproduct(b, v) + t *= y;
Vector3 r1 = Crossproduct(v, a) + t *= x;
Vector3 r2 = Crossproduct(d, u) + s *= n;
Vector3 r3 = Crossproduct(u, c) + s *= z;
return (Matrix4
(
r0.x, r0.y, r0.z, -Dotproduct(b, t),
r1.x, r1.y, r1.z, Dotproduct(a, t),
r2.x, r2.y, r2.z, -Dotproduct(d, s),
r3.x, r3.y, r3.z, Dotproduct(c, s)
));
}
}