-
Notifications
You must be signed in to change notification settings - Fork 0
/
3d_math.c
567 lines (476 loc) · 10.7 KB
/
3d_math.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
* Matheus' Simple 3D Math
* Copyright 2023 Matheus Klein Schaefer
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* @file 3d_math.c
* @brief 3d_math.h implementation file
*
* @author
* - Matheus Klein Schaefer
*/
#include <math.h>
#include "3d_math.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
void math_vec2_zero(vec2 vec)
{
vec[0] = 0.0f;
vec[1] = 0.0f;
}
void math_vec2_min(vec2 result, vec2 vec_a, vec2 vec_b)
{
result[0] = fminf(vec_a[0], vec_b[0]);
result[1] = fminf(vec_a[1], vec_b[1]);
}
void math_vec2_max(vec2 result, vec2 vec_a, vec2 vec_b)
{
result[0] = fmaxf(vec_a[0], vec_b[0]);
result[1] = fmaxf(vec_a[1], vec_b[1]);
}
void math_vec2_copy(vec2 result, vec2 original)
{
result[0] = original[0];
result[1] = original[1];
}
void math_vec2_add(vec2 result, vec2 vec_a, vec2 vec_b)
{
result[0] = vec_a[0] + vec_b[0];
result[1] = vec_a[1] + vec_b[1];
}
void math_vec2_sub(vec2 result, vec2 vec_a, vec2 vec_b)
{
result[0] = vec_a[0] - vec_b[0];
result[1] = vec_a[1] - vec_b[1];
}
void math_vec2_scale(vec2 result, vec2 vec, float scalar)
{
result[0] = vec[0] * scalar;
result[1] = vec[1] * scalar;
}
float math_vec2_dot(vec2 vec_a, vec2 vec_b)
{
float dot = vec_a[0] * vec_b[0] + vec_a[1] * vec_b[1];
return dot;
}
float math_vec2_cross(vec2 vec_a, vec2 vec_b)
{
float cross = vec_a[0] * vec_b[1] - vec_a[1] * vec_b[0];
return cross;
}
float math_vec2_len(vec2 vec)
{
float result = sqrtf(math_vec2_dot(vec, vec));
return result;
}
void math_vec2_normalize(vec2 result, vec2 vec)
{
float norm = math_vec2_len(vec);
if(norm == 0.0f)
{
math_vec2_zero(result);
return;
}
math_vec2_scale(result, vec, 1.0f / norm);
}
/*void math_vec2_lerp(vec2 result, vec2 vec_a, vec2 vec_b, float amount)
{
glm_vec2_lerp(vec_a, vec_b, amount, result);
}*/
void math_vec3_zero(vec3 vec)
{
vec[0] = 0.0f;
vec[1] = 0.0f;
vec[2] = 0.0f;
}
void math_vec3_min(vec3 result, vec3 vec_a, vec3 vec_b)
{
result[0] = fminf(vec_a[0], vec_b[0]);
result[1] = fminf(vec_a[1], vec_b[1]);
result[2] = fminf(vec_a[2], vec_b[2]);
}
void math_vec3_max(vec3 result, vec3 vec_a, vec3 vec_b)
{
result[0] = fmaxf(vec_a[0], vec_b[0]);
result[1] = fmaxf(vec_a[1], vec_b[1]);
result[2] = fmaxf(vec_a[2], vec_b[2]);
}
void math_vec3_copy(vec3 result, vec3 original)
{
result[0] = original[0];
result[1] = original[1];
result[2] = original[2];
}
void math_vec3_negate(vec3 result, vec3 vec)
{
result[0] = -vec[0];
result[1] = -vec[1];
result[2] = -vec[2];
}
void math_vec3_add(vec3 result, vec3 vec_a, vec3 vec_b)
{
result[0] = vec_a[0] + vec_b[0];
result[1] = vec_a[1] + vec_b[1];
result[2] = vec_a[2] + vec_b[2];
}
void math_vec3_sub(vec3 result, vec3 vec_a, vec3 vec_b)
{
result[0] = vec_a[0] - vec_b[0];
result[1] = vec_a[1] - vec_b[1];
result[2] = vec_a[2] - vec_b[2];
}
void math_vec3_div(vec3 result, vec3 vec, float div)
{
result[0] = vec[0] / div;
result[1] = vec[1] / div;
result[2] = vec[2] / div;
}
void math_vec3_scale(vec3 result, vec3 vec, float scalar)
{
result[0] = vec[0] * scalar;
result[1] = vec[1] * scalar;
result[2] = vec[2] * scalar;
}
float math_vec3_dot(vec3 vec_a, vec3 vec_b)
{
float dot = vec_a[0] * vec_b[0] + vec_a[1] * vec_b[1] + vec_a[2] * vec_b[2];
return dot;
}
void math_vec3_cross(vec3 result, vec3 vec_a, vec3 vec_b)
{
result[0] = vec_a[1] * vec_b[2] - vec_a[2] * vec_b[1];
result[1] = vec_a[2] * vec_b[0] - vec_a[0] * vec_b[2];
result[2] = vec_a[0] * vec_b[1] - vec_a[1] * vec_b[0];
}
float math_vec3_len(vec3 vec)
{
float result = sqrtf(math_vec3_dot(vec, vec));
return result;
}
void math_vec3_normalize(vec3 vec)
{
float norm = math_vec3_len(vec);
if(norm == 0.0f)
{
math_vec3_zero(vec);
return;
}
math_vec3_scale(vec, vec, 1.0f / norm);
}
void math_vec3_normalize_to(vec3 result, vec3 vec)
{
float norm = math_vec3_len(vec);
if(norm == 0.0f)
{
math_vec3_zero(result);
return;
}
math_vec3_scale(result, vec, 1.0f / norm);
}
/*void math_vec3_lerp(vec3 result, vec3 vec_a, vec3 vec_b, float amount)
{
glm_vec3_lerp(vec_a, vec_b, amount, result);
}*/
void math_vec4_zero(vec4 vec)
{
vec[0] = 0.0f;
vec[1] = 0.0f;
vec[2] = 0.0f;
vec[3] = 0.0f;
}
void math_vec4_min(vec4 result, vec4 vec_a, vec4 vec_b)
{
result[0] = fminf(vec_a[0], vec_b[0]);
result[1] = fminf(vec_a[1], vec_b[1]);
result[2] = fminf(vec_a[2], vec_b[2]);
result[3] = fminf(vec_a[3], vec_b[3]);
}
void math_vec4_max(vec4 result, vec4 vec_a, vec4 vec_b)
{
result[0] = fmaxf(vec_a[0], vec_b[0]);
result[1] = fmaxf(vec_a[1], vec_b[1]);
result[2] = fmaxf(vec_a[2], vec_b[2]);
result[3] = fmaxf(vec_a[3], vec_b[3]);
}
void math_vec4_copy(vec4 result, vec4 original)
{
result[0] = original[0];
result[1] = original[1];
result[2] = original[2];
result[3] = original[3];
}
void math_vec4_add(vec4 result, vec4 vec_a, vec4 vec_b)
{
result[0] = vec_a[0] + vec_b[0];
result[1] = vec_a[1] + vec_b[1];
result[2] = vec_a[2] + vec_b[2];
result[3] = vec_a[3] + vec_b[3];
}
void math_vec4_sub(vec4 result, vec4 vec_a, vec4 vec_b)
{
result[0] = vec_a[0] - vec_b[0];
result[1] = vec_a[1] - vec_b[1];
result[2] = vec_a[2] - vec_b[2];
result[3] = vec_a[3] - vec_b[3];
}
void math_vec4_div(vec4 result, vec4 vec, float div)
{
result[0] = vec[0] / div;
result[1] = vec[1] / div;
result[2] = vec[2] / div;
result[3] = vec[3] / div;
}
void math_vec4_scale(vec4 result, vec4 vec, float scalar)
{
result[0] = vec[0] * scalar;
result[1] = vec[1] * scalar;
result[2] = vec[2] * scalar;
result[3] = vec[3] * scalar;
}
float math_vec4_dot(vec4 vec_a, vec4 vec_b)
{
float dot = vec_a[0] * vec_b[0] + vec_a[1] * vec_b[1] + vec_a[2] * vec_b[2] + vec_a[3] * vec_b[3];
return dot;
}
float math_vec4_len(vec4 vec)
{
float result = sqrtf(math_vec4_dot(vec, vec));
return result;
}
void math_vec4_normalize(vec4 vec)
{
float norm = math_vec4_len(vec);
if(norm == 0.0f)
{
math_vec4_zero(vec);
return;
}
math_vec4_scale(vec, vec, 1.0f / norm);
}
/*void math_vec4_lerp(vec4 result, vec4 vec_a, vec4 vec_b, float amount)
{
glm_vec4_lerp(vec_a, vec_b, amount, result);
}*/
void math_mat4_zero(mat4 mat)
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
mat[i][j] = 0.0f;
}
void math_mat4_identity(mat4 mat)
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
mat[i][j] = i == j ? 1.0f : 0.0f;
}
void math_mat4_add(mat4 result, mat4 mat_a, mat4 mat_b)
{
for(int i = 0; i < 4; ++i)
{
math_vec4_add(result[i], mat_a[i], mat_b[i]);
}
}
void math_mat4_sub(mat4 result, mat4 mat_a, mat4 mat_b)
{
for(int i = 0; i < 4; ++i)
{
math_vec4_sub(result[i], mat_a[i], mat_b[i]);
}
}
void math_mat4_scale(mat4 result, mat4 mat, float s)
{
for(int i = 0; i < 4; i++)
{
math_vec4_scale(result[i], mat[i], s);
}
}
void math_mat4_row(vec4 result, mat4 mat, uint8_t index)
{
for(uint8_t j = 0; j < 4; ++j)
{
result[j] = mat[j][index];
}
}
void math_mat4_col(vec4 result, mat4 mat, uint8_t index)
{
for(int j = 0; j < 4; ++j)
{
result[j] = mat[index][j];
}
}
void math_mat4_scale_xyz(mat4 mat, float scalar)
{
math_mat4_scale(mat, mat, scalar);
}
void math_mat4_mul(mat4 result, mat4 mat_a, mat4 mat_b)
{
int i, j, k;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
result[i][j] = 0.0f;
for(k = 0; k < 4; k++)
{
result[i][j] += mat_a[i][k] * mat_b[k][j];
}
}
}
}
void math_mat4_copy(mat4 result, mat4 original)
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
result[i][j] = original[i][j];
}
void math_mat4_translate(mat4 mat, vec3 vec)
{
mat[0][3] += vec[0];
mat[1][3] += vec[1];
mat[2][3] += vec[2];
}
void math_mat4_rotate_axis(mat4 mat, vec3 axis, float angle)
{
float c = cos(angle);
float s = sin(angle);
float t = 1.0f - c;
float x = axis[0];
float y = axis[1];
float z = axis[2];
mat[0][0] = t * x * x + c;
mat[0][1] = t * x * y - s * z;
mat[0][2] = t * x * z + s * y;
mat[0][3] = 0.0f;
mat[1][0] = t * x * y + s * z;
mat[1][1] = t * y * y + c;
mat[1][2] = t * y * z - s * x;
mat[1][3] = 0.0f;
mat[2][0] = t * x * z - s * y;
mat[2][1] = t * y * z + s * x;
mat[2][2] = t * z * z + c;
mat[2][3] = 0.0f;
mat[3][0] = 0.0f;
mat[3][1] = 0.0f;
mat[3][2] = 0.0f;
mat[3][3] = 1.0f;
}
void math_mat4_rotate_x(mat4 result, mat4 mat, float angle)
{
float s = sinf(angle);
float c = cosf(angle);
mat4 aux;
math_mat4_identity(result);
aux[1][1] = c;
aux[1][2] = s;
aux[2][1] = -s;
aux[2][2] = c;
math_mat4_mul(result, mat, aux);
}
void math_mat4_rotate_y(mat4 result, mat4 mat, float angle)
{
float s = sinf(angle);
float c = cosf(angle);
mat4 aux;
math_mat4_identity(result);
aux[0][0] = c;
aux[0][2] = -s;
aux[2][0] = s;
aux[2][2] = c;
math_mat4_mul(result, mat, aux);
}
void math_mat4_rotate_z(mat4 result, mat4 mat, float angle)
{
float s = sinf(angle);
float c = cosf(angle);
mat4 aux;
math_mat4_identity(result);
aux[0][0] = c;
aux[0][1] = s;
aux[1][0] = -s;
aux[1][1] = c;
math_mat4_mul(result, mat, aux);
}
void math_quat_zero(quat qt)
{
qt[0] = qt[1] = qt[2] = qt[3] = 0.0f;
}
void math_quat_identity(quat qt)
{
qt[0] = qt[1] = qt[2] = 0.0f;
qt[3] = 1.0f;
}
void math_quat_add(quat result, quat qt_a, quat qt_b)
{
math_vec4_add(result, qt_a, qt_b);
}
void math_quat_sub(quat result, quat qt_a, quat qt_b)
{
math_vec4_sub(result, qt_a, qt_b);
}
void math_quat_scale(quat result, quat qt, float scalar)
{
result[0] = qt[0] * scalar;
result[1] = qt[1] * scalar;
result[2] = qt[2] * scalar;
result[3] = qt[3] * scalar;
}
void math_quat_norm(quat result, quat qt)
{
float k = 1.0f / math_vec4_len(qt); //quat is literally vec4 here
math_quat_scale(result, qt, k);
}
void math_lookat(mat4 result, vec3 position, vec3 target, vec3 up)
{
vec3 f;
vec3 u;
vec3 s;
math_vec3_sub(f, target, position);
math_vec3_normalize(f);
math_vec3_cross(s, f, up);
math_vec3_normalize(s);
math_vec3_cross(u, s, f);
result[0][0] = s[0];
result[0][1] = u[0];
result[0][2] =-f[0];
result[1][0] = s[1];
result[1][1] = u[1];
result[1][2] =-f[1];
result[2][0] = s[2];
result[2][1] = u[2];
result[2][2] =-f[2];
result[3][0] = -math_vec3_dot(s, position);
result[3][1] = -math_vec3_dot(u, position);
result[3][2] = math_vec3_dot(f, position);
result[0][3] = result[1][3] = result[2][3] = 0.0f;
result[3][3] = 1.0f;
}
void math_perspective(mat4 result, float fov, float aspect, float near, float far)
{
float f;
float fn;
math_mat4_zero(result);
f = 1.0f / tanf(fov * 0.5f);
fn = 1.0f / (near - far);
result[0][0] = f / aspect;
result[1][1] = f;
result[2][2] = far * fn;
result[2][3] = -1.0f;
result[3][2] = near * far * fn;
}
float deg_to_rad(float deg)
{
return deg * (M_PI / 180.0f);
}
float rad_to_deg(float rad)
{
return rad * (180.0f / M_PI);
}
float math_lerp(float v0, float v1, float t)
{
return (1 - t) * v0 + t * v1;
}