-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvec_math.h
More file actions
640 lines (534 loc) · 12.3 KB
/
vec_math.h
File metadata and controls
640 lines (534 loc) · 12.3 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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#ifndef VEC_MATH_H
#define VEC_MATH_H
#include <math.h>
#include <string.h>
#include <stdio.h>
#define TRUE 1
#define FALSE 0
/* Common 3D Math */
#define DEG_TO_RAD(X) ((X * M_PI) / 180.0f)
#define RAD_TO_DEG(X) ((X * 180.0f) / M_PI)
#define CLAMP(VAL, MIN, MAX) (VAL < MIN ? MIN: (VAL > MAX ? MAX : VAL))
static inline float Lerp (float t, float min, float max)
{
return t * max + (1.0f - t) * min;
}
static inline float InvLerp (float val, float min, float max)
{
return (val - min) / max;
}
/* Vectors make use of a union to allow elements to be accessed by name or index */
typedef struct Vec2
{
union
{
struct
{
float x;
float y;
};
float data[2];
};
} Vec2_t;
static inline Vec2_t Vec2_Zero()
{
Vec2_t vec;
vec.x = 0.0f;
vec.y = 0.0f;
return vec;
}
static inline Vec2_t Vec2_Create(float x, float y)
{
Vec2_t vec;
vec.x = x;
vec.y = y;
return vec;
}
static inline Vec2_t Vec2_Clear(float val)
{
Vec2_t vec;
vec.x = val;
vec.y = val;
return vec;
}
static inline Vec2_t Vec2_Add(Vec2_t a, Vec2_t b)
{
Vec2_t vec;
vec.x = a.x + b.x;
vec.y = a.y + b.y;
return vec;
}
static inline Vec2_t Vec2_Sub(Vec2_t a, Vec2_t b)
{
Vec2_t vec;
vec.x = a.x - b.x;
vec.y = a.y - b.y;
return vec;
}
static inline Vec2_t Vec2_Scale(Vec2_t a, float scale)
{
Vec2_t vec;
vec.x = a.x * scale;
vec.y = a.y * scale;
return vec;
}
static inline Vec2_t Vec2_Negate(Vec2_t a)
{
Vec2_t vec;
vec.x = -a.x;
vec.y = -a.y;
return vec;
}
static inline float Vec2_ToAngle(Vec2_t a)
{
return atan2f(a.y, a.x);
}
static inline float Vec2_Length(Vec2_t vec)
{
return sqrtf(vec.x * vec.x + vec.y * vec.y);
}
static inline float Vec2_LengthSq(Vec2_t vec)
{
return (vec.x * vec.x + vec.y * vec.y);
}
static inline float Vec2_Dist(Vec2_t a, Vec2_t b)
{
return Vec2_Length(Vec2_Sub(a, b));
}
static inline float Vec2_ManhatDist(Vec2_t a, Vec2_t b)
{
return fabsf(a.x - b.x) + fabsf(a.y - b.y);
}
static inline Vec2_t Vec2_Normalize(Vec2_t a)
{
float s = Vec2_Length(a);
Vec2_t n;
n.x = a.x / s;
n.y = a.y / s;
return n;
}
static inline float Vec2_Dot(Vec2_t a, Vec2_t b)
{
return a.x * b.x + a.y * b.y;
}
static inline Vec2_t Vec2_Cross(Vec2_t a)
{
return Vec2_Create(a.y, -a.x);
}
static inline Vec2_t Vec2_Lerp(Vec2_t a, Vec2_t b, float t)
{
Vec2_t v;
v.x = Lerp(t, a.x, b.x);
v.y = Lerp(t, a.y, b.y);
return v;
}
typedef struct Vec3
{
union
{
struct
{
float x;
float y;
float z;
};
struct
{
float r;
float g;
float b;
};
float data[3];
};
} Vec3_t;
static inline void Vec3_Print(Vec3_t v)
{
printf("%f, %f, %f\n", v.x, v.y, v.z);
}
static inline Vec3_t Vec3_Zero()
{
Vec3_t vec;
vec.x = 0.0f;
vec.y = 0.0f;
vec.z = 0.0f;
return vec;
}
static inline Vec3_t Vec3_Create(float x, float y, float z)
{
Vec3_t vec;
vec.x = x;
vec.y = y;
vec.z = z;
return vec;
}
static inline Vec3_t Vec3_FromVec2(Vec2_t vec)
{
return Vec3_Create(vec.x, vec.y, 0.0f);
}
static inline Vec3_t Vec3_Clear(float val)
{
Vec3_t vec;
vec.x = val;
vec.y = val;
vec.z = val;
return vec;
}
static inline Vec3_t Vec3_Add(Vec3_t a, Vec3_t b)
{
Vec3_t vec;
vec.x = a.x + b.x;
vec.y = a.y + b.y;
vec.z = a.z + b.z;
return vec;
}
static inline Vec3_t Vec3_Sub(Vec3_t a, Vec3_t b)
{
Vec3_t vec;
vec.x = a.x - b.x;
vec.y = a.y - b.y;
vec.z = a.z - b.z;
return vec;
}
static inline Vec3_t Vec3_Mult(Vec3_t a, Vec3_t b)
{
Vec3_t vec;
vec.x = a.x * b.x;
vec.y = a.y * b.y;
vec.z = a.z * b.z;
return vec;
}
static inline Vec3_t Vec3_Scale(Vec3_t a, float scale)
{
Vec3_t vec;
vec.x = a.x * scale;
vec.y = a.y * scale;
vec.z = a.z * scale;
return vec;
}
static inline Vec3_t Vec3_Mid(Vec3_t a, Vec3_t b)
{
Vec3_t vec;
vec.x = (a.x + b.x) / 2.0f;
vec.y = (a.y + b.y) / 2.0f;
vec.z = (a.z + b.z) / 2.0f;
return vec;
}
static inline float Vec3_Length(Vec3_t a)
{
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
}
static inline float Vec3_LengthSq(Vec3_t a)
{
return (a.x * a.x + a.y * a.y + a.z * a.z);
}
static inline float Vec3_Dist(Vec3_t a, Vec3_t b)
{
return Vec3_Length(Vec3_Sub(a, b));
}
static inline float Vec3_DistSq(Vec3_t a, Vec3_t b)
{
return Vec3_LengthSq(Vec3_Sub(a, b));
}
static inline float Vec3_ManhatLength(Vec3_t a)
{
return fabsf(a.x) + fabsf(a.y) + fabsf(a.z);
}
static inline float Vec3_ManhatDist(Vec3_t a, Vec3_t b)
{
return fabsf(a.x - b.x) + fabsf(a.y - b.y) + fabsf(a.z - b.z);
}
static inline Vec3_t Vec3_Normalize(Vec3_t a)
{
float s = Vec3_Length(a);
Vec3_t n;
n.x = a.x / s;
n.y = a.y / s;
n.z = a.z / s;
return n;
}
static inline Vec3_t Vec3_Negate(Vec3_t a)
{
Vec3_t vec;
vec.x = -a.x;
vec.y = -a.y;
vec.z = -a.z;
return vec;
}
static inline float Vec3_Dot(Vec3_t a, Vec3_t b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static inline Vec3_t Vec3_Cross(Vec3_t a, Vec3_t b)
{
Vec3_t vec;
vec.x = a.y * b.z - a.z * b.y;
vec.y = a.z * b.x - a.x * b.z;
vec.z = a.x * b.y - a.y * b.x;
return vec;
}
static inline Vec3_t Vec3_Lerp(Vec3_t a, Vec3_t b, float t)
{
Vec3_t v;
v.x = Lerp(t, a.x, b.x);
v.y = Lerp(t, a.y, b.y);
v.z = Lerp(t, a.z, b.z);
return v;
}
static inline Vec2_t Vec3_ToVec2(Vec3_t a)
{
return Vec2_Create(a.x, a.y);
}
typedef struct Vec4
{
union
{
struct
{
float x;
float y;
float z;
float w;
};
struct
{
float r;
float g;
float b;
float a;
};
float data[4];
};
} Vec4_t;
static inline Vec4_t Vec4_Zero()
{
Vec4_t vec;
vec.x = 0.0f;
vec.y = 0.0f;
vec.z = 0.0f;
vec.w = 0.0f;
return vec;
}
static inline Vec4_t Vec4_Create(float x, float y, float z, float w)
{
Vec4_t vec;
vec.x = x;
vec.y = y;
vec.z = z;
vec.w = w;
return vec;
}
static inline Vec4_t Vec4_Add(Vec4_t a, Vec4_t b)
{
Vec4_t vec;
vec.x = a.x + b.x;
vec.y = a.y + b.y;
vec.z = a.z + b.z;
vec.w = a.w + b.w;
return vec;
}
static inline Vec4_t Vec4_Sub(Vec4_t a, Vec4_t b)
{
Vec4_t vec;
vec.x = a.x - b.x;
vec.y = a.y - b.y;
vec.z = a.z - b.z;
vec.w = a.w - b.w;
return vec;
}
typedef struct
{
float m[16];
} Mat4_t;
extern void Mat4_Mult(const Mat4_t* a, const Mat4_t* b, Mat4_t* dest);
extern void Mat4_Transpose(Mat4_t* a);
extern float Mat4_Det(const Mat4_t* matrix);
extern void Mat4_Inverse(const Mat4_t* matrix, Mat4_t* ret);
extern int Mat4_UnProject(Vec3_t screenCoord, Mat4_t* matrix, Vec3_t* outVec);
extern Mat4_t Mat4_CreateLook(Vec3_t eye,
Vec3_t target,
Vec3_t up);
extern Mat4_t Mat4_CreateFrustum(float left,
float right,
float bottom,
float top,
float near,
float far);
static inline float Mat4_Get(const Mat4_t* mat, int x, int y)
{
return mat->m[x * 4 + y];
}
static inline void Mat4_Set(Mat4_t* mat, int x, int y, float val)
{
mat->m[x * 4 + y] = val;
}
static inline void Mat4_Copy(Mat4_t* dest, const Mat4_t* source)
{
memcpy(dest, source, sizeof(Mat4_t));
}
static inline void Mat4_Identity(Mat4_t* mat)
{
int i;
for (i = 0; i < 16; i++)
{
mat->m[i] = ((i % 5) ? 0 : 1);
}
}
static inline Vec3_t Mat4_MultVec3(const Mat4_t* mat, Vec3_t a)
{
Vec3_t vec;
vec.x = (a.x * mat->m[0]) + (a.y * mat->m[4]) + (a.z * mat->m[8]) + mat->m[12];
vec.y = (a.x * mat->m[1]) + (a.y * mat->m[5]) + (a.z * mat->m[9]) + mat->m[13];
vec.z = (a.x * mat->m[2]) + (a.y * mat->m[6]) + (a.z * mat->m[10]) + mat->m[14];
return vec;
}
static inline Vec4_t Mat4_MultVec4(const Mat4_t* mat, Vec4_t a)
{
Vec4_t vec;
vec.x = (a.x * mat->m[0]) + (a.y * mat->m[4]) + (a.z * mat->m[8]) + (a.w * mat->m[12]);
vec.y = (a.x * mat->m[1]) + (a.y * mat->m[5]) + (a.z * mat->m[9]) + (a.w * mat->m[13]);
vec.z = (a.x * mat->m[2]) + (a.y * mat->m[6]) + (a.z * mat->m[10]) + (a.w * mat->m[14]);
vec.w = (a.x * mat->m[3]) + (a.y * mat->m[7]) + (a.z * mat->m[11]) + (a.w * mat->m[15]);
return vec;
}
static inline void Mat4_Translate(Mat4_t* m, Vec3_t trans)
{
Mat4_Set(m, 3, 0, Mat4_Get(m, 0, 0) * trans.x + Mat4_Get(m, 1, 0) * trans.y + Mat4_Get(m, 2, 0) * trans.z + Mat4_Get(m, 3, 0));
Mat4_Set(m, 3, 1, Mat4_Get(m, 0, 1) * trans.x + Mat4_Get(m, 1, 1) * trans.y + Mat4_Get(m, 2, 1) * trans.z + Mat4_Get(m, 3, 1));
Mat4_Set(m, 3, 2, Mat4_Get(m, 0, 2) * trans.x + Mat4_Get(m, 1, 2) * trans.y + Mat4_Get(m, 2, 2) * trans.z + Mat4_Get(m, 3, 2));
Mat4_Set(m, 3, 3, Mat4_Get(m, 0, 3) * trans.x + Mat4_Get(m, 1, 3) * trans.y + Mat4_Get(m, 2, 3) * trans.z + Mat4_Get(m, 3, 3));
}
static inline void Mat4_Scale(Mat4_t* mat, Vec3_t scale)
{
mat->m[0] *= scale.x;
mat->m[4] *= scale.x;
mat->m[8] *= scale.x;
mat->m[12] *= scale.x;
mat->m[1] *= scale.y;
mat->m[5] *= scale.y;
mat->m[9] *= scale.y;
mat->m[13] *= scale.y;
mat->m[2] *= scale.z;
mat->m[6] *= scale.z;
mat->m[10] *= scale.z;
mat->m[14] *= scale.z;
}
static inline Mat4_t Mat4_CreateTranslate(Vec3_t translate)
{
Mat4_t m;
Mat4_Identity(&m);
Mat4_Set(&m, 3, 0, translate.x);
Mat4_Set(&m, 3, 1, translate.y);
Mat4_Set(&m, 3, 2, translate.z);
return m;
}
typedef struct Quat
{
union
{
struct
{
float x;
float y;
float z;
float w;
};
float data[4];
};
} Quat_t;
extern Quat_t Quat_FromEuler(float pitch, float yaw, float roll);
extern Quat_t Quat_FromAngle(float angle, float x, float y, float z);
extern void Quat_ToMatrix(Quat_t a, Mat4_t* dest);
extern Quat_t Quat_Slerp(Quat_t a, Quat_t b, float t);
static inline Quat_t Quat_Create(float x, float y, float z, float w)
{
Quat_t quat;
quat.x = x;
quat.y = y;
quat.z = z;
quat.w = w;
return quat;
}
static inline Quat_t Quat_Normalize(Quat_t quaternion)
{
Quat_t newQuat;
float mag2 = quaternion.w * quaternion.w + quaternion.x * quaternion.x + quaternion.y * quaternion.y + quaternion.z * quaternion.z;
if ( mag2 != 0.0f && (fabsf(mag2 - 1.0f) > 0.000001))
{
float mag = sqrtf(mag2);
newQuat.w /= mag;
newQuat.x /= mag;
newQuat.y /= mag;
newQuat.z /= mag;
}
return newQuat;
}
static inline Quat_t Quat_CreateIdentity()
{
Quat_t quat;
quat.x = 0.0f;
quat.y = 0.0f;
quat.z = 0.0f;
quat.w = 1.0f;
return quat;
}
static inline Quat_t Quat_Add(Quat_t a, Quat_t b)
{
Quat_t vec;
vec.x = a.x + b.x;
vec.y = a.y + b.y;
vec.z = a.z + b.z;
vec.w = a.w + b.w;
return vec;
}
static inline Quat_t Quat_Sub(Quat_t a, Quat_t b)
{
Quat_t vec;
vec.x = a.x - b.x;
vec.y = a.y - b.y;
vec.z = a.z - b.z;
vec.w = a.w - b.w;
return vec;
}
static inline Quat_t Quat_Scale(Quat_t quat, float scale)
{
Quat_t newQuat;
newQuat.x = quat.x * scale;
newQuat.y = quat.y * scale;
newQuat.z = quat.z * scale;
newQuat.w = quat.w * scale;
return newQuat;
}
static inline Quat_t Quat_Negate(Quat_t a)
{
Quat_t q;
q.x = -a.x;
q.y = -a.y;
q.z = -a.z;
q.w = a.w;
return q;
}
static inline Quat_t Quat_Mult(Quat_t a, Quat_t b)
{
Quat_t vec;
vec.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y;
vec.y = a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z;
vec.z = a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x;
vec.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
return vec;
}
static inline Vec3_t Quat_RotateVec3(Quat_t a, Vec3_t v)
{
Quat_t vecQuat, resQuat;
vecQuat.x = v.x;
vecQuat.y = v.y;
vecQuat.z = v.z;
vecQuat.w = 0.0f;
resQuat = Quat_Mult(vecQuat, Quat_Negate(a));
resQuat = Quat_Mult(a, resQuat);
return Vec3_Create(resQuat.x, resQuat.y, resQuat.z);
}
static inline float Quat_Dot(Quat_t a, Quat_t b)
{
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
#endif