52#define M_PI 3.14159265358979323846f
55#define M_TAU (2.0f * M_PI)
72 return vec2_length((
vec2){fmaxf(q.
x, 0.0f), fmaxf(q.
y, 0.0f)}) + fminf(fmaxf(q.
x, q.
y), 0.0f) - r;
103 if (p.
x >= min.
x && p.
x <= max.
x && p.
y >= min.
y && p.
y <= max.
y)
126 for (
int i = 0; i <
n; i++) {
127 area += (p[j].
x + p[i].
x) * (p[j].y - p[i].y);
131 return fabsf(area / 2.0f);
134#define CAST_VEC(vec, type) ((type*)((vec).buffer))
142 " % .6f % .6f % .6f % .6f\n"
143 " % .6f % .6f % .6f % .6f\n"
144 " % .6f % .6f % .6f % .6f\n"
145 " % .6f % .6f % .6f % .6f\n",
156 " % .6f % .6f % .6f\n"
157 " % .6f % .6f % .6f\n"
158 " % .6f % .6f % .6f\n",
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);
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;
190 const float a = 1.0f / (1.0f +
n.z);
191 const float b = -
n.x *
n.y * a;
193 b1->
x = 1.0f -
n.x *
n.x * a;
198 b2->
y = 1.0f -
n.y *
n.y * a;
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);
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++) \
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--) \
292#define REMOVE_FROM_ARR(arr, index, count) \
293 for (int i = index; i < (int)(*count)-1; i++) \
299#define PRINT_VEC2N(vec) \
300 printf("%s x: %.3f y: %.3f\n", #vec, vec.x, vec.y); \
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); \
305#define PRINT_VEC3N(vec) \
306 printf("%s x: %.3f y: %.3f z: %.3f\n", #vec, vec.x, vec.y, vec.z); \
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); \
315 if (t.
w == 0) t.
w = 1;
316 return (
vec3){ t.
x / t.
w, t.
y / t.
w, t.
z / t.
w };
320 return (
vec3){ t.
x / t.
w, t.
y / t.
w, t.
z / t.
w };
325 return (
vec3){ t.
x, t.
y, t.
z };
346 mat.
a0 = 1.0f - 2.0f * (y2 + z2);
347 mat.
a1 = 2.0f * (xy - wz);
348 mat.
a2 = 2.0f * (xz + wy);
350 mat.
b0 = 2.0f * (xy + wz);
351 mat.
b1 = 1.0f - 2.0f * (x2 + z2);
352 mat.
b2 = 2.0f * (yz - wx);
354 mat.
c0 = 2.0f * (xz - wy);
355 mat.
c1 = 2.0f * (yz + wx);
356 mat.
c2 = 1.0f - 2.0f * (x2 + y2);
362 float s = sin(angle/2);
367 quat.
w = cos(angle/2);
373 float dot = a.
x * b.
x + a.
y * b.
y + a.
z * b.
z + a.
w * a.
w;
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
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;
467 m.
a0 = 1.0f - 2.0f * (yy + zz);
468 m.
a1 = 2.0f * (xy + wz);
469 m.
a2 = 2.0f * (xz - wy);
471 m.
b0 = 2.0f * (xy - wz);
472 m.
b1 = 1.0f - 2.0f * (xx + zz);
473 m.
b2 = 2.0f * (yz + wx);
475 m.
c0 = 2.0f * (xz + wy);
476 m.
c1 = 2.0f * (yz - wx);
477 m.
c2 = 1.0f - 2.0f * (xx + yy);
515 for (
int i = 0; i < 4; ++i)
517 for (
int j = 0; j < 4; ++j)
519 dst[j * 4 + i] = src[i * 4 + j];
582 float invDet = (det != 0.0f) ? 1.0f/det : 1.0f;
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.
bool test_sphere_aabb(vec3 light_pos, float light_radius, vec4 min, vec4 max)
AABBBounds transform_aabb_by_model_matrix(MeshNode mn, mat4 mat)
vec3 compute_3d_barycentric(vec3 a, vec3 b, vec3 c, vec3 p)
vec3 vec3_cross(vec3 a, vec3 b)
vec3 vec3_normalize(vec3 v)
mat4 m4_mul(mat4 a, mat4 b)
vec3 vec3_scale(vec3 a, float b)
vec4 mul_m4_v4(mat4 m, vec4 v)
vec4 mul_m4_v4r(mat4 m, vec4 v)
vec2 vec2_add(vec2 a, vec2 b)
vec2 vec2_scale(vec2 a, float b)
vec3 vec3_sub(vec3 a, vec3 b)
vec3 vec3_add(vec3 a, vec3 b)
vec2 vec2_sub(vec2 a, vec2 b)
float clamp(float a, float b, float c)
float vec2_length(vec2 v)
float SDF_rect(vec2 p, vec2 center, vec2 size, float r)
float SDF_circle(vec2 p, vec2 center, float radius)
AABB transform_aabb_by_model_matrix_rotation(AABB aabb, mat4 mat)
bool vec4_equal(vec4 a, vec4 b, float eps)
void compute_convex_hull(vec3 *points, int count, vec3 *out_hull, int *out_count, vec3 normal)
bool is_similar_plane(vec3 n1, vec3 n2, vec3 p1, vec3 p2, float epsilon)
void init_vector(Vector *v, size_t i, size_t type_size)
bool compute_barycentric(const vec2 uv0, const vec2 uv1, const vec2 uv2, const vec2 p, float *b0, float *b1, float *b2)
bool vec3_equal(vec3 a, vec3 b, float eps)
int clip_against_plane(const vec3 *in, int in_count, vec3 *out, vec3 plane_point, vec3 plane_normal)
void calculate_aabb_extents(AABB *aabb)
void compute_aabb_from_points(vec3 *points, int point_count, AABB *aabb)
vec3 closest_point_segment(vec3 p, vec3 a, vec3 b)
bool aabbs_intersect(AABB *a, AABB *b)
int clip_pts_against_plane(ClipVertex *in, int in_count, ClipVertex *out, vec3 plane_point, vec3 plane_normal)
float triangle_area(vec3 a, vec3 b, vec3 c)
vec3 generate_bounce(uint32_t i, uint32_t N)
AABB transform_aabb_by_position(AABB aabb, vec3 pos)
void calculate_aabb_bounds(AABB *aabb)
bool triangles_are_contiguous(Triangle *a, Triangle *b)
void compute_hull_faces(vec3 *points, size_t *indices, size_t count, size_t *out_hull, size_t *out_count, vec3 normal)
bool mesh_faces_are_contiguous(MeshFace *a, vec3 *a_verts, MeshFace *b, vec3 *b_verts)
uint16_t float32_to_float16(float f)
vec3 closest_point_triangle(vec3 p, vec3 a, vec3 b, vec3 c)
void * vector_expand(Vector *v, size_t i)
int vector_push(Vector *v, void *o)
vec3 project_point_plane(vec3 point, vec3 plane_point, vec3 normal)
mat4 compose_2d_transform(vec2 transform, float angle, vec2 scale)
vec3 mat3_mul_vec3(mat3 m, vec3 v)
bool point_in_aabb(vec3 p, AABB *aabb)
vec4 vec3_to_vec4(vec3 v)
void print_aabb(AABB *aabb)
mat3 quat_to_mat3(const vec4 q)
vec3 vec3_clamp(vec3 a, vec3 b, vec3 c)
AABB translate_aabb(AABB a, vec3 b)
vec4 quat_mul(vec4 a, vec4 b)
mat4 mat4_translation(vec3 t)
mat4 compose_trs_from_vectors(vec3 pos, vec4 rot, vec3 scale)
void mat4_transpose(float *dst, const float *src)
vec3 OBB_min_corner(OBB *obb)
vec3 get_mat4_axis_z(mat4 a)
vec3 compute_hull_centroid(vec3 *hull_points, int hull_count, float bottom, float top)
mat3 mat3_mul(mat3 M, mat3 N)
vec3 get_mat4_axis_y(mat4 a)
vec3 vec3_min(vec3 a, vec3 b)
vec4 quat_copy_sign(vec4 a, vec4 b)
vec3 mat4_mul_direction(const mat4 m, vec3 v)
mat4 mat4_mul_flt(mat4 a, float b)
void print_tri(vec3 a, vec3 b, vec3 c)
mat3 mat3_transpose(mat3 in)
mat3 mat3_inverse_transpose(mat3 m)
float wrap_angle_pi(float angle)
vec3 get_mat4_axis_x(mat4 a)
mat4 mat4_from_quat(vec4 q)
vec3 mat4_mul_point(mat4 m, vec3 v)
vec3 get_mat4_axis_w(mat4 a)
void copy_vector(Vector *a, Vector *b)
vec3 compute_tri_normal(vec3 a, vec3 b, vec3 c)
bool vec2_inside_bounds(vec2 p, vec2 min, vec2 max)
vec3 vec4_to_vec3(vec4 v)
float calculate_polygon_area(vec3 p[], int n)
bool point_in_obb(OBB *obb, vec3 pos)
void compute_triangle_normal(Triangle *tri)
vec4 quat_from_axis_angle(vec3 axis, float angle)
mat3 mat3_from_mat4(const mat4 *m)
mat4 m4_transpose(mat4 in)
vec3 mat3_mul_vec3r(mat3 m, vec3 v)
float find_hull_point_z_from_plane(Plane p, vec2 xy)
vec3 mat4_mul_pointr(const mat4 m, vec3 v)
vec3 vec3_max(vec3 a, vec3 b)
void print_triangle(Triangle *t)
void OBB_vectors(mat3 rot, vec3 *fwd, vec3 *right, vec3 *up)
vec3 triangle_centroid(vec3 A, vec3 B, vec3 C)