16 printf(
"ERR: NO TYPE SIZE FOR VECTOR SPECIFIED\n");
64 if (!v->
buffer) printf(
"BUFFER WITHOUT SIZE\n");
69 int original_count = v->
count;
80 v->
buffer = malloc(type_size * i);
98 float c = cosf(angle);
99 float s = sinf(angle);
102 out.
a0 = c * scale.
x; out.
b0 = -s * scale.
y; out.
c0 = 0.0f; out.
d0 = transform.
x;
103 out.
a1 = s * scale.
x; out.
b1 = c * scale.
y; out.
c1 = 0.0f; out.
d1 = transform.
y;
104 out.
a2 = 0.0f; out.
b2 = 0.0f; out.
c2 = 1.0f; out.
d2 = 0.0f;
105 out.
a3 = 0.0f; out.
b3 = 0.0f; out.
c3 = 0.0f; out.
d3 = 1.0f;
111 uint32_t x = *(uint32_t*)&f;
113 uint32_t sign = (x >> 16) & 0x8000;
114 int32_t exponent = ((x >> 23) & 0xff) - 127;
115 uint32_t mantissa = x & 0x7fffff;
117 if (exponent <= -15) {
120 if (exponent >= 16) {
121 return sign | 0x7c00;
124 uint32_t new_exponent = (exponent + 15) << 10;
125 uint32_t new_mantissa = mantissa >> 13;
127 return sign | new_exponent | new_mantissa;
135{
return x - floorf(x * (1.0f / 289.0f)) * 289.0f; }
146 (
vec4){ 1.0f, 1.0f, 1.0f, 1.0f }
166 p.
x = x.
x - floorf(x.
x);
167 p.
y = x.
y - floorf(x.
y);
168 p.
z = x.
z - floorf(x.
z);
181 p.
x = x.
x - floorf(x.
x);
182 p.
y = x.
y - floorf(x.
y);
183 p.
z = x.
z - floorf(x.
z);
184 p.
w = x.
w - floorf(x.
w);
197 vec3 a = (
vec3){floorf(p.
x), floorf(p.
y), floorf(p.
z)};
216 return o4.
y * d.
y + o4.
x * (1.0 - d.
y);
231 for (
int i = 0; i < 5; ++i) {
253 return vec3_dot(delta, delta) <= (light_radius * light_radius);
322 ab.
y * ac.
z - ab.
z * ac.
y,
323 ab.
z * ac.
x - ab.
x * ac.
z,
324 ab.
x * ac.
y - ab.
y * ac.
x
326 return sqrtf(cross.
x * cross.
x + cross.
y * cross.
y + cross.
z * cross.
z) * 0.5f;
333 if (dot < 1 - epsilon)
return false;
337 return fabsf(d1 - d2) < epsilon;
348 if (fabsf(dA) < 0.000001f) {
355 if (u < 0.0f || u > 1.0f) {
361 if (v < 0.0f || u + v > 1.0f) {
369 float w = 1.0f - u - v;
386 const vec2 p,
float * b0,
float * b1,
float * b2)
388 float x0 = uv0.
x, y0 = uv0.
y;
389 float x1 = uv1.
x, y1 = uv1.
y;
390 float x2 = uv2.
x, y2 = uv2.
y;
391 float x = p.
x, y = p.
y;
393 float denom = (y1 - y2)*(x0 - x2) + (x2 - x1)*(y0 - y2);
394 if (fabsf(denom) < 1e-6f)
400 float inv_denom = 1.0f / denom;
402 *b0 = ((y1 - y2)*(x - x2) + (x2 - x1)*(y - y2)) * inv_denom;
403 *b1 = ((y2 - y0)*(x - x2) + (x0 - x2)*(y - y2)) * inv_denom;
404 *b2 = 1.0f - *b0 - *b1;
421 float wwA = 1.0f - wwB - wwC;
437 float u = (ix + (float)
rand() / RAND_MAX) / grid;
438 float v = (iy + (float)
rand() / RAND_MAX) / grid;
440 float theta = 2.0f *
M_PI * v;
442 float x = r * cos(theta);
443 float y = r * sin(theta);
444 float z = sqrt(fmaxf(0, 1.0f - x*x - y*y));
445 return (
vec3){x,y,z};
452 float phi =
M_PI * (3.0f - sqrtf(5.0f));
454 float y = 1.0f - 2.0f*(i+0.5f)/N;
456 float r = sqrtf(1-y*y);
527 if (fabsf(a.
x - b.
x) < eps &&
528 fabsf(a.
y - b.
y) < eps &&
529 fabsf(a.
z - b.
z) < eps &&
530 fabsf(a.
w - b.
w) < eps)
543 if (fabsf(a.
x - b.
x) < eps &&
544 fabsf(a.
y - b.
y) < eps &&
545 fabsf(a.
z - b.
z) < eps)
573 if (eq >= 3)
return true;
583 for (
int i = 0; i < 3; i++)
586 size_t i1 = a->
indices[(i+1)%3];
587 vec3 a0 = a_verts[i0];
588 vec3 a1 = a_verts[i1];
589 for (
int j = 0; j < 3; j++)
592 size_t j1 = b->
indices[(j+1)%3];
593 vec3 b0 = b_verts[j0];
594 vec3 b1 = b_verts[j1];
613 for (
int i = 1; i < count; i++) {
614 if (points[i].x < points[min_idx].x ||
615 (points[i].x == points[min_idx].x && points[i].y < points[min_idx].y)) {
623 for (
int i = 1; i < count; i++) {
624 if (points[i].z < points[min_idx].z ||
625 (points[i].z == points[min_idx].z && points[i].y < points[min_idx].y)) {
633 for (
int i = 1; i < count; i++) {
634 if (points[indices[i]].x < points[indices[min_idx]].x ||
635 (points[indices[i]].x == points[indices[min_idx]].x && points[indices[i]].y < points[indices[min_idx]].y)) {
643 for (
int i = 1; i < count; i++) {
644 if (points[indices[i]].z < points[indices[min_idx]].z ||
645 (points[indices[i]].z == points[indices[min_idx]].z && points[indices[i]].y < points[indices[min_idx]].y)) {
653 for (
int i = 1; i < count; i++) {
654 if (points[i].x < points[min_idx].x ||
655 (points[i].x == points[min_idx].x && points[i].y < points[min_idx].y)) {
663 float cross = (a.
x - p.
x) * (b.
y - p.
y) - (a.
y - p.
y) * (b.
x - p.
x);
668 float cross = (a.
x - p.
x) * (b.
y - p.
y) - (a.
y - p.
y) * (b.
x - p.
x);
672 float cross = (a.
z - p.
z) * (b.
y - p.
y) - (a.
y - p.
y) * (b.
z - p.
z);
677 if (count < 3)
return;
681 bool z_deg = 1 - fabsf(normal.
z) < 0.001f;
684 int max_steps = count + 2;
687 if (++steps > max_steps) {
688 fprintf(stderr,
"Error: convex hull infinite loop detected.\n");
691 out_hull[(*out_count)++] = points[p];
693 for (
int i = 0; i < count; i++) {
698 if (
ccwr(points[p], points[i], points[q])) {
706 if (
ccw(points[p], points[i], points[q])) {
720 if (count < 3)
return;
726 if (fabsf(N.
z) < 0.9f)
734 vec2 * local_points = malloc(
sizeof(
vec2) * count);
736 for (
int i = 0; i < count; i++)
738 vec3 p = points[indices[i]];
744 bool z_deg = 1 - fabsf(normal.
z) < 0.001f;
747 int max_steps = count + 2;
750 if (++steps > max_steps) {
751 fprintf(stderr,
"Error: convex hull infinite loop detected.\n");
754 out_hull[(*out_count)++] = indices[p];
756 for (
int i = 0; i < count; i++) {
759 if (
ccwv2(local_points[p], local_points[i], local_points[q])) {
770 aabb->
min_x = INFINITY;
771 aabb->
min_y = INFINITY;
772 aabb->
min_z = INFINITY;
773 aabb->
max_x = -INFINITY;
774 aabb->
max_y = -INFINITY;
775 aabb->
max_z = -INFINITY;
777 for (
int i = 0; i < point_count; i++)
803 for(
int i=0;i<in_count;i++)
805 int j = (i+1)%in_count;
818 out[out_count++] =
B;
822 float t = da/(da-db);
828 float t = da/(da-db);
831 out[out_count++] =
B;
842 for(
int i=0;i<in_count;i++)
844 int j = (i+1)%in_count;
857 out[out_count].
p =
B;
864 float t = da/(da-db);
873 float t = da/(da-db);
878 out[out_count].
p =
B;
938 vec3 p = plane_point;
942 for (
int k = 0; k < 3; k++)
963 else if (splits == 3)
973 int new_point_count = 0;
974 for (
int k = 0; k < 3; k++)
977 float db = d[(k+1) % 3];
978 if (s[k] != s[(k+1) % 3])
986 float t = da / (da - db);
987 new_points[new_point_count++] =
995 inside[0] = new_points[0];
996 inside[1] = new_points[1];
997 size_t ib = points->
count - 2;
998 size_t ic = points->
count - 1;
1000 int opp_outside_tri_emitted = 0;
1001 int opp_inside_tri_emitted = 0;
1005 int in_idx[3] = {0};
1007 for (
int k = 0; k < 3; k++)
1015 in_idx[in_idx_cnt++] = k;
1024 inside[0] = face_points[in_idx[0]];
1025 int ia = points->
count - 1;
1037 inside[0] = face_points[in_idx[0]];
1038 int ia = points->
count - 1;
1047 inside[0] = face_points[in_idx[1]];
1048 size_t id = points->
count - 1;
1082 bool did_clip =
false;
1085 bool has_not_split =
true;
1086 bool is_out =
false;
1089 pts_a[0] = face_points[0];
1090 pts_a[1] = face_points[1];
1091 pts_a[2] = face_points[2];
1098 for(
int i=1; i < b_cnt-1; i++)
1105 if (area2 < 0.001f)
continue;
1109 pts[2] = pts_b[i+1];
1133 int did_cut =
false;
1134 bool cannot_cut =
false;
1140 if (new_verts.
count > 0 && new_faces.
count > 0)
1159 for (
int i = 0; i < new_verts.
count; i++)
1161 int index = i + points->
count;
1162 pts[index] = new_pts[i];
1182 for (
int i = 0; i < new_faces.
count; i++)
1184 int index = i + faces->
count;
1218 float denom = d00 * d11 - d01 * d01;
1220 float v = (d11 * d20 - d01 * d21) / denom;
1221 float w = (d00 * d21 - d01 * d20) / denom;
1222 float u = 1.0f - v - w;
1225 const float eps = 0.01f;
1250 t =
clamp(t, 0.0f, 1.0f);
1261 float t = (ap.
x * ab.
x + ap.
y * ab.
y) / (ab.
x * ab.
x + ab.
y * ab.
y);
1262 t = fmaxf(0.0f, fminf(1.0f, t));
1264 vec2 closest = { a.
x + t * ab.
x, a.
y + t * ab.
y };
1265 float dx = p.
x - closest.
x;
1266 float dy = p.
y - closest.
y;
1268 return sqrtf(dx*dx + dy*dy);
1280 if (dd > 0)
return (
vec3){999,999,999};
1282 float t = da/(da-db);
1303 if (ab_dist <= bc_dist && ab_dist <= ac_dist && ab_dist <= p_dist)
1307 if (bc_dist <= ab_dist && bc_dist <= ac_dist && bc_dist <= p_dist)
1311 if (ac_dist <= ab_dist && ac_dist <= bc_dist && ac_dist <= p_dist)
1316 if (p_dist <= ab_dist && p_dist <= bc_dist && p_dist <= ac_dist)
1336 if (d1 <= 0.0f && d2 <= 0.0f)
return a;
1342 if (d3 >= 0.0f && d4 <= d3)
return b;
1345 float vc = d1*d4 - d3*d2;
1346 if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f) {
1348 float v = d1 / (d1 - d3);
1357 if (d6 >= 0.0f && d5 <= d6)
return c;
1361 float vb = d5*d2 - d1*d6;
1362 if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f) {
1363 float w = d2 / (d2 - d6);
1367 float va = d3*d6 - d5*d4;
1368 if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f) {
1370 float w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
1375 float denom = 1.0f / (va + vb + vc);
1376 float v = vb * denom;
1377 float w = vc * denom;
1381#define DEGREES_TO_RAD (3.14159265358979323846 / 180.0)
1387 float projected_rad = radius * cosf(elevation_rad);
1391 -projected_rad * sinf(azimuth_rad),
1392 radius * sinf(elevation_rad),
1393 -projected_rad * cosf(azimuth_rad)
RayHit ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c)
vec3 generate_sphere_dir(int i, int N)
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)
float vec3_dot(vec3 a, vec3 b)
vec4 vec4_add(vec4 a, vec4 b)
vec3 vec3_normalize(vec3 v)
vec3 vec3_scale(vec3 a, float b)
vec3 vec3_mul(vec3 a, vec3 b)
vec4 vec4_mul(vec4 a, vec4 b)
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)
vec4 vec4_scale(vec4 a, float b)
float clamp(float a, float b, float c)
float vec3_length(vec3 v)
void clip_triangle(Vector *points, Vector *faces, vec3 planes[6], vec3 plane_points[6], vec3 a, vec3 b, vec3 c, vec3 normal)
AABB transform_aabb_by_model_matrix_rotation(AABB aabb, mat4 mat)
bool vec4_equal(vec4 a, vec4 b, float eps)
bool ccwv2(vec2 p, vec2 a, vec2 b)
void compute_convex_hull(vec3 *points, int count, vec3 *out_hull, int *out_count, vec3 normal)
vec3 point_triangle_distance(vec3 proposed, vec3 pts[3], vec3 normal)
bool perform_clip(Vector *points, Vector *faces, vec3 planes[6], vec3 plane_points[6], vec3 face_points[3], 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)
size_t leftmost_pointv2(vec2 *points, size_t count)
bool point_in_triangle(vec3 P, vec3 A, vec3 B, vec3 C)
size_t leftmost_pointi(vec3 *points, size_t *indices, size_t count)
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)
int vector_alloc(Vector *v, size_t i)
int leftmost_pointr(vec3 *points, int count)
void compute_aabb_from_points(vec3 *points, int point_count, AABB *aabb)
vec3 closest_point_segment(vec3 p, vec3 a, vec3 b)
void init_aabb(AABB *a, vec3 center, vec3 extents)
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)
int leftmost_point(vec3 *points, int count)
bool ccwr(vec3 p, vec3 a, vec3 b)
float triangle_area(vec3 a, vec3 b, vec3 c)
vec3 generate_bounce(uint32_t i, uint32_t N)
void free_vector(Vector *v)
AABB transform_aabb_by_position(AABB aabb, vec3 pos)
float point_to_segment_dist(vec2 p, vec2 a, vec2 b)
void calculate_aabb_bounds(AABB *aabb)
bool ccw(vec3 p, vec3 a, vec3 b)
int clip_tri_against_plane(Vector *points, Vector *faces, vec3 plane, vec3 plane_point, vec3 face_points[3], vec3 j_normal)
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)
vec3 segment_plane_distance(vec3 proposed, vec3 pts[3], vec3 normal)
bool mesh_faces_are_contiguous(MeshFace *a, vec3 *a_verts, MeshFace *b, vec3 *b_verts)
vec3 spherical_to_cartesian(float azimuth_deg, float elevation_deg, float radius)
uint16_t float32_to_float16(float f)
vec3 closest_point_triangle(vec3 p, vec3 a, vec3 b, vec3 c)
size_t leftmost_pointri(vec3 *points, size_t *indices, size_t count)
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 vec3_clamp(vec3 a, vec3 b, vec3 c)
#define CAST_VEC(vec, type)
vec3 mat4_mul_direction(const mat4 m, vec3 v)
vec3 mat4_mul_point(mat4 m, vec3 v)