Untitled
Loading...
Searching...
No Matches
utilities.h
Go to the documentation of this file.
1#ifndef UTILITIES_H
2#define UTILITIES_H
3
4#include "utilities/types.h"
5#include "utilities/mathf.h"
6#include <stdio.h>
7#include <string.h>
8
9int vector_push(Vector * v, void * o);
10void * vector_expand(Vector * v, size_t i);
11
12mat4 compose_2d_transform(vec2 transform, float angle, vec2 scale);
13
14uint16_t float32_to_float16(float f);
15void init_vector(Vector * v, size_t i, size_t type_size);
16float triangle_area(vec3 a, vec3 b, vec3 c);
17bool is_similar_plane(vec3 n1, vec3 n2, vec3 p1, vec3 p2, float epsilon);
18bool test_sphere_aabb(vec3 light_pos, float light_radius, vec3 min, vec3 max);
19
20void compute_convex_hull(vec3 *points, int count, vec3 *out_hull, int *out_count, vec3 normal);
21void compute_hull_faces(vec3 * points, size_t * indices, size_t count, size_t * out_hull, size_t * out_count, vec3 normal);
22vec3 compute_hull_centroid(vec3 *hull_points, int hull_count, float bottom, float top);
23void calculate_aabb_extents(AABB * aabb);
24void calculate_aabb_bounds(AABB * aabb);
25void compute_aabb_from_points(vec3 * points, int point_count, AABB * aabb);
26bool aabbs_intersect(AABB * a, AABB * b);
30
31float fbm (vec3 p);
32
33bool vec4_equal(vec4 a, vec4 b, float eps);
34bool vec3_equal(vec3 a, vec3 b, float eps);
36bool compute_barycentric(const vec2 uv0, const vec2 uv1, const vec2 uv2, const vec2 p, float * b0, float * b1, float * b2);
37
38bool ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c, float *out_t, vec3 *out_hit_point, vec3 * barycentric);
39
40void compute_3d_barycentric(vec3 a, vec3 b, vec3 c, vec3 p, float * wA, float * wB, float * wC);
41
42vec3 generate_bounce(uint32_t i, uint32_t N);
43vec3 generate_sphere_dir(int i, int N);
44
45bool mesh_faces_are_contiguous(MeshFace * a, vec3 * a_verts, MeshFace * b, vec3 * b_verts);
46
47vec3 project_point_plane(vec3 point, vec3 plane_point, vec3 normal);
50
51#ifndef M_PI
52#define M_PI 3.14159265358979323846f
53#endif
54
55#define M_TAU (2.0f * M_PI)
56
57static inline float wrap_angle_pi(float angle) {
58 angle = fmodf(angle + M_PI, M_TAU);
59 if (angle < 0.0f) {
60 angle += M_TAU;
61 }
62 return angle - M_PI;
63}
64
65static inline float SDF_circle(vec2 p, vec2 center, float radius) {
66 return vec2_length(vec2_sub(p, center)) - radius;
67}
68static inline float SDF_rect(vec2 p, vec2 center, vec2 size, float r) {
69 vec2 local = vec2_sub(p, center);
70 vec2 q = vec2_sub((vec2){fabsf(local.x), fabsf(local.y)}, vec2_scale(size, 0.5));
71 q = vec2_add(q, (vec2){r,r});
72 return vec2_length((vec2){fmaxf(q.x, 0.0f), fmaxf(q.y, 0.0f)}) + fminf(fmaxf(q.x, q.y), 0.0f) - r;
73}
74
75static inline vec3 vec3_min(vec3 a, vec3 b)
76{
77 a.x = MIN(a.x, b.x);
78 a.y = MIN(a.y, b.y);
79 a.z = MIN(a.z, b.z);
80 return a;
81}
82
83static inline vec3 vec3_max(vec3 a, vec3 b)
84{
85 a.x = MAX(a.x, b.x);
86 a.y = MAX(a.y, b.y);
87 a.z = MAX(a.z, b.z);
88 return a;
89}
90
91static inline vec3 vec3_clamp(vec3 a, vec3 b, vec3 c)
92{
93 a.x = clamp(a.x, b.x, c.x);
94 a.y = clamp(a.y, b.y, c.y);
95 a.z = clamp(a.z, b.z, c.z);
96 return a;
97}
98
99
100static inline bool vec2_inside_bounds(vec2 p, vec2 min, vec2 max)
101{
102
103 if (p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y)
104 {
105 return true;
106 }
107
108 return false;
109
110}
111
112
114 const vec3 *in,
115 int in_count,
116 vec3 *out,
117 vec3 plane_point,
118 vec3 plane_normal);
119int clip_pts_against_plane( ClipVertex *in, int in_count, ClipVertex *out, vec3 plane_point, vec3 plane_normal);
120
121static inline float calculate_polygon_area(vec3 p[], int n) {
122 float area = 0.0f;
123
124 int j = n - 1;
125
126 for (int i = 0; i < n; i++) {
127 area += (p[j].x + p[i].x) * (p[j].y - p[i].y);
128 j = i;
129 }
130
131 return fabsf(area / 2.0f);
132}
133
134#define CAST_VEC(vec, type) ((type*)((vec).buffer))
135
136
137static inline void print_mat4 ( mat4 *m )
138
139{
140
141 printf("MAT4:\n"
142 " % .6f % .6f % .6f % .6f\n"
143 " % .6f % .6f % .6f % .6f\n"
144 " % .6f % .6f % .6f % .6f\n"
145 " % .6f % .6f % .6f % .6f\n",
146 m->a0,m->a1,m->a2,m->a3,
147 m->b0,m->b1,m->b2,m->b3,
148 m->c0,m->c1,m->c2,m->c3,
149 m->d0,m->d1,m->d2,m->d3);
150
151}
152static inline void print_mat3 ( mat3 *m )
153{
154
155 printf("MAT3:\n"
156 " % .6f % .6f % .6f\n"
157 " % .6f % .6f % .6f\n"
158 " % .6f % .6f % .6f\n",
159 m->a0,m->a1,m->a2,
160 m->b0,m->b1,m->b2,
161 m->c0,m->c1,m->c2);
162
163
164}
165static inline vec3 vec4_to_vec3(vec4 v) {return (vec3){v.x, v.y, v.z};}
166static inline vec4 vec3_to_vec4(vec3 v) {return (vec4){v.x, v.y, v.z, 0.0};}
167
168static inline void print_aabb(AABB * aabb)
169{
170 printf("MIN: %f %f %f\n MAX: %f %f %f\n CENTER: %f %f %f\n EXTENTS: %f %f %f\n", aabb->min_x, aabb->min_y, aabb->min_z, aabb->max_x, aabb->max_y, aabb->max_z, aabb->center.x, aabb->center.y, aabb->center.z, aabb->extents.x, aabb->extents.y, aabb->extents.z);
171
172}
173
174
175static inline void print_obb(OBB * obb)
176{
177 print_mat3(&obb->rot);
178 printf("CENTER: %f %f %f\n EXTENTS: %f %f %f\n", obb->center.x, obb->center.y, obb->center.z, obb->extents.x, obb->extents.y, obb->extents.z);
179
180}
181
182
183static inline void frisvad_basis(const vec3 n, vec3 *b1, vec3 *b2) {
184 if (n.z < -0.99998796f) {
185 b1->x = 0.0f; b1->y = -1.0f; b1->z = 0.0f;
186 b2->x = -1.0f; b2->y = 0.0f; b2->z = 0.0f;
187 return;
188 }
189
190 const float a = 1.0f / (1.0f + n.z);
191 const float b = -n.x * n.y * a;
192
193 b1->x = 1.0f - n.x * n.x * a;
194 b1->y = b;
195 b1->z = -n.x;
196
197 b2->x = b;
198 b2->y = 1.0f - n.y * n.y * a;
199 b2->z = -n.y;
200}
201static inline AABB translate_aabb(AABB a, vec3 b)
202{
203 a.min_x += b.x;
204 a.max_x += b.x;
205 a.min_y += b.y;
206 a.max_y += b.y;
207 a.min_z += b.z;
208 a.max_z += b.z;
209 a.center = vec3_add(a.center, b);
210 return a;
211
212}
213
214static inline bool point_in_aabb(vec3 p, AABB * aabb)
215{
216
217
218 if (p.x >= aabb->min_x && p.x <= aabb->max_x && p.y >= aabb->min_y && p.y <= aabb->max_y && p.z >= aabb->min_z && p.z <= aabb->max_z) return true;
219 return false;
220
221}
222
223
224// copy a into b
225static inline void copy_vector(Vector * a, Vector * b)
226{
227
228 if (b->buffer) free(b->buffer);
229 *b = *a;
230 b->buffer = malloc(b->type_size * b->capacity);
231 memcpy(b->buffer, a->buffer, sizeof(b->type_size) * b->capacity);
232
233}
234
235
236
237static inline float find_hull_point_z_from_plane(Plane p, vec2 xy)
238{
239
240 return -(p.normal.x * xy.x + p.normal.y * xy.y + p.d) / p.normal.z;
241
242}
243
244static inline void compute_triangle_normal(Triangle * tri)
245{
246
247 vec3 AB = vec3_sub(tri->pts[B].pos, tri->pts[A].pos);
248 vec3 AC = vec3_sub(tri->pts[C].pos, tri->pts[A].pos);
249
250
251 tri->normal = vec3_normalize(vec3_cross(AB, AC));
252}
253
254static inline vec3 compute_tri_normal(vec3 a, vec3 b, vec3 c)
255{
256
257 vec3 AB = vec3_sub(b, a);
258 vec3 AC = vec3_sub(c, a);
259
260
261 return vec3_normalize(vec3_cross(AB, AC));
262}
263
264static inline void print_triangle(Triangle * t)
265{
266
267 printf("A: %f %f %f B: %f %f %f C: %f %f %f\n", t->pts[0].pos.x, t->pts[0].pos.y, t->pts[0].pos.z, t->pts[1].pos.x, t->pts[1].pos.y, t->pts[1].pos.z, t->pts[2].pos.x, t->pts[2].pos.y, t->pts[2].pos.z);
268}
269
270static inline void print_tri(vec3 a, vec3 b, vec3 c)
271{
272
273 printf("A: %f %f %f B: %f %f %f C: %f %f %f\n", a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z);
274}
275
276#define REMOVE_FROM_VEC(vec, index, type) \
277 type * buf = CAST_VEC(*vec, type); \
278 for (int i = index; i < (int)(vec)->count-1; i++) \
279 { \
280 buf[i] = buf[i+1]; \
281 } \
282 (vec)->count--; \
283
284#define SHIFT_VEC_RIGHT(vec, index, type) \
285 type * buf = CAST_VEC(*vec, type); \
286 for (int i = (int)(vec)->count; i > index; i--) \
287 { \
288 buf[i] = buf[i-1]; \
289 } \
290 (vec)->count++; \
291
292#define REMOVE_FROM_ARR(arr, index, count) \
293 for (int i = index; i < (int)(*count)-1; i++) \
294 { \
295 arr[i] = arr[i+1]; \
296 } \
297 (*count)--; \
298
299#define PRINT_VEC2N(vec) \
300 printf("%s x: %.3f y: %.3f\n", #vec, vec.x, vec.y); \
301
302#define PRINT_VEC3(vec, a, b, c) \
303 printf("%s %s: %.3f %s: %.3f %s: %.3f\n", #vec, a, vec.x, b, vec.y, c, vec.z); \
304
305#define PRINT_VEC3N(vec) \
306 printf("%s x: %.3f y: %.3f z: %.3f\n", #vec, vec.x, vec.y, vec.z); \
307
308#define PRINT_VEC4N(vec) \
309 printf("%s x: %.3f y: %.3f z: %.3f w: %.3f\n", #vec, vec.x, vec.y, vec.z, vec.w); \
310
311
312
313static inline vec3 mat4_mul_point(mat4 m, vec3 v) {
314 vec4 t = mul_m4_v4(m, (vec4){v.x, v.y, v.z, 1.0f});
315 if (t.w == 0) t.w = 1;
316 return (vec3){ t.x / t.w, t.y / t.w, t.z / t.w };
317}
318static inline vec3 mat4_mul_pointr(const mat4 m, vec3 v) {
319 vec4 t = mul_m4_v4r(m, (vec4){v.x, v.y, v.z, 1.0f});
320 return (vec3){ t.x / t.w, t.y / t.w, t.z / t.w };
321}
322
323static inline vec3 mat4_mul_direction(const mat4 m, vec3 v) {
324 vec4 t = mul_m4_v4(m, (vec4){v.x, v.y, v.z, 0.0f});
325 return (vec3){ t.x, t.y, t.z };
326}
327
328
329static inline mat3 quat_to_mat3(const vec4 q) {
330 float w = q.x;
331 float x = q.y;
332 float y = q.z;
333 float z = q.w;
334
335 float x2 = x * x;
336 float y2 = y * y;
337 float z2 = z * z;
338 float xy = x * y;
339 float xz = x * z;
340 float yz = y * z;
341 float wx = w * x;
342 float wy = w * y;
343 float wz = w * z;
344
345 mat3 mat;
346 mat.a0 = 1.0f - 2.0f * (y2 + z2);
347 mat.a1 = 2.0f * (xy - wz);
348 mat.a2 = 2.0f * (xz + wy);
349
350 mat.b0 = 2.0f * (xy + wz);
351 mat.b1 = 1.0f - 2.0f * (x2 + z2);
352 mat.b2 = 2.0f * (yz - wx);
353
354 mat.c0 = 2.0f * (xz - wy);
355 mat.c1 = 2.0f * (yz + wx);
356 mat.c2 = 1.0f - 2.0f * (x2 + y2);
357 return mat;
358}
359
360
361static inline vec4 quat_from_axis_angle(vec3 axis, float angle) {
362 float s = sin(angle/2);
363 vec4 quat;
364 quat.x = axis.x * s;
365 quat.y = axis.y * s;
366 quat.z = axis.z * s;
367 quat.w = cos(angle/2);
368 return quat;
369}
370
371static inline vec4 quat_copy_sign(vec4 a, vec4 b){
372
373 float dot = a.x * b.x + a.y * b.y + a.z * b.z + a.w * a.w;
374 if (dot < 0){
375 a.w = -a.w;
376 a.x = -a.x;
377 a.y = -a.y;
378 a.z = -a.z;
379 }
380 return a;
381}
382
383
384static inline vec4 quat_mul(vec4 a, vec4 b) {
385 return (vec4){
386 a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
387 a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
388 a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
389 a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
390 };
391}
392
393
394static inline vec3 get_mat4_axis_x(mat4 a) {
395 return (vec3){a.a0, a.a1, a.a2};
396}
397static inline vec3 get_mat4_axis_y(mat4 a) {
398 return (vec3){a.b0, a.b1, a.b2};
399}
400static inline vec3 get_mat4_axis_z(mat4 a) {
401 return (vec3){a.c0, a.c1, a.c2};
402}
403static inline vec3 get_mat4_axis_w(mat4 a) {
404 return (vec3){a.d0, a.d1, a.d2};
405}
406
407static inline mat4 mat4_mul_flt(mat4 a, float b)
408{
409 a.a0 *= b;
410 a.a1 *= b;
411 a.a2 *= b;
412 a.a3 *= b;
413 a.b0 *= b;
414 a.b1 *= b;
415 a.b2 *= b;
416 a.b3 *= b;
417 a.c0 *= b;
418 a.c1 *= b;
419 a.c2 *= b;
420 a.c3 *= b;
421 a.d0 *= b;
422 a.d1 *= b;
423 a.d2 *= b;
424 a.d3 *= b;
425 return a;
426
427}
428
429
430static inline mat4 mat4_identity() {
431 return (mat4){
432 1,0,0,0,
433 0,1,0,0,
434 0,0,1,0,
435 0,0,0,1
436 };
437}
438static inline mat3 mat3_identity() {
439 return (mat3){
440 1,0,0,
441 0,1,0,
442 0,0,1,
443 };
444}
445static inline mat4 mat4_translation(vec3 t) {
446 mat4 m = mat4_identity();
447 m.d0 = t.x;
448 m.d1 = t.y;
449 m.d2 = t.z;
450 return m;
451}
452static inline mat4 mat4_scale(vec3 s) {
453 mat4 m = mat4_identity();
454 m.a0 = s.x;
455 m.b1 = s.y;
456 m.c2 = s.z;
457 return m;
458}
459static inline mat4 mat4_from_quat(vec4 q) {
460 float x = q.x, y = q.y, z = q.z, w = q.w;
461 float xx = x * x, yy = y * y, zz = z * z;
462 float xy = x * y, xz = x * z, yz = y * z;
463 float wx = w * x, wy = w * y, wz = w * z;
464
465 mat4 m = mat4_identity();
466
467 m.a0 = 1.0f - 2.0f * (yy + zz);
468 m.a1 = 2.0f * (xy + wz);
469 m.a2 = 2.0f * (xz - wy);
470
471 m.b0 = 2.0f * (xy - wz);
472 m.b1 = 1.0f - 2.0f * (xx + zz);
473 m.b2 = 2.0f * (yz + wx);
474
475 m.c0 = 2.0f * (xz + wy);
476 m.c1 = 2.0f * (yz - wx);
477 m.c2 = 1.0f - 2.0f * (xx + yy);
478
479 return m;
480}
481static inline mat4 compose_trs_from_vectors(vec3 pos, vec4 rot, vec3 scale) {
482 mat4 T = mat4_translation(pos);
484 mat4 S = mat4_scale(scale);
485
486 return m4_mul(T, m4_mul(R, S));
487}
488
489
490static inline mat3 mat3_from_mat4(const mat4 *m) {
491 return (mat3){
492 m->a0, m->a1, m->a2,
493 m->b0, m->b1, m->b2,
494 m->c0, m->c1, m->c2
495 };
496}
497
498static inline vec3 mat3_mul_vec3(mat3 m, vec3 v) {
499 return (vec3){
500 m.a0*v.x + m.a1*v.y + m.a2*v.z,
501 m.b0*v.x + m.b1*v.y + m.b2*v.z,
502 m.c0*v.x + m.c1*v.y + m.c2*v.z
503 };
504}
505static inline vec3 mat3_mul_vec3r(mat3 m, vec3 v) {
506 return (vec3){
507 m.a0*v.x + m.b0*v.y + m.c0*v.z,
508 m.a1*v.x + m.b1*v.y + m.c1*v.z,
509 m.a2*v.x + m.b2*v.y + m.c2*v.z
510 };
511}
512
513static inline void mat4_transpose(float * dst, const float * src)
514{
515 for (int i = 0; i < 4; ++i)
516 {
517 for (int j = 0; j < 4; ++j)
518 {
519 dst[j * 4 + i] = src[i * 4 + j];
520 }
521 }
522}
523
524
525static inline mat4 m4_transpose(mat4 in){
526 mat4 out;
527 out.a0 = in.a0;
528 out.a1 = in.b0;
529 out.a2 = in.c0;
530 out.a3 = in.d0;
531 out.b0 = in.a1;
532 out.b1 = in.b1;
533 out.b2 = in.c1;
534 out.b3 = in.d1;
535 out.c0 = in.a2;
536 out.c1 = in.b2;
537 out.c2 = in.c2;
538 out.c3 = in.d2;
539 out.d0 = in.a3;
540 out.d1 = in.b3;
541 out.d2 = in.c3;
542 out.d3 = in.d3;
543 return out;
544}
545static inline mat3 mat3_transpose(mat3 in){
546 mat3 out;
547 out.a0 = in.a0;
548 out.a1 = in.b0;
549 out.a2 = in.c0;
550 out.b0 = in.a1;
551 out.b1 = in.b1;
552 out.b2 = in.c1;
553 out.c0 = in.a2;
554 out.c1 = in.b2;
555 out.c2 = in.c2;
556 return out;
557}
558
559static inline mat3 mat3_mul(mat3 M, mat3 N) {
560 mat3 R;
561
562 R.a0 = M.a0 * N.a0 + M.a1 * N.b0 + M.a2 * N.c0;
563 R.a1 = M.a0 * N.a1 + M.a1 * N.b1 + M.a2 * N.c1;
564 R.a2 = M.a0 * N.a2 + M.a1 * N.b2 + M.a2 * N.c2;
565
566 R.b0 = M.b0 * N.a0 + M.b1 * N.b0 + M.b2 * N.c0;
567 R.b1 = M.b0 * N.a1 + M.b1 * N.b1 + M.b2 * N.c1;
568 R.b2 = M.b0 * N.a2 + M.b1 * N.b2 + M.b2 * N.c2;
569
570 R.c0 = M.c0 * N.a0 + M.c1 * N.b0 + M.c2 * N.c0;
571 R.c1 = M.c0 * N.a1 + M.c1 * N.b1 + M.c2 * N.c1;
572 R.c2 = M.c0 * N.a2 + M.c1 * N.b2 + M.c2 * N.c2;
573
574 return R;
575}
577 float det =
578 m.a0*(m.b1*m.c2 - m.b2*m.c1) -
579 m.a1*(m.b0*m.c2 - m.b2*m.c0) +
580 m.a2*(m.b0*m.c1 - m.b1*m.c0);
581
582 float invDet = (det != 0.0f) ? 1.0f/det : 1.0f;
583
584 mat3 r;
585 r.a0 = (m.b1*m.c2 - m.b2*m.c1) * invDet;
586 r.a1 = -(m.a1*m.c2 - m.a2*m.c1) * invDet;
587 r.a2 = (m.a1*m.b2 - m.a2*m.b1) * invDet;
588
589 r.b0 = -(m.b0*m.c2 - m.b2*m.c0) * invDet;
590 r.b1 = (m.a0*m.c2 - m.a2*m.c0) * invDet;
591 r.b2 = -(m.a0*m.b2 - m.a2*m.b0) * invDet;
592
593 r.c0 = (m.b0*m.c1 - m.b1*m.c0) * invDet;
594 r.c1 = -(m.a0*m.c1 - m.a1*m.c0) * invDet;
595 r.c2 = (m.a0*m.b1 - m.a1*m.b0) * invDet;
596
597 return r;
598}
599
600static inline void OBB_vectors(mat3 rot, vec3 * fwd, vec3 * right, vec3 * up)
601{
602 *fwd = (vec3){rot.a0, rot.b0, rot.c0};
603 *right = (vec3){rot.a1, rot.b1, rot.c1};
604 *up = (vec3){rot.a2, rot.b2, rot.c2};
605}
606
607static inline vec3 OBB_min_corner(OBB * obb)
608{
609
610 vec3 fwd; vec3 right; vec3 up;
611 OBB_vectors(obb->rot, &fwd, &right, &up);
612
613 vec3 pos = vec3_add(obb->center,
614 vec3_add(vec3_scale(fwd, -obb->extents.x),
615 vec3_add(vec3_scale(right, -obb->extents.y),
616 vec3_scale(up, -obb->extents.z))));
617 return pos;
618
619}
620
621
622
623static inline bool point_in_obb(OBB * obb, vec3 pos)
624{
625
626 vec3 local = vec3_sub(pos, obb->center);
627 vec3 norm = mat3_mul_vec3(mat3_inverse_transpose(obb->rot), local);
628
629 if (fabsf(norm.x) <= obb->extents.x &&
630 fabsf(norm.y) <= obb->extents.y &&
631 fabsf(norm.z) <= obb->extents.z)
632 {
633 return true;
634 }
635
636 return false;
637}
638
639
641{
642 return vec3_scale(vec3_add(vec3_add(A, B), C), 0.333333f);
643}
644
645#endif
RayHit ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c)
vec3 generate_sphere_dir(int i, int N)
void frisvad_basis(in vec3 n, out vec3 b1, out vec3 b2)
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
Definition camera.c:1004
bool test_sphere_aabb(vec3 light_pos, float light_radius, vec4 min, vec4 max)
AABBBounds transform_aabb_by_model_matrix(MeshNode mn, mat4 mat)
uniform uint n
vec3 extents
Definition dynamic.frag:23
vec3 center
Definition dynamic.frag:15
mat3 rot
Definition types.h:49
Oriented Bounding Boxes.
Definition dynamic.frag:14
vec3 compute_3d_barycentric(vec3 a, vec3 b, vec3 c, vec3 p)
vec3 vec3_cross(vec3 a, vec3 b)
Definition mathf.h:80
#define MIN(a, b)
Definition mathf.h:435
vec3 vec3_normalize(vec3 v)
Definition mathf.h:46
mat4 m4_mul(mat4 a, mat4 b)
Definition mathf.h:401
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:248
vec4 mul_m4_v4(mat4 m, vec4 v)
Definition mathf.h:383
vec4 mul_m4_v4r(mat4 m, vec4 v)
Definition mathf.h:392
vec2 vec2_add(vec2 a, vec2 b)
Definition mathf.h:274
vec2 vec2_scale(vec2 a, float b)
Definition mathf.h:244
vec3 vec3_sub(vec3 a, vec3 b)
Definition mathf.h:266
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:286
vec2 vec2_sub(vec2 a, vec2 b)
Definition mathf.h:270
float clamp(float a, float b, float c)
Definition mathf.h:379
float vec2_length(vec2 v)
Definition mathf.h:336
#define MAX(a, b)
Definition mathf.h:433
const int size
Definition sky.frag:9
float SDF_rect(vec2 p, vec2 center, vec2 size, float r)
Definition text.frag:35
float SDF_circle(vec2 p, vec2 center, float radius)
Definition text.frag:30
float x
Definition types.h:11
float a1
Definition types.h:21
float w
Definition types.h:13
float c2
Definition types.h:20
float a0
Definition types.h:20
float z
Definition types.h:13
float d3
Definition types.h:21
vec3 normal
Definition types.h:275
float c2
Definition types.h:21
float a1
Definition types.h:20
float b0
Definition types.h:21
float c1
Definition types.h:21
float x
Definition types.h:13
float c0
Definition types.h:20
float a2
Definition types.h:21
@ B
Definition types.h:26
@ A
Definition types.h:26
@ C
Definition types.h:26
float x
Definition types.h:12
float b0
Definition types.h:20
float max_z
Definition types.h:39
float b1
Definition types.h:20
float min_x
Definition types.h:37
float d
Definition types.h:294
size_t type_size
Definition types.h:209
float max_y
Definition types.h:38
float b3
Definition types.h:21
float y
Definition types.h:11
float y
Definition types.h:13
float y
Definition types.h:12
float d1
Definition types.h:21
float a3
Definition types.h:21
float c0
Definition types.h:21
vec3 pos
Definition types.h:220
float z
Definition types.h:12
float min_z
Definition types.h:39
float b2
Definition types.h:21
float d0
Definition types.h:21
float min_y
Definition types.h:38
vec3 normal
Definition types.h:293
Point pts[3]
Definition types.h:273
float b2
Definition types.h:20
size_t capacity
Definition types.h:209
void * buffer
Definition types.h:210
float a2
Definition types.h:20
float a0
Definition types.h:21
float c3
Definition types.h:21
float c1
Definition types.h:20
float b1
Definition types.h:21
float d2
Definition types.h:21
float max_x
Definition types.h:37
Definition types.h:292
Definition types.h:20
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13
AABB transform_aabb_by_model_matrix_rotation(AABB aabb, mat4 mat)
Definition utilities.c:499
bool vec4_equal(vec4 a, vec4 b, float eps)
Definition utilities.c:525
void compute_convex_hull(vec3 *points, int count, vec3 *out_hull, int *out_count, vec3 normal)
Definition utilities.c:676
bool is_similar_plane(vec3 n1, vec3 n2, vec3 p1, vec3 p2, float epsilon)
Definition utilities.c:330
void init_vector(Vector *v, size_t i, size_t type_size)
Definition utilities.c:74
bool compute_barycentric(const vec2 uv0, const vec2 uv1, const vec2 uv2, const vec2 p, float *b0, float *b1, float *b2)
Definition utilities.c:385
bool vec3_equal(vec3 a, vec3 b, float eps)
Definition utilities.c:541
int clip_against_plane(const vec3 *in, int in_count, vec3 *out, vec3 plane_point, vec3 plane_normal)
Definition utilities.c:794
void calculate_aabb_extents(AABB *aabb)
Definition utilities.c:298
void compute_aabb_from_points(vec3 *points, int point_count, AABB *aabb)
Definition utilities.c:768
vec3 closest_point_segment(vec3 p, vec3 a, vec3 b)
Definition utilities.c:1244
bool aabbs_intersect(AABB *a, AABB *b)
Definition utilities.c:276
int clip_pts_against_plane(ClipVertex *in, int in_count, ClipVertex *out, vec3 plane_point, vec3 plane_normal)
Definition utilities.c:838
float triangle_area(vec3 a, vec3 b, vec3 c)
Definition utilities.c:318
vec3 generate_bounce(uint32_t i, uint32_t N)
Definition utilities.c:429
AABB transform_aabb_by_position(AABB aabb, vec3 pos)
Definition utilities.c:466
void calculate_aabb_bounds(AABB *aabb)
Definition utilities.c:305
bool triangles_are_contiguous(Triangle *a, Triangle *b)
Definition utilities.c:557
void compute_hull_faces(vec3 *points, size_t *indices, size_t count, size_t *out_hull, size_t *out_count, vec3 normal)
Definition utilities.c:718
bool mesh_faces_are_contiguous(MeshFace *a, vec3 *a_verts, MeshFace *b, vec3 *b_verts)
Definition utilities.c:578
uint16_t float32_to_float16(float f)
Definition utilities.c:110
vec3 closest_point_triangle(vec3 p, vec3 a, vec3 b, vec3 c)
Definition utilities.c:1327
void * vector_expand(Vector *v, size_t i)
Definition utilities.c:49
float fbm(vec3 p)
Definition utilities.c:222
int vector_push(Vector *v, void *o)
Definition utilities.c:25
vec3 project_point_plane(vec3 point, vec3 plane_point, vec3 normal)
Definition utilities.c:1235
mat4 compose_2d_transform(vec2 transform, float angle, vec2 scale)
Definition utilities.c:97
vec3 mat3_mul_vec3(mat3 m, vec3 v)
Definition utilities.h:498
bool point_in_aabb(vec3 p, AABB *aabb)
Definition utilities.h:214
vec4 vec3_to_vec4(vec3 v)
Definition utilities.h:166
void print_aabb(AABB *aabb)
Definition utilities.h:168
mat3 quat_to_mat3(const vec4 q)
Definition utilities.h:329
vec3 vec3_clamp(vec3 a, vec3 b, vec3 c)
Definition utilities.h:91
AABB translate_aabb(AABB a, vec3 b)
Definition utilities.h:201
vec4 quat_mul(vec4 a, vec4 b)
Definition utilities.h:384
mat4 mat4_translation(vec3 t)
Definition utilities.h:445
void print_mat3(mat3 *m)
Definition utilities.h:152
mat4 compose_trs_from_vectors(vec3 pos, vec4 rot, vec3 scale)
Definition utilities.h:481
void mat4_transpose(float *dst, const float *src)
Definition utilities.h:513
vec3 OBB_min_corner(OBB *obb)
Definition utilities.h:607
vec3 get_mat4_axis_z(mat4 a)
Definition utilities.h:400
vec3 compute_hull_centroid(vec3 *hull_points, int hull_count, float bottom, float top)
mat3 mat3_mul(mat3 M, mat3 N)
Definition utilities.h:559
vec3 get_mat4_axis_y(mat4 a)
Definition utilities.h:397
vec3 vec3_min(vec3 a, vec3 b)
Definition utilities.h:75
vec4 quat_copy_sign(vec4 a, vec4 b)
Definition utilities.h:371
vec3 mat4_mul_direction(const mat4 m, vec3 v)
Definition utilities.h:323
mat4 mat4_mul_flt(mat4 a, float b)
Definition utilities.h:407
void print_mat4(mat4 *m)
Definition utilities.h:137
void print_tri(vec3 a, vec3 b, vec3 c)
Definition utilities.h:270
mat3 mat3_transpose(mat3 in)
Definition utilities.h:545
mat3 mat3_inverse_transpose(mat3 m)
Definition utilities.h:576
float wrap_angle_pi(float angle)
Definition utilities.h:57
vec3 get_mat4_axis_x(mat4 a)
Definition utilities.h:394
mat4 mat4_from_quat(vec4 q)
Definition utilities.h:459
vec3 mat4_mul_point(mat4 m, vec3 v)
Definition utilities.h:313
vec3 get_mat4_axis_w(mat4 a)
Definition utilities.h:403
void copy_vector(Vector *a, Vector *b)
Definition utilities.h:225
vec3 compute_tri_normal(vec3 a, vec3 b, vec3 c)
Definition utilities.h:254
bool vec2_inside_bounds(vec2 p, vec2 min, vec2 max)
Definition utilities.h:100
vec3 vec4_to_vec3(vec4 v)
Definition utilities.h:165
float calculate_polygon_area(vec3 p[], int n)
Definition utilities.h:121
mat4 mat4_identity()
Definition utilities.h:430
bool point_in_obb(OBB *obb, vec3 pos)
Definition utilities.h:623
void compute_triangle_normal(Triangle *tri)
Definition utilities.h:244
vec4 quat_from_axis_angle(vec3 axis, float angle)
Definition utilities.h:361
mat3 mat3_identity()
Definition utilities.h:438
mat3 mat3_from_mat4(const mat4 *m)
Definition utilities.h:490
mat4 m4_transpose(mat4 in)
Definition utilities.h:525
vec3 mat3_mul_vec3r(mat3 m, vec3 v)
Definition utilities.h:505
void print_obb(OBB *obb)
Definition utilities.h:175
float find_hull_point_z_from_plane(Plane p, vec2 xy)
Definition utilities.h:237
vec3 mat4_mul_pointr(const mat4 m, vec3 v)
Definition utilities.h:318
vec3 vec3_max(vec3 a, vec3 b)
Definition utilities.h:83
void print_triangle(Triangle *t)
Definition utilities.h:264
void OBB_vectors(mat3 rot, vec3 *fwd, vec3 *right, vec3 *up)
Definition utilities.h:600
#define M_TAU
Definition utilities.h:55
#define M_PI
Definition utilities.h:52
vec3 triangle_centroid(vec3 A, vec3 B, vec3 C)
Definition utilities.h:640
mat4 mat4_scale(vec3 s)
Definition utilities.h:452