Kohi Game Engine
kmath.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include "defines.h"
17 #include "math_types.h"
18 #include "memory/kmemory.h"
19 
21 #define K_PI 3.14159265358979323846f
22 
24 #define K_2PI (2.0f * K_PI)
25 
27 #define K_4PI (4.0f * K_PI)
28 
30 #define K_HALF_PI (0.5f * K_PI)
31 
33 #define K_QUARTER_PI (0.25f * K_PI)
34 
36 #define K_ONE_OVER_PI (1.0f / K_PI)
37 
39 #define K_ONE_OVER_TWO_PI (1.0f / K_2PI)
40 
42 #define K_SQRT_TWO 1.41421356237309504880f
43 
45 #define K_SQRT_THREE 1.73205080756887729352f
46 
48 #define K_SQRT_ONE_OVER_TWO 0.70710678118654752440f
49 
51 #define K_SQRT_ONE_OVER_THREE 0.57735026918962576450f
52 
54 #define K_DEG2RAD_MULTIPLIER (K_PI / 180.0f)
55 
57 #define K_RAD2DEG_MULTIPLIER (180.0f / K_PI)
58 
60 #define K_SEC_TO_US_MULTIPLIER (1000.0f * 1000.0f)
61 
63 #define K_SEC_TO_MS_MULTIPLIER 1000.0f
64 
66 #define K_MS_TO_SEC_MULTIPLIER 0.001f
67 
69 #define K_INFINITY (1e30f * 1e30f)
70 
72 #define K_FLOAT_EPSILON 1.192092896e-07f
73 
74 #define K_FLOAT_MIN -3.40282e+38F
75 
76 #define K_FLOAT_MAX 3.40282e+38F
77 
78 // ------------------------------------------
79 // General math functions
80 // ------------------------------------------
81 
87 KINLINE void kswapf(f32* a, f32* b) {
88  f32 temp = *a;
89  *a = *b;
90  *b = temp;
91 }
92 
93 #define KSWAP(type, a, b) \
94  { \
95  type temp = a; \
96  a = b; \
97  b = temp; \
98  }
99 
102  return x == 0.0f ? 0.0f : x < 0.0f ? -1.0f
103  : 1.0f;
104 }
105 
107 KINLINE f32 kstep(f32 edge, f32 x) {
108  return x < edge ? 0.0f : 1.0f;
109 }
110 
118 
126 
134 
142 
144 
146 
154 
162 
170 
178 
186 
194 
202 
204 
206  return a + t * (b - a);
207 }
208 
216  return (value != 0) && ((value & (value - 1)) == 0);
217 }
218 
225 
234 
240 
247 
257 
266 KINLINE f32 ksmoothstep(f32 edge_0, f32 edge_1, f32 x) {
267  f32 t = KCLAMP((x - edge_0) / (edge_1 - edge_0), 0.0f, 1.0f);
268  return t * t * (3.0 - 2.0 * t);
269 }
270 
280 
286  return kabs(f_0 - f_1) < K_FLOAT_EPSILON;
287 }
288 
289 // ------------------------------------------
290 // Vector 2
291 // ------------------------------------------
292 
301  vec2 out_vector;
302  out_vector.x = x;
303  out_vector.y = y;
304  return out_vector;
305 }
306 
311 KINLINE vec2 vec2_zero(void) { return (vec2){0.0f, 0.0f}; }
312 
317 KINLINE vec2 vec2_one(void) { return (vec2){1.0f, 1.0f}; }
318 
322 KINLINE vec2 vec2_up(void) { return (vec2){0.0f, 1.0f}; }
323 
327 KINLINE vec2 vec2_down(void) { return (vec2){0.0f, -1.0f}; }
328 
332 KINLINE vec2 vec2_left(void) { return (vec2){-1.0f, 0.0f}; }
333 
337 KINLINE vec2 vec2_right(void) { return (vec2){1.0f, 0.0f}; }
338 
346 KINLINE vec2 vec2_add(vec2 vector_0, vec2 vector_1) {
347  return (vec2){vector_0.x + vector_1.x, vector_0.y + vector_1.y};
348 }
349 
357 KINLINE vec2 vec2_sub(vec2 vector_0, vec2 vector_1) {
358  return (vec2){vector_0.x - vector_1.x, vector_0.y - vector_1.y};
359 }
360 
368 KINLINE vec2 vec2_mul(vec2 vector_0, vec2 vector_1) {
369  return (vec2){vector_0.x * vector_1.x, vector_0.y * vector_1.y};
370 }
371 
380 KINLINE vec2 vec2_mul_scalar(vec2 vector_0, f32 scalar) {
381  return (vec2){vector_0.x * scalar, vector_0.y * scalar};
382 }
383 
392 KINLINE vec2 vec2_mul_add(vec2 vector_0, vec2 vector_1, vec2 vector_2) {
393  return (vec2){
394  vector_0.x * vector_1.x + vector_2.x,
395  vector_0.y * vector_1.y + vector_2.y};
396 }
397 
405 KINLINE vec2 vec2_div(vec2 vector_0, vec2 vector_1) {
406  return (vec2){vector_0.x / vector_1.x, vector_0.y / vector_1.y};
407 }
408 
416  return vector.x * vector.x + vector.y * vector.y;
417 }
418 
426  return ksqrt(vec2_length_squared(vector));
427 }
428 
434 KINLINE void vec2_normalize(vec2* vector) {
435  const f32 length = vec2_length(*vector);
436  vector->x /= length;
437  vector->y /= length;
438 }
439 
447  vec2_normalize(&vector);
448  return vector;
449 }
450 
461 KINLINE b8 vec2_compare(vec2 vector_0, vec2 vector_1, f32 tolerance) {
462  if (kabs(vector_0.x - vector_1.x) > tolerance) {
463  return false;
464  }
465 
466  if (kabs(vector_0.y - vector_1.y) > tolerance) {
467  return false;
468  }
469 
470  return true;
471 }
472 
480 KINLINE f32 vec2_distance(vec2 vector_0, vec2 vector_1) {
481  vec2 d = (vec2){vector_0.x - vector_1.x, vector_0.y - vector_1.y};
482  return vec2_length(d);
483 }
484 
494  vec2 d = (vec2){vector_0.x - vector_1.x, vector_0.y - vector_1.y};
495  return vec2_length_squared(d);
496 }
497 
498 // ------------------------------------------
499 // Vector 3
500 // ------------------------------------------
501 
510 KINLINE vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){x, y, z}; }
511 
512 /*
513  * @brief Returns a new vec3 containing the x, y and z components of the
514  * supplied vec4, essentially dropping the w component.
515  *
516  * @param vector The 4-component vector to extract from.
517  * @return A new vec3
518  */
520  return (vec3){vector.x, vector.y, vector.z};
521 }
522 
523 /*
524  * @brief Returns a new vec3 containing the x and y components of the
525  * supplied vec2, with a z component specified.
526  *
527  * @param vector The 2-component vector to extract from.
528  * @param z The value to use for the z element.
529  * @return A new vec3
530  */
532  return (vec3){vector.x, vector.y, z};
533 }
534 
544  return (vec4){vector.x, vector.y, vector.z, w};
545 }
546 
551 KINLINE vec3 vec3_zero(void) { return (vec3){0.0f, 0.0f, 0.0f}; }
552 
557 KINLINE vec3 vec3_one(void) { return (vec3){1.0f, 1.0f, 1.0f}; }
558 
562 KINLINE vec3 vec3_up(void) { return (vec3){0.0f, 1.0f, 0.0f}; }
563 
567 KINLINE vec3 vec3_down(void) { return (vec3){0.0f, -1.0f, 0.0f}; }
568 
572 KINLINE vec3 vec3_left(void) { return (vec3){-1.0f, 0.0f, 0.0f}; }
573 
577 KINLINE vec3 vec3_right(void) { return (vec3){1.0f, 0.0f, 0.0f}; }
578 
582 KINLINE vec3 vec3_forward(void) { return (vec3){0.0f, 0.0f, -1.0f}; }
583 
587 KINLINE vec3 vec3_backward(void) { return (vec3){0.0f, 0.0f, 1.0f}; }
588 
596 KINLINE vec3 vec3_add(vec3 vector_0, vec3 vector_1) {
597  return (vec3){vector_0.x + vector_1.x, vector_0.y + vector_1.y,
598  vector_0.z + vector_1.z};
599 }
600 
608 KINLINE vec3 vec3_sub(vec3 vector_0, vec3 vector_1) {
609  return (vec3){vector_0.x - vector_1.x, vector_0.y - vector_1.y,
610  vector_0.z - vector_1.z};
611 }
612 
620 KINLINE vec3 vec3_mul(vec3 vector_0, vec3 vector_1) {
621  return (vec3){vector_0.x * vector_1.x, vector_0.y * vector_1.y,
622  vector_0.z * vector_1.z};
623 }
624 
633 KINLINE vec3 vec3_mul_scalar(vec3 vector_0, f32 scalar) {
634  return (vec3){vector_0.x * scalar, vector_0.y * scalar, vector_0.z * scalar};
635 }
636 
645 KINLINE vec3 vec3_mul_add(vec3 vector_0, vec3 vector_1, vec3 vector_2) {
646  return (vec3){
647  vector_0.x * vector_1.x + vector_2.x,
648  vector_0.y * vector_1.y + vector_2.y,
649  vector_0.z * vector_1.z + vector_2.z};
650 }
651 
659 KINLINE vec3 vec3_div(vec3 vector_0, vec3 vector_1) {
660  return (vec3){vector_0.x / vector_1.x, vector_0.y / vector_1.y,
661  vector_0.z / vector_1.z};
662 }
663 
664 KINLINE vec3 vec3_div_scalar(vec3 vector_0, f32 scalar) {
665  vec3 result;
666  for (u64 i = 0; i < 3; ++i) {
667  result.elements[i] = vector_0.elements[i] / scalar;
668  }
669  return result;
670 }
671 
679  return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z;
680 }
681 
689  return ksqrt(vec3_length_squared(vector));
690 }
691 
697 KINLINE void vec3_normalize(vec3* vector) {
698  const f32 length = vec3_length(*vector);
699  vector->x /= length;
700  vector->y /= length;
701  vector->z /= length;
702 }
703 
711  vec3_normalize(&vector);
712  return vector;
713 }
714 
723 KINLINE f32 vec3_dot(vec3 vector_0, vec3 vector_1) {
724  f32 p = 0;
725  p += vector_0.x * vector_1.x;
726  p += vector_0.y * vector_1.y;
727  p += vector_0.z * vector_1.z;
728  return p;
729 }
730 
740 KINLINE vec3 vec3_cross(vec3 vector_0, vec3 vector_1) {
741  return (vec3){vector_0.y * vector_1.z - vector_0.z * vector_1.y,
742  vector_0.z * vector_1.x - vector_0.x * vector_1.z,
743  vector_0.x * vector_1.y - vector_0.y * vector_1.x};
744 }
745 
756 KINLINE b8 vec3_compare(vec3 vector_0, vec3 vector_1, f32 tolerance) {
757  if (kabs(vector_0.x - vector_1.x) > tolerance) {
758  return false;
759  }
760 
761  if (kabs(vector_0.y - vector_1.y) > tolerance) {
762  return false;
763  }
764 
765  if (kabs(vector_0.z - vector_1.z) > tolerance) {
766  return false;
767  }
768 
769  return true;
770 }
771 
779 KINLINE f32 vec3_distance(vec3 vector_0, vec3 vector_1) {
780  vec3 d = (vec3){vector_0.x - vector_1.x, vector_0.y - vector_1.y,
781  vector_0.z - vector_1.z};
782  return vec3_length(d);
783 }
784 
794  vec3 d = (vec3){vector_0.x - vector_1.x, vector_0.y - vector_1.y,
795  vector_0.z - vector_1.z};
796  return vec3_length_squared(d);
797 }
798 
807  f32 length_sq = vec3_length_squared(v_1);
808  if (length_sq == 0.0f) {
809  // NOTE: handle divide-by-zero case (i.e. v_1 is a zero vector).
810  return vec3_zero();
811  }
812  f32 scalar = vec3_dot(v_0, v_1) / length_sq;
813  return vec3_mul_scalar(v_1, scalar);
814 }
815 
824  vec3 out;
825  out.x = v.x * m.data[0 + 0] + v.y * m.data[4 + 0] + v.z * m.data[8 + 0] + w * m.data[12 + 0];
826  out.y = v.x * m.data[0 + 1] + v.y * m.data[4 + 1] + v.z * m.data[8 + 1] + w * m.data[12 + 1];
827  out.z = v.x * m.data[0 + 2] + v.y * m.data[4 + 2] + v.z * m.data[8 + 2] + w * m.data[12 + 2];
828  return out;
829 }
830 
831 // ------------------------------------------
832 // Vector 4
833 // ------------------------------------------
834 
845  vec4 out_vector;
846 #if defined(KUSE_SIMD)
847  out_vector.data = _mm_setr_ps(x, y, z, w);
848 #else
849  out_vector.x = x;
850  out_vector.y = y;
851  out_vector.z = z;
852  out_vector.w = w;
853 #endif
854  return out_vector;
855 }
856 
865  return (vec3){vector.x, vector.y, vector.z};
866 }
867 
877 #if defined(KUSE_SIMD)
878  vec4 out_vector;
879  out_vector.data = _mm_setr_ps(x, y, z, w);
880  return out_vector;
881 #else
882  return (vec4){vector.x, vector.y, vector.z, w};
883 #endif
884 }
885 
890 KINLINE vec4 vec4_zero(void) { return (vec4){0.0f, 0.0f, 0.0f, 0.0f}; }
891 
896 KINLINE vec4 vec4_one(void) { return (vec4){1.0f, 1.0f, 1.0f, 1.0f}; }
897 
905 KINLINE vec4 vec4_add(vec4 vector_0, vec4 vector_1) {
906  vec4 result;
907  for (u64 i = 0; i < 4; ++i) {
908  result.elements[i] = vector_0.elements[i] + vector_1.elements[i];
909  }
910  return result;
911 }
912 
920 KINLINE vec4 vec4_sub(vec4 vector_0, vec4 vector_1) {
921  vec4 result;
922  for (u64 i = 0; i < 4; ++i) {
923  result.elements[i] = vector_0.elements[i] - vector_1.elements[i];
924  }
925  return result;
926 }
927 
935 KINLINE vec4 vec4_mul(vec4 vector_0, vec4 vector_1) {
936  vec4 result;
937  for (u64 i = 0; i < 4; ++i) {
938  result.elements[i] = vector_0.elements[i] * vector_1.elements[i];
939  }
940  return result;
941 }
942 
951 KINLINE vec4 vec4_mul_scalar(vec4 vector_0, f32 scalar) {
952  return (vec4){vector_0.x * scalar, vector_0.y * scalar, vector_0.z * scalar, vector_0.w * scalar};
953 }
954 
963 KINLINE vec4 vec4_mul_add(vec4 vector_0, vec4 vector_1, vec4 vector_2) {
964  return (vec4){
965  vector_0.x * vector_1.x + vector_2.x,
966  vector_0.y * vector_1.y + vector_2.y,
967  vector_0.z * vector_1.z + vector_2.z,
968  vector_0.w * vector_1.w + vector_2.w,
969  };
970 }
971 
979 KINLINE vec4 vec4_div(vec4 vector_0, vec4 vector_1) {
980  vec4 result;
981  for (u64 i = 0; i < 4; ++i) {
982  result.elements[i] = vector_0.elements[i] / vector_1.elements[i];
983  }
984  return result;
985 }
986 
987 KINLINE vec4 vec4_div_scalar(vec4 vector_0, f32 scalar) {
988  vec4 result;
989  for (u64 i = 0; i < 4; ++i) {
990  result.elements[i] = vector_0.elements[i] / scalar;
991  }
992  return result;
993 }
994 
1002  return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z +
1003  vector.w * vector.w;
1004 }
1005 
1013  return ksqrt(vec4_length_squared(vector));
1014 }
1015 
1021 KINLINE void vec4_normalize(vec4* vector) {
1022  const f32 length = vec4_length(*vector);
1023  vector->x /= length;
1024  vector->y /= length;
1025  vector->z /= length;
1026  vector->w /= length;
1027 }
1028 
1036  vec4_normalize(&vector);
1037  return vector;
1038 }
1039 
1054 KINLINE f32 vec4_dot_f32(f32 a0, f32 a1, f32 a2, f32 a3, f32 b0, f32 b1, f32 b2,
1055  f32 b3) {
1056  f32 p;
1057  p = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
1058  return p;
1059 }
1060 
1071 KINLINE b8 vec4_compare(vec4 vector_0, vec4 vector_1, f32 tolerance) {
1072  if (kabs(vector_0.x - vector_1.x) > tolerance) {
1073  return false;
1074  }
1075 
1076  if (kabs(vector_0.y - vector_1.y) > tolerance) {
1077  return false;
1078  }
1079 
1080  if (kabs(vector_0.z - vector_1.z) > tolerance) {
1081  return false;
1082  }
1083 
1084  if (kabs(vector_0.w - vector_1.w) > tolerance) {
1085  return false;
1086  }
1087 
1088  return true;
1089 }
1090 
1098 KINLINE void vec4_clamp(vec4* vector, f32 min, f32 max) {
1099  if (vector) {
1100  for (u8 i = 0; i < 4; ++i) {
1101  vector->elements[i] = KCLAMP(vector->elements[i], min, max);
1102  }
1103  }
1104 }
1105 
1114 KINLINE vec4 vec4_clamped(vec4 vector, f32 min, f32 max) {
1115  vec4_clamp(&vector, min, max);
1116  return vector;
1117 }
1118 
1132  mat4 out_matrix;
1133  kzero_memory(out_matrix.data, sizeof(f32) * 16);
1134  out_matrix.data[0] = 1.0f;
1135  out_matrix.data[5] = 1.0f;
1136  out_matrix.data[10] = 1.0f;
1137  out_matrix.data[15] = 1.0f;
1138  return out_matrix;
1139 }
1140 
1148 KINLINE mat4 mat4_mul(mat4 matrix_0, mat4 matrix_1) {
1149  mat4 out_matrix = mat4_identity();
1150 
1151  const f32* m1_ptr = matrix_0.data;
1152  const f32* m2_ptr = matrix_1.data;
1153  f32* dst_ptr = out_matrix.data;
1154 
1155  for (i32 i = 0; i < 4; ++i) {
1156  for (i32 j = 0; j < 4; ++j) {
1157  *dst_ptr = m1_ptr[0] * m2_ptr[0 + j] + m1_ptr[1] * m2_ptr[4 + j] +
1158  m1_ptr[2] * m2_ptr[8 + j] + m1_ptr[3] * m2_ptr[12 + j];
1159  dst_ptr++;
1160  }
1161  m1_ptr += 4;
1162  }
1163  return out_matrix;
1164 }
1165 
1178 KINLINE mat4 mat4_orthographic(f32 left, f32 right, f32 bottom, f32 top,
1179  f32 near_clip, f32 far_clip) {
1180  mat4 out_matrix = mat4_identity();
1181 
1182  f32 lr = 1.0f / (left - right);
1183  f32 bt = 1.0f / (bottom - top);
1184  f32 nf = 1.0f / (near_clip - far_clip);
1185 
1186  out_matrix.data[0] = -2.0f * lr;
1187  out_matrix.data[5] = -2.0f * bt;
1188  out_matrix.data[10] = nf;
1189 
1190  out_matrix.data[12] = (left + right) * lr;
1191  out_matrix.data[13] = (top + bottom) * bt;
1192  out_matrix.data[14] = -near_clip * nf;
1193 
1194  return out_matrix;
1195 }
1196 
1207 KINLINE mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip, f32 far_clip) {
1208  f32 half_tan_fov = ktan(fov_radians * 0.5f);
1209  mat4 out_matrix;
1210  kzero_memory(out_matrix.data, sizeof(f32) * 16);
1211  out_matrix.data[0] = 1.0f / (aspect_ratio * half_tan_fov);
1212  out_matrix.data[5] = 1.0f / half_tan_fov;
1213  out_matrix.data[10] = far_clip / (near_clip - far_clip);
1214  out_matrix.data[11] = -1.0f;
1215  out_matrix.data[14] = (far_clip * near_clip) / (near_clip - far_clip);
1216  return out_matrix;
1217 }
1218 
1228 KINLINE mat4 mat4_look_at(vec3 position, vec3 target, vec3 up) {
1229  mat4 out_matrix;
1230  vec3 z_axis = vec3_normalized(vec3_sub(target, position));
1231  vec3 x_axis = vec3_normalized(vec3_cross(z_axis, up));
1232  vec3 y_axis = vec3_cross(x_axis, z_axis);
1233 
1234  out_matrix.data[0] = x_axis.x;
1235  out_matrix.data[1] = y_axis.x;
1236  out_matrix.data[2] = -z_axis.x;
1237  out_matrix.data[3] = 0;
1238  out_matrix.data[4] = x_axis.y;
1239  out_matrix.data[5] = y_axis.y;
1240  out_matrix.data[6] = -z_axis.y;
1241  out_matrix.data[7] = 0;
1242  out_matrix.data[8] = x_axis.z;
1243  out_matrix.data[9] = y_axis.z;
1244  out_matrix.data[10] = -z_axis.z;
1245  out_matrix.data[11] = 0;
1246  out_matrix.data[12] = -vec3_dot(x_axis, position);
1247  out_matrix.data[13] = -vec3_dot(y_axis, position);
1248  out_matrix.data[14] = vec3_dot(z_axis, position);
1249  out_matrix.data[15] = 1.0f;
1250  return out_matrix;
1251 }
1252 
1260  mat4 out_matrix;
1261  out_matrix.data[0] = matrix.data[0];
1262  out_matrix.data[1] = matrix.data[4];
1263  out_matrix.data[2] = matrix.data[8];
1264  out_matrix.data[3] = matrix.data[12];
1265  out_matrix.data[4] = matrix.data[1];
1266  out_matrix.data[5] = matrix.data[5];
1267  out_matrix.data[6] = matrix.data[9];
1268  out_matrix.data[7] = matrix.data[13];
1269  out_matrix.data[8] = matrix.data[2];
1270  out_matrix.data[9] = matrix.data[6];
1271  out_matrix.data[10] = matrix.data[10];
1272  out_matrix.data[11] = matrix.data[14];
1273  out_matrix.data[12] = matrix.data[3];
1274  out_matrix.data[13] = matrix.data[7];
1275  out_matrix.data[14] = matrix.data[11];
1276  out_matrix.data[15] = matrix.data[15];
1277  return out_matrix;
1278 }
1279 
1287  const f32* m = matrix.data;
1288 
1289  f32 t0 = m[10] * m[15];
1290  f32 t1 = m[14] * m[11];
1291  f32 t2 = m[6] * m[15];
1292  f32 t3 = m[14] * m[7];
1293  f32 t4 = m[6] * m[11];
1294  f32 t5 = m[10] * m[7];
1295  f32 t6 = m[2] * m[15];
1296  f32 t7 = m[14] * m[3];
1297  f32 t8 = m[2] * m[11];
1298  f32 t9 = m[10] * m[3];
1299  f32 t10 = m[2] * m[7];
1300  f32 t11 = m[6] * m[3];
1301 
1302  mat3 temp_mat;
1303  f32* o = temp_mat.data;
1304 
1305  o[0] = (t0 * m[5] + t3 * m[9] + t4 * m[13]) -
1306  (t1 * m[5] + t2 * m[9] + t5 * m[13]);
1307  o[1] = (t1 * m[1] + t6 * m[9] + t9 * m[13]) -
1308  (t0 * m[1] + t7 * m[9] + t8 * m[13]);
1309  o[2] = (t2 * m[1] + t7 * m[5] + t10 * m[13]) -
1310  (t3 * m[1] + t6 * m[5] + t11 * m[13]);
1311  o[3] = (t5 * m[1] + t8 * m[5] + t11 * m[9]) -
1312  (t4 * m[1] + t9 * m[5] + t10 * m[9]);
1313 
1314  f32 determinant = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * o[3]);
1315  return determinant;
1316 }
1317 
1325  const f32* m = matrix.data;
1326 
1327  f32 t0 = m[10] * m[15];
1328  f32 t1 = m[14] * m[11];
1329  f32 t2 = m[6] * m[15];
1330  f32 t3 = m[14] * m[7];
1331  f32 t4 = m[6] * m[11];
1332  f32 t5 = m[10] * m[7];
1333  f32 t6 = m[2] * m[15];
1334  f32 t7 = m[14] * m[3];
1335  f32 t8 = m[2] * m[11];
1336  f32 t9 = m[10] * m[3];
1337  f32 t10 = m[2] * m[7];
1338  f32 t11 = m[6] * m[3];
1339  f32 t12 = m[8] * m[13];
1340  f32 t13 = m[12] * m[9];
1341  f32 t14 = m[4] * m[13];
1342  f32 t15 = m[12] * m[5];
1343  f32 t16 = m[4] * m[9];
1344  f32 t17 = m[8] * m[5];
1345  f32 t18 = m[0] * m[13];
1346  f32 t19 = m[12] * m[1];
1347  f32 t20 = m[0] * m[9];
1348  f32 t21 = m[8] * m[1];
1349  f32 t22 = m[0] * m[5];
1350  f32 t23 = m[4] * m[1];
1351 
1352  mat4 out_matrix;
1353  f32* o = out_matrix.data;
1354 
1355  o[0] = (t0 * m[5] + t3 * m[9] + t4 * m[13]) -
1356  (t1 * m[5] + t2 * m[9] + t5 * m[13]);
1357  o[1] = (t1 * m[1] + t6 * m[9] + t9 * m[13]) -
1358  (t0 * m[1] + t7 * m[9] + t8 * m[13]);
1359  o[2] = (t2 * m[1] + t7 * m[5] + t10 * m[13]) -
1360  (t3 * m[1] + t6 * m[5] + t11 * m[13]);
1361  o[3] = (t5 * m[1] + t8 * m[5] + t11 * m[9]) -
1362  (t4 * m[1] + t9 * m[5] + t10 * m[9]);
1363 
1364  f32 d = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * o[3]);
1365 
1366  // Check for singular matrix (determinant near zero)
1367  if (kabs(d) < 1e-6f) {
1368  // Return identity matrix if the determinant is close to zero (singular matrix)
1369  return mat4_identity();
1370  }
1371 
1372  o[0] = d * o[0];
1373  o[1] = d * o[1];
1374  o[2] = d * o[2];
1375  o[3] = d * o[3];
1376  o[4] = d * ((t1 * m[4] + t2 * m[8] + t5 * m[12]) -
1377  (t0 * m[4] + t3 * m[8] + t4 * m[12]));
1378  o[5] = d * ((t0 * m[0] + t7 * m[8] + t8 * m[12]) -
1379  (t1 * m[0] + t6 * m[8] + t9 * m[12]));
1380  o[6] = d * ((t3 * m[0] + t6 * m[4] + t11 * m[12]) -
1381  (t2 * m[0] + t7 * m[4] + t10 * m[12]));
1382  o[7] = d * ((t4 * m[0] + t9 * m[4] + t10 * m[8]) -
1383  (t5 * m[0] + t8 * m[4] + t11 * m[8]));
1384  o[8] = d * ((t12 * m[7] + t15 * m[11] + t16 * m[15]) -
1385  (t13 * m[7] + t14 * m[11] + t17 * m[15]));
1386  o[9] = d * ((t13 * m[3] + t18 * m[11] + t21 * m[15]) -
1387  (t12 * m[3] + t19 * m[11] + t20 * m[15]));
1388  o[10] = d * ((t14 * m[3] + t19 * m[7] + t22 * m[15]) -
1389  (t15 * m[3] + t18 * m[7] + t23 * m[15]));
1390  o[11] = d * ((t17 * m[3] + t20 * m[7] + t23 * m[11]) -
1391  (t16 * m[3] + t21 * m[7] + t22 * m[11]));
1392  o[12] = d * ((t14 * m[10] + t17 * m[14] + t13 * m[6]) -
1393  (t16 * m[14] + t12 * m[6] + t15 * m[10]));
1394  o[13] = d * ((t20 * m[14] + t12 * m[2] + t19 * m[10]) -
1395  (t18 * m[10] + t21 * m[14] + t13 * m[2]));
1396  o[14] = d * ((t18 * m[6] + t23 * m[14] + t15 * m[2]) -
1397  (t22 * m[14] + t14 * m[2] + t19 * m[6]));
1398  o[15] = d * ((t22 * m[10] + t16 * m[2] + t21 * m[6]) -
1399  (t20 * m[6] + t23 * m[10] + t17 * m[2]));
1400 
1401  return out_matrix;
1402 }
1403 
1411  mat4 out_matrix = mat4_identity();
1412  out_matrix.data[12] = position.x;
1413  out_matrix.data[13] = position.y;
1414  out_matrix.data[14] = position.z;
1415  return out_matrix;
1416 }
1417 
1425  mat4 out_matrix = mat4_identity();
1426  out_matrix.data[0] = scale.x;
1427  out_matrix.data[5] = scale.y;
1428  out_matrix.data[10] = scale.z;
1429  return out_matrix;
1430 }
1431 
1441  mat4 out_matrix;
1442 
1443  out_matrix.data[0] = (1.0f - 2.0f * (r.y * r.y + r.z * r.z)) * s.x;
1444  out_matrix.data[1] = (r.x * r.y + r.z * r.w) * s.x * 2.0f;
1445  out_matrix.data[2] = (r.x * r.z - r.y * r.w) * s.x * 2.0f;
1446  out_matrix.data[3] = 0.0f;
1447  out_matrix.data[4] = (r.x * r.y - r.z * r.w) * s.y * 2.0f;
1448  out_matrix.data[5] = (1.0f - 2.0f * (r.x * r.x + r.z * r.z)) * s.y;
1449  out_matrix.data[6] = (r.y * r.z + r.x * r.w) * s.y * 2.0f;
1450  out_matrix.data[7] = 0.0f;
1451  out_matrix.data[8] = (r.x * r.z + r.y * r.w) * s.z * 2.0f;
1452  out_matrix.data[9] = (r.y * r.z - r.x * r.w) * s.z * 2.0f;
1453  out_matrix.data[10] = (1.0f - 2.0f * (r.x * r.x + r.y * r.y)) * s.z;
1454  out_matrix.data[11] = 0.0f;
1455  out_matrix.data[12] = t.x;
1456  out_matrix.data[13] = t.y;
1457  out_matrix.data[14] = t.z;
1458  out_matrix.data[15] = 1.0f;
1459 
1460  return out_matrix;
1461 }
1462 
1469 KINLINE mat4 mat4_euler_x(f32 angle_radians) {
1470  mat4 out_matrix = mat4_identity();
1471  f32 c = kcos(angle_radians);
1472  f32 s = ksin(angle_radians);
1473 
1474  out_matrix.data[5] = c;
1475  out_matrix.data[6] = s;
1476  out_matrix.data[9] = -s;
1477  out_matrix.data[10] = c;
1478  return out_matrix;
1479 }
1480 
1487 KINLINE mat4 mat4_euler_y(f32 angle_radians) {
1488  mat4 out_matrix = mat4_identity();
1489  f32 c = kcos(angle_radians);
1490  f32 s = ksin(angle_radians);
1491 
1492  out_matrix.data[0] = c;
1493  out_matrix.data[2] = -s;
1494  out_matrix.data[8] = s;
1495  out_matrix.data[10] = c;
1496  return out_matrix;
1497 }
1498 
1505 KINLINE mat4 mat4_euler_z(f32 angle_radians) {
1506  mat4 out_matrix = mat4_identity();
1507 
1508  f32 c = kcos(angle_radians);
1509  f32 s = ksin(angle_radians);
1510 
1511  out_matrix.data[0] = c;
1512  out_matrix.data[1] = s;
1513  out_matrix.data[4] = -s;
1514  out_matrix.data[5] = c;
1515  return out_matrix;
1516 }
1517 
1526 KINLINE mat4 mat4_euler_xyz(f32 x_radians, f32 y_radians, f32 z_radians) {
1527  mat4 rx = mat4_euler_x(x_radians);
1528  mat4 ry = mat4_euler_y(y_radians);
1529  mat4 rz = mat4_euler_z(z_radians);
1530  mat4 out_matrix = mat4_mul(rx, ry);
1531  out_matrix = mat4_mul(out_matrix, rz);
1532  return out_matrix;
1533 }
1534 
1542  vec3 forward;
1543  forward.x = -matrix.data[8];
1544  forward.y = -matrix.data[9];
1545  forward.z = -matrix.data[10];
1546  vec3_normalize(&forward);
1547  return forward;
1548 }
1549 
1557  vec3 backward;
1558  backward.x = matrix.data[8];
1559  backward.y = matrix.data[9];
1560  backward.z = matrix.data[10];
1561  vec3_normalize(&backward);
1562  return backward;
1563 }
1564 
1572  vec3 up;
1573  up.x = matrix.data[1];
1574  up.y = matrix.data[5];
1575  up.z = matrix.data[9];
1576  vec3_normalize(&up);
1577  return up;
1578 }
1579 
1587  vec3 down;
1588  down.x = -matrix.data[1];
1589  down.y = -matrix.data[5];
1590  down.z = -matrix.data[9];
1591  vec3_normalize(&down);
1592  return down;
1593 }
1594 
1602  vec3 left;
1603  left.x = -matrix.data[0];
1604  left.y = -matrix.data[1];
1605  left.z = -matrix.data[2];
1606  vec3_normalize(&left);
1607  return left;
1608 }
1609 
1617  vec3 right;
1618  right.x = matrix.data[0];
1619  right.y = matrix.data[1];
1620  right.z = matrix.data[2];
1621  vec3_normalize(&right);
1622  return right;
1623 }
1624 
1632  vec3 pos;
1633  pos.x = matrix.data[12];
1634  pos.y = matrix.data[13];
1635  pos.z = matrix.data[14];
1636  return pos;
1637 }
1638 
1647  return (vec3){
1648  v.x * m.data[0] + v.y * m.data[4] + v.z * m.data[8] + m.data[12],
1649  v.x * m.data[1] + v.y * m.data[5] + v.z * m.data[9] + m.data[13],
1650  v.x * m.data[2] + v.y * m.data[6] + v.z * m.data[10] + m.data[14]};
1651 }
1652 
1661  return (vec3){
1662  v.x * m.data[0] + v.y * m.data[4] + v.z * m.data[8] + m.data[12],
1663  v.x * m.data[1] + v.y * m.data[5] + v.z * m.data[9] + m.data[13],
1664  v.x * m.data[2] + v.y * m.data[6] + v.z * m.data[10] + m.data[14]};
1665 }
1666 
1675  return (vec4){
1676  v.x * m.data[0] + v.y * m.data[1] + v.z * m.data[2] + v.w * m.data[3],
1677  v.x * m.data[4] + v.y * m.data[5] + v.z * m.data[6] + v.w * m.data[7],
1678  v.x * m.data[8] + v.y * m.data[9] + v.z * m.data[10] + v.w * m.data[11],
1679  v.x * m.data[12] + v.y * m.data[13] + v.z * m.data[14] + v.w * m.data[15]};
1680 }
1681 
1690  return (vec4){
1691  v.x * m.data[0] + v.y * m.data[4] + v.z * m.data[8] + v.w * m.data[12],
1692  v.x * m.data[1] + v.y * m.data[5] + v.z * m.data[9] + v.w * m.data[13],
1693  v.x * m.data[2] + v.y * m.data[6] + v.z * m.data[10] + v.w * m.data[14],
1694  v.x * m.data[3] + v.y * m.data[7] + v.z * m.data[11] + v.w * m.data[15]};
1695 }
1696 
1697 // ------------------------------------------
1698 // Quaternion
1699 // ------------------------------------------
1700 
1706 KINLINE quat quat_identity(void) { return (quat){0, 0, 0, 1.0f}; }
1707 
1715  return ksqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
1716 }
1717 
1725  f32 normal = quat_normal(q);
1726  return (quat){q.x / normal, q.y / normal, q.z / normal, q.w / normal};
1727 }
1728 
1736 KINLINE quat quat_conjugate(quat q) { return (quat){-q.x, -q.y, -q.z, q.w}; }
1737 
1745 
1754  quat out_quaternion;
1755 
1756  out_quaternion.x =
1757  q_0.x * q_1.w + q_0.y * q_1.z - q_0.z * q_1.y + q_0.w * q_1.x;
1758 
1759  out_quaternion.y =
1760  -q_0.x * q_1.z + q_0.y * q_1.w + q_0.z * q_1.x + q_0.w * q_1.y;
1761 
1762  out_quaternion.z =
1763  q_0.x * q_1.y - q_0.y * q_1.x + q_0.z * q_1.w + q_0.w * q_1.z;
1764 
1765  out_quaternion.w =
1766  -q_0.x * q_1.x - q_0.y * q_1.y - q_0.z * q_1.z + q_0.w * q_1.w;
1767 
1768  return out_quaternion;
1769 }
1770 
1779  return q_0.x * q_1.x + q_0.y * q_1.y + q_0.z * q_1.z + q_0.w * q_1.w;
1780 }
1781 
1782 KINLINE vec3 vec3_min(vec3 vector_0, vec3 vector_1) {
1783  return vec3_create(
1784  KMIN(vector_0.x, vector_1.y),
1785  KMIN(vector_0.y, vector_1.y),
1786  KMIN(vector_0.z, vector_1.z));
1787 }
1788 
1789 KINLINE vec3 vec3_max(vec3 vector_0, vec3 vector_1) {
1790  return vec3_create(
1791  KMAX(vector_0.x, vector_1.y),
1792  KMAX(vector_0.y, vector_1.y),
1793  KMAX(vector_0.z, vector_1.z));
1794 }
1795 
1797  return vec3_create(ksign(v.x), ksign(v.y), ksign(v.z));
1798 }
1799 
1801  vec3 u = vec3_create(q.x, q.y, q.z);
1802  f32 s = q.w;
1803 
1804  return vec3_add(
1805  vec3_add(
1806  vec3_mul_scalar(u, 2.0f * vec3_dot(u, v)),
1807  vec3_mul_scalar(v, (s * s - vec3_dot(u, u)))),
1808  vec3_mul_scalar(vec3_cross(u, v), 2.0f * s));
1809 }
1810 
1818  mat4 out_matrix = mat4_identity();
1819 
1820  // https://stackoverflow.com/questions/1556260/convert-quaternion-rotation-to-rotation-matrix
1821 
1822  quat n = quat_normalize(q);
1823 
1824  out_matrix.data[0] = 1.0f - 2.0f * n.y * n.y - 2.0f * n.z * n.z;
1825  out_matrix.data[1] = 2.0f * n.x * n.y - 2.0f * n.z * n.w;
1826  out_matrix.data[2] = 2.0f * n.x * n.z + 2.0f * n.y * n.w;
1827 
1828  out_matrix.data[4] = 2.0f * n.x * n.y + 2.0f * n.z * n.w;
1829  out_matrix.data[5] = 1.0f - 2.0f * n.x * n.x - 2.0f * n.z * n.z;
1830  out_matrix.data[6] = 2.0f * n.y * n.z - 2.0f * n.x * n.w;
1831 
1832  out_matrix.data[8] = 2.0f * n.x * n.z - 2.0f * n.y * n.w;
1833  out_matrix.data[9] = 2.0f * n.y * n.z + 2.0f * n.x * n.w;
1834  out_matrix.data[10] = 1.0f - 2.0f * n.x * n.x - 2.0f * n.y * n.y;
1835 
1836  return out_matrix;
1837 }
1838 
1848  mat4 out_matrix;
1849 
1850  f32* o = out_matrix.data;
1851  o[0] = (q.x * q.x) - (q.y * q.y) - (q.z * q.z) + (q.w * q.w);
1852  o[1] = 2.0f * ((q.x * q.y) + (q.z * q.w));
1853  o[2] = 2.0f * ((q.x * q.z) - (q.y * q.w));
1854  o[3] = center.x - center.x * o[0] - center.y * o[1] - center.z * o[2];
1855 
1856  o[4] = 2.0f * ((q.x * q.y) - (q.z * q.w));
1857  o[5] = -(q.x * q.x) + (q.y * q.y) - (q.z * q.z) + (q.w * q.w);
1858  o[6] = 2.0f * ((q.y * q.z) + (q.x * q.w));
1859  o[7] = center.y - center.x * o[4] - center.y * o[5] - center.z * o[6];
1860 
1861  o[8] = 2.0f * ((q.x * q.z) + (q.y * q.w));
1862  o[9] = 2.0f * ((q.y * q.z) - (q.x * q.w));
1863  o[10] = -(q.x * q.x) - (q.y * q.y) + (q.z * q.z) + (q.w * q.w);
1864  o[11] = center.z - center.x * o[8] - center.y * o[9] - center.z * o[10];
1865 
1866  o[12] = 0.0f;
1867  o[13] = 0.0f;
1868  o[14] = 0.0f;
1869  o[15] = 1.0f;
1870  return out_matrix;
1871 }
1872 
1881 KINLINE quat quat_from_axis_angle(vec3 axis, f32 angle, b8 normalize) {
1882  const f32 half_angle = 0.5f * angle;
1883  f32 s = ksin(half_angle);
1884  f32 c = kcos(half_angle);
1885 
1886  quat q = (quat){s * axis.x, s * axis.y, s * axis.z, c};
1887  if (normalize) {
1888  return quat_normalize(q);
1889  }
1890  return q;
1891 }
1892 
1903 KINLINE quat quat_slerp(quat q_0, quat q_1, f32 percentage) {
1904  quat out_quaternion;
1905  // Source: https://en.wikipedia.org/wiki/Slerp
1906  // Only unit quaternions are valid rotations.
1907  // Normalize to avoid undefined behavior.
1908  quat v0 = quat_normalize(q_0);
1909  quat v1 = quat_normalize(q_1);
1910 
1911  // Compute the cosine of the angle between the two vectors.
1912  f32 dot = quat_dot(v0, v1);
1913 
1914  // If the dot product is negative, slerp won't take
1915  // the shorter path. Note that v1 and -v1 are equivalent when
1916  // the negation is applied to all four components. Fix by
1917  // reversing one quaternion.
1918  if (dot < 0.0f) {
1919  v1.x = -v1.x;
1920  v1.y = -v1.y;
1921  v1.z = -v1.z;
1922  v1.w = -v1.w;
1923  dot = -dot;
1924  }
1925 
1926  const f32 DOT_THRESHOLD = 0.9995f;
1927  if (dot > DOT_THRESHOLD) {
1928  // If the inputs are too close for comfort, linearly interpolate
1929  // and normalize the result.
1930  out_quaternion = (quat){v0.x + ((v1.x - v0.x) * percentage),
1931  v0.y + ((v1.y - v0.y) * percentage),
1932  v0.z + ((v1.z - v0.z) * percentage),
1933  v0.w + ((v1.w - v0.w) * percentage)};
1934 
1935  return quat_normalize(out_quaternion);
1936  }
1937 
1938  // Since dot is in range [0, DOT_THRESHOLD], acos is safe
1939  f32 theta_0 = kacos(dot); // theta_0 = angle between input vectors
1940  f32 theta = theta_0 * percentage; // theta = angle between v0 and result
1941  f32 sin_theta = ksin(theta); // compute this value only once
1942  f32 sin_theta_0 = ksin(theta_0); // compute this value only once
1943 
1944  f32 s0 =
1945  kcos(theta) -
1946  dot * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0)
1947  f32 s1 = sin_theta / sin_theta_0;
1948 
1949  return (quat){(v0.x * s0) + (v1.x * s1), (v0.y * s0) + (v1.y * s1),
1950  (v0.z * s0) + (v1.z * s1), (v0.w * s0) + (v1.w * s1)};
1951 }
1952 
1959 KINLINE f32 deg_to_rad(f32 degrees) { return degrees * K_DEG2RAD_MULTIPLIER; }
1960 
1967 KINLINE f32 rad_to_deg(f32 radians) { return radians * K_RAD2DEG_MULTIPLIER; }
1968 
1979 KINLINE f32 range_convert_f32(f32 value, f32 old_min, f32 old_max, f32 new_min,
1980  f32 new_max) {
1981  return (((value - old_min) * (new_max - new_min)) / (old_max - old_min)) +
1982  new_min;
1983 }
1984 
1993 KINLINE void rgbu_to_u32(u32 r, u32 g, u32 b, u32* out_u32) {
1994  *out_u32 = (((r & 0x0FF) << 16) | ((g & 0x0FF) << 8) | (b & 0x0FF));
1995 }
1996 
2005 KINLINE void u32_to_rgb(u32 rgbu, u32* out_r, u32* out_g, u32* out_b) {
2006  *out_r = (rgbu >> 16) & 0x0FF;
2007  *out_g = (rgbu >> 8) & 0x0FF;
2008  *out_b = (rgbu) & 0x0FF;
2009 }
2010 
2020 KINLINE void rgb_u32_to_vec3(u32 r, u32 g, u32 b, vec3* out_v) {
2021  out_v->r = r / 255.0f;
2022  out_v->g = g / 255.0f;
2023  out_v->b = b / 255.0f;
2024 }
2025 
2034 KINLINE void vec3_to_rgb_u32(vec3 v, u32* out_r, u32* out_g, u32* out_b) {
2035  *out_r = v.r * 255;
2036  *out_g = v.g * 255;
2037  *out_b = v.b * 255;
2038 }
2039 
2041 
2057 KAPI frustum frustum_create(const vec3* position, const vec3* target, const vec3* up, f32 aspect, f32 fov, f32 near, f32 far);
2058 
2060 
2068 KAPI void frustum_corner_points_world_space(mat4 projection_view, vec4* corners);
2069 
2078 KAPI f32 plane_signed_distance(const plane_3d* p, const vec3* position);
2079 
2090 KAPI b8 plane_intersects_sphere(const plane_3d* p, const vec3* center,
2091  f32 radius);
2092 
2102 KAPI b8 frustum_intersects_sphere(const frustum* f, const vec3* center, f32 radius);
2103 
2113 
2125 KAPI b8 plane_intersects_aabb(const plane_3d* p, const vec3* center, const vec3* extents);
2126 
2138 KAPI b8 frustum_intersects_aabb(const frustum* f, const vec3* center,
2139  const vec3* extents);
2140 
2142  return (point.x >= rect.x && point.x <= rect.x + rect.width) && (point.y >= rect.y && point.y <= rect.y + rect.height);
2143 }
2144 
2145 KAPI f32 vec3_distance_to_line(vec3 point, vec3 line_start, vec3 line_direction);
2146 
2148  return (vec3){
2149  (extents.min.x + extents.max.x) * 0.5f,
2150  (extents.min.y + extents.max.y) * 0.5f,
2151  };
2152 }
2153 
2155  return (vec3){
2156  kabs(extents.min.x - extents.max.x) * 0.5f,
2157  kabs(extents.min.y - extents.max.y) * 0.5f,
2158  };
2159 }
2160 
2162  return (vec3){
2163  (extents.min.x + extents.max.x) * 0.5f,
2164  (extents.min.y + extents.max.y) * 0.5f,
2165  (extents.min.z + extents.max.z) * 0.5f,
2166  };
2167 }
2168 
2170  return (vec3){
2171  kabs(extents.min.x - extents.max.x) * 0.5f,
2172  kabs(extents.min.y - extents.max.y) * 0.5f,
2173  kabs(extents.min.z - extents.max.z) * 0.5f,
2174  };
2175 }
2176 
2178  return (vec2){
2179  (v_0.x - v_1.x) * 0.5f,
2180  (v_0.y - v_1.y) * 0.5f};
2181 }
2182 
2184  return (vec3){
2185  (v_0.x - v_1.x) * 0.5f,
2186  (v_0.y - v_1.y) * 0.5f,
2187  (v_0.z - v_1.z) * 0.5f};
2188 }
2189 
2191  return vec3_add(vec3_mul_scalar(v_0, 1.0f - t), vec3_mul_scalar(v_1, t));
2192 }
2193 
2195  vec3 edge1 = vec3_sub(tri->verts[1], tri->verts[0]);
2196  vec3 edge2 = vec3_sub(tri->verts[2], tri->verts[0]);
2197 
2198  vec3 normal = vec3_cross(edge1, edge2);
2199  return vec3_normalized(normal);
2200 }
2201 
2203  normal = vec3_normalized(normal);
2204  reference_up = vec3_normalized(reference_up);
2205 
2206  // Compute rotation axis as the cross product
2207  vec3 axis = vec3_cross(reference_up, normal);
2208  f32 dot = vec3_dot(reference_up, normal);
2209 
2210  // If dot is near 1, the vectors are already aligned, return identity quaternion
2211  if (dot > 0.9999f) {
2212  return quat_identity();
2213  }
2214 
2215  // If dot is near -1, the vectors are opposite, use an arbitrary perpendicular axis
2216  if (dot < -0.9999f) {
2217  axis = vec3_normalized(vec3_cross(reference_up, (vec3){1, 0, 0})); // Try X-axis
2218  if (vec3_length_squared(axis) < K_FLOAT_EPSILON) {
2219  axis = vec3_normalized(vec3_cross(reference_up, (vec3){0, 0, 1})); // Try Z-axis
2220  }
2221  }
2222 
2223  // Compute the quaternion components
2224  f32 angle = kacos(dot); // Angle between the vectors
2225  return quat_from_axis_angle(axis, angle, false);
2226 }
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:205
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:25
_Bool b8
8-bit boolean type
Definition: defines.h:58
float f32
32-bit floating point number
Definition: defines.h:47
#define KMIN(x, y)
Definition: defines.h:291
signed int i32
Signed 32-bit integer.
Definition: defines.h:39
#define KMAX(x, y)
Definition: defines.h:292
#define KINLINE
Inline qualifier.
Definition: defines.h:252
#define KCLAMP(value, min, max)
Clamps value to a range of min and max (inclusive).
Definition: defines.h:232
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:19
KINLINE mat4 mat4_inverse(mat4 matrix)
Creates and returns an inverse of the provided matrix.
Definition: kmath.h:1324
KINLINE vec3 vec3_cross(vec3 vector_0, vec3 vector_1)
Calculates and returns the cross product of the supplied vectors. The cross product is a new vector w...
Definition: kmath.h:740
KINLINE quat quat_conjugate(quat q)
Returns the conjugate of the provided quaternion. That is, The x, y and z elements are negated,...
Definition: kmath.h:1736
KINLINE vec2 vec2_mul(vec2 vector_0, vec2 vector_1)
Multiplies vector_0 by vector_1 and returns a copy of the result.
Definition: kmath.h:368
KINLINE vec4 vec4_one(void)
Creates and returns a 4-component vector with all components set to 1.0f.
Definition: kmath.h:896
KINLINE mat4 mat4_mul(mat4 matrix_0, mat4 matrix_1)
Returns the result of multiplying matrix_0 and matrix_1.
Definition: kmath.h:1148
KINLINE vec3 vec3_sign(vec3 v)
Definition: kmath.h:1796
KINLINE mat4 mat4_from_translation_rotation_scale(vec3 t, quat r, vec3 s)
Returns a matrix created from the provided translation, rotation and scale (TRS).
Definition: kmath.h:1440
KAPI f32 klog2(f32 x)
Computes the base-2 logarithm of x (i.e. how many times x can be divided by 2).
KINLINE vec4 vec4_div(vec4 vector_0, vec4 vector_1)
Divides vector_0 by vector_1 and returns a copy of the result.
Definition: kmath.h:979
KAPI i32 krandom_in_range(i32 min, i32 max)
Returns a random integer that is within the given range (inclusive).
KAPI f32 kacos(f32 x)
Calculates the arc cosine of x.
KINLINE vec2 vec2_one(void)
Creates and returns a 2-component vector with all components set to 1.0f.
Definition: kmath.h:317
KINLINE mat4 mat4_translation(vec3 position)
Creates and returns a translation matrix from the given position.
Definition: kmath.h:1410
KINLINE vec2 vec2_up(void)
Creates and returns a 2-component vector pointing up (0, 1).
Definition: kmath.h:322
KINLINE void rgbu_to_u32(u32 r, u32 g, u32 b, u32 *out_u32)
Converts rgb int values [0-255] to a single 32-bit integer.
Definition: kmath.h:1993
KINLINE f32 vec2_distance_squared(vec2 vector_0, vec2 vector_1)
Returns the squared distance between vector_0 and vector_1. NOTE: If purely for comparison purposes,...
Definition: kmath.h:493
KINLINE vec3 extents_2d_center(extents_2d extents)
Definition: kmath.h:2147
KINLINE vec3 vec3_backward(void)
Creates and returns a 3-component vector pointing backward (0, 0, 1).
Definition: kmath.h:587
KINLINE vec4 vec3_to_vec4(vec3 vector, f32 w)
Returns a new vec4 using vector as the x, y and z components and w for w.
Definition: kmath.h:543
KAPI b8 plane_intersects_sphere(const plane_3d *p, const vec3 *center, f32 radius)
Indicates if plane p intersects a sphere constructed via center and radius.
KAPI f32 ktan(f32 x)
Calculates the tangent of x.
KINLINE mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip, f32 far_clip)
Creates and returns a perspective matrix. Typically used to render 3d scenes.
Definition: kmath.h:1207
KINLINE vec4 vec4_mul_mat4(vec4 v, mat4 m)
Performs v * m.
Definition: kmath.h:1689
KINLINE vec3 vec3_max(vec3 vector_0, vec3 vector_1)
Definition: kmath.h:1789
KINLINE f32 klerp(f32 a, f32 b, f32 t)
Definition: kmath.h:205
KINLINE f32 vec2_distance(vec2 vector_0, vec2 vector_1)
Returns the distance between vector_0 and vector_1.
Definition: kmath.h:480
KINLINE vec3 vec3_lerp(vec3 v_0, vec3 v_1, f32 t)
Definition: kmath.h:2190
KINLINE mat4 mat4_identity(void)
Creates and returns an identity matrix:
Definition: kmath.h:1131
KINLINE mat4 mat4_look_at(vec3 position, vec3 target, vec3 up)
Creates and returns a look-at matrix, or a matrix looking at target from the perspective of position.
Definition: kmath.h:1228
KINLINE vec3 vec3_mid(vec3 v_0, vec3 v_1)
Definition: kmath.h:2183
KAPI f32 kfloor(f32 x)
Returns the largest integer value less than or equal to x.
KAPI f32 ksqrt(f32 x)
Calculates the square root of x.
KINLINE void kswapf(f32 *a, f32 *b)
Definition: kmath.h:87
KINLINE vec3 mat4_right(mat4 matrix)
Returns a right vector relative to the provided matrix.
Definition: kmath.h:1616
KINLINE f32 vec4_dot_f32(f32 a0, f32 a1, f32 a2, f32 a3, f32 b0, f32 b1, f32 b2, f32 b3)
Calculates the dot product using the elements of vec4s provided in split-out format.
Definition: kmath.h:1054
KAPI b8 frustum_intersects_aabb(const frustum *f, const vec3 *center, const vec3 *extents)
Indicates if frustum f intersects an axis-aligned bounding box constructed via center and extents.
KINLINE void vec4_normalize(vec4 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:1021
KINLINE vec2 vec2_sub(vec2 vector_0, vec2 vector_1)
Subtracts vector_1 from vector_0 and returns a copy of the result.
Definition: kmath.h:357
KAPI f32 ksin(f32 x)
Calculates the sine of x.
KAPI f32 kabs(f32 x)
Calculates the absolute value of x.
KINLINE mat4 mat4_scale(vec3 scale)
Returns a scale matrix using the provided scale.
Definition: kmath.h:1424
KAPI b8 frustum_intersects_ksphere(const frustum *f, const ksphere *sphere)
Indicates if the frustum intersects (or contains) a sphere constructed via center and radius.
KINLINE vec3 vec3_forward(void)
Creates and returns a 3-component vector pointing forward (0, 0, -1).
Definition: kmath.h:582
KAPI f32 kfrandom_in_range(f32 min, f32 max)
Returns a random floating-point number that is within the given range (inclusive).
KINLINE f32 vec4_length_squared(vec4 vector)
Returns the squared length of the provided vector.
Definition: kmath.h:1001
KAPI f32 plane_signed_distance(const plane_3d *p, const vec3 *position)
Obtains the signed distance between the plane p and the provided postion.
KAPI b8 plane_intersects_aabb(const plane_3d *p, const vec3 *center, const vec3 *extents)
Indicates if plane p intersects an axis-aligned bounding box constructed via center and extents.
KINLINE vec3 triangle_get_normal(const triangle *tri)
Definition: kmath.h:2194
KINLINE vec3 mat4_backward(mat4 matrix)
Returns a backward vector relative to the provided matrix.
Definition: kmath.h:1556
KINLINE void vec2_normalize(vec2 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:434
KINLINE vec3 vec3_mul(vec3 vector_0, vec3 vector_1)
Multiplies vector_0 by vector_1 and returns a copy of the result.
Definition: kmath.h:620
KINLINE vec2 vec2_right(void)
Creates and returns a 2-component vector pointing right (1, 0).
Definition: kmath.h:337
KINLINE vec2 vec2_mul_add(vec2 vector_0, vec2 vector_1, vec2 vector_2)
Multiplies vector_0 by vector_1, then adds the result to vector_2.
Definition: kmath.h:392
KINLINE mat4 mat4_orthographic(f32 left, f32 right, f32 bottom, f32 top, f32 near_clip, f32 far_clip)
Creates and returns an orthographic projection matrix. Typically used to render flat or 2D scenes.
Definition: kmath.h:1178
KINLINE quat quat_inverse(quat q)
Returns an inverse copy of the provided quaternion.
Definition: kmath.h:1744
KINLINE vec3 vec3_mul_add(vec3 vector_0, vec3 vector_1, vec3 vector_2)
Multiplies vector_0 by vector_1, then adds the result to vector_2.
Definition: kmath.h:645
KINLINE f32 vec3_length(vec3 vector)
Returns the length of the provided vector.
Definition: kmath.h:688
KINLINE vec3 vec3_sub(vec3 vector_0, vec3 vector_1)
Subtracts vector_1 from vector_0 and returns a copy of the result.
Definition: kmath.h:608
KINLINE vec2 vec2_mul_scalar(vec2 vector_0, f32 scalar)
Multiplies all elements of vector_0 by scalar and returns a copy of the result.
Definition: kmath.h:380
KAPI f32 katan(f32 x)
Calculates the arctangent of x.
KAPI f32 kattenuation_min_max(f32 min, f32 max, f32 x)
Returns the attenuation of x based off distance from the midpoint of min and max.
KINLINE vec3 mat4_mul_vec3(mat4 m, vec3 v)
Performs m * v.
Definition: kmath.h:1646
KINLINE void rgb_u32_to_vec3(u32 r, u32 g, u32 b, vec3 *out_v)
Converts rgb integer values [0-255] to a vec3 of floating-point values [0.0-1.0].
Definition: kmath.h:2020
KAPI f32 kfrandom(void)
Returns a random floating-point number.
KINLINE vec2 vec2_create(f32 x, f32 y)
Creates and returns a new 2-element vector using the supplied values.
Definition: kmath.h:300
KINLINE f32 ksmoothstep(f32 edge_0, f32 edge_1, f32 x)
Perform Hermite interpolation between two values.
Definition: kmath.h:266
#define K_RAD2DEG_MULTIPLIER
A multiplier used to convert radians to degrees.
Definition: kmath.h:57
KINLINE mat4 quat_to_rotation_matrix(quat q, vec3 center)
Calculates a rotation matrix based on the quaternion and the passed in center point.
Definition: kmath.h:1847
KINLINE vec2 vec2_down(void)
Creates and returns a 2-component vector pointing down (0, -1).
Definition: kmath.h:327
KINLINE b8 is_power_of_2(u64 value)
Indicates if the value is a power of 2. 0 is considered not a power of 2.
Definition: kmath.h:215
KAPI f32 vec3_distance_to_line(vec3 point, vec3 line_start, vec3 line_direction)
KINLINE vec3 mat4_left(mat4 matrix)
Returns a left vector relative to the provided matrix.
Definition: kmath.h:1601
KINLINE f32 deg_to_rad(f32 degrees)
Converts provided degrees to radians.
Definition: kmath.h:1959
KAPI f32 kpow(f32 x, f32 y)
KINLINE f32 vec3_length_squared(vec3 vector)
Returns the squared length of the provided vector.
Definition: kmath.h:678
KINLINE vec4 vec4_mul(vec4 vector_0, vec4 vector_1)
Multiplies vector_0 by vector_1 and returns a copy of the result.
Definition: kmath.h:935
KINLINE vec4 vec4_add(vec4 vector_0, vec4 vector_1)
Adds vector_1 to vector_0 and returns a copy of the result.
Definition: kmath.h:905
KAPI b8 frustum_intersects_sphere(const frustum *f, const vec3 *center, f32 radius)
Indicates if the frustum intersects (or contains) a sphere constructed via center and radius.
KINLINE vec4 vec4_mul_scalar(vec4 vector_0, f32 scalar)
Multiplies all elements of vector_0 by scalar and returns a copy of the result.
Definition: kmath.h:951
KINLINE quat quat_normalize(quat q)
Returns a normalized copy of the provided quaternion.
Definition: kmath.h:1724
KINLINE f32 vec2_length_squared(vec2 vector)
Returns the squared length of the provided vector.
Definition: kmath.h:415
KAPI i32 krandom(void)
Returns a random integer.
KINLINE mat4 mat4_euler_x(f32 angle_radians)
Creates a rotation matrix from the provided x angle.
Definition: kmath.h:1469
KAPI f32 kceil(f32 x)
Returns the smallest integer value greater than or equal to x.
KINLINE vec4 vec4_normalized(vec4 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:1035
KINLINE vec2 vec2_normalized(vec2 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:446
KINLINE vec3 vec3_from_vec4(vec4 vector)
Definition: kmath.h:519
KINLINE f32 quat_dot(quat q_0, quat q_1)
Calculates the dot product of the provided quaternions.
Definition: kmath.h:1778
KAPI f32 kcos(f32 x)
Calculates the cosine of x.
KINLINE vec3 vec3_create(f32 x, f32 y, f32 z)
Creates and returns a new 3-element vector using the supplied values.
Definition: kmath.h:510
KINLINE vec3 extents_3d_half(extents_3d extents)
Definition: kmath.h:2169
KAPI u64 krandom_u64(void)
Returns a random unsigned 64-bit integer.
KINLINE vec3 mat4_position(mat4 matrix)
Returns the position relative to the provided matrix.
Definition: kmath.h:1631
KINLINE quat quat_from_surface_normal(vec3 normal, vec3 reference_up)
Definition: kmath.h:2202
KINLINE vec4 vec4_from_vec3(vec3 vector, f32 w)
Returns a new vec4 using vector as the x, y and z components and w for w.
Definition: kmath.h:876
KINLINE void vec3_normalize(vec3 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:697
KINLINE vec3 mat4_down(mat4 matrix)
Returns a downward vector relative to the provided matrix.
Definition: kmath.h:1586
KINLINE vec4 vec4_zero(void)
Creates and returns a 4-component vector with all components set to 0.0f.
Definition: kmath.h:890
KINLINE vec2 vec2_div(vec2 vector_0, vec2 vector_1)
Divides vector_0 by vector_1 and returns a copy of the result.
Definition: kmath.h:405
KINLINE f32 vec2_length(vec2 vector)
Returns the length of the provided vector.
Definition: kmath.h:425
KINLINE f32 quat_normal(quat q)
Returns the normal of the provided quaternion.
Definition: kmath.h:1714
KINLINE b8 rect_2d_contains_point(rect_2d rect, vec2 point)
Definition: kmath.h:2141
KINLINE void vec4_clamp(vec4 *vector, f32 min, f32 max)
Clamps the provided vector in-place to the given min/max values.
Definition: kmath.h:1098
KINLINE vec2 vec2_add(vec2 vector_0, vec2 vector_1)
Adds vector_1 to vector_0 and returns a copy of the result.
Definition: kmath.h:346
KINLINE mat4 mat4_euler_y(f32 angle_radians)
Creates a rotation matrix from the provided y angle.
Definition: kmath.h:1487
KINLINE vec3 vec3_min(vec3 vector_0, vec3 vector_1)
Definition: kmath.h:1782
KINLINE f32 mat4_determinant(mat4 matrix)
Calculates the determinant of the given matrix.
Definition: kmath.h:1286
KINLINE f32 vec3_distance_squared(vec3 vector_0, vec3 vector_1)
Returns the squared distance between vector_0 and vector_1. Less intensive than calling the non-squar...
Definition: kmath.h:793
KINLINE mat4 mat4_transposed(mat4 matrix)
Returns a transposed copy of the provided matrix (rows->colums)
Definition: kmath.h:1259
KAPI f32 katan2(f32 x, f32 y)
KINLINE quat quat_identity(void)
Creates an identity quaternion.
Definition: kmath.h:1706
#define K_FLOAT_EPSILON
Smallest positive number where 1.0 + FLOAT_EPSILON != 0.
Definition: kmath.h:72
KINLINE f32 rad_to_deg(f32 radians)
Converts provided radians to degrees.
Definition: kmath.h:1967
KINLINE quat quat_mul(quat q_0, quat q_1)
Multiplies the provided quaternions.
Definition: kmath.h:1753
KAPI f32 klog(f32 x)
Computes the logarithm of x.
KINLINE mat4 mat4_euler_z(f32 angle_radians)
Creates a rotation matrix from the provided z angle.
Definition: kmath.h:1505
KINLINE vec3 vec3_up(void)
Creates and returns a 3-component vector pointing up (0, 1, 0).
Definition: kmath.h:562
KINLINE vec3 vec3_mul_mat4(vec3 v, mat4 m)
Performs v * m.
Definition: kmath.h:1660
KINLINE vec4 vec4_create(f32 x, f32 y, f32 z, f32 w)
Creates and returns a new 4-element vector using the supplied values.
Definition: kmath.h:844
KINLINE vec4 vec4_div_scalar(vec4 vector_0, f32 scalar)
Definition: kmath.h:987
KINLINE vec3 vec3_down(void)
Creates and returns a 3-component vector pointing down (0, -1, 0).
Definition: kmath.h:567
KINLINE vec2 vec2_zero(void)
Creates and returns a 2-component vector with all components set to 0.0f.
Definition: kmath.h:311
KAPI frustum frustum_from_view_projection(mat4 view_projection)
KINLINE vec2 vec2_mid(vec2 v_0, vec2 v_1)
Definition: kmath.h:2177
KINLINE vec3 vec3_project(vec3 v_0, vec3 v_1)
Projects v_0 onto v_1.
Definition: kmath.h:806
KINLINE vec3 mat4_forward(mat4 matrix)
Returns a forward vector relative to the provided matrix.
Definition: kmath.h:1541
KINLINE f32 ksign(f32 x)
Returns 0.0f if x == 0.0f, -1.0f if negative, otherwise 1.0f.
Definition: kmath.h:101
KINLINE vec2 vec2_left(void)
Creates and returns a 2-component vector pointing left (-1, 0).
Definition: kmath.h:332
KINLINE vec3 extents_3d_center(extents_3d extents)
Definition: kmath.h:2161
KINLINE b8 vec2_compare(vec2 vector_0, vec2 vector_1, f32 tolerance)
Compares all elements of vector_0 and vector_1 and ensures the difference is less than tolerance.
Definition: kmath.h:461
KINLINE vec3 vec3_mul_scalar(vec3 vector_0, f32 scalar)
Multiplies all elements of vector_0 by scalar and returns a copy of the result.
Definition: kmath.h:633
KAPI f32 kasin(f32 x)
KINLINE void vec3_to_rgb_u32(vec3 v, u32 *out_r, u32 *out_g, u32 *out_b)
Converts a vec3 of rgbvalues [0.0-1.0] to integer rgb values [0-255].
Definition: kmath.h:2034
KINLINE f32 kstep(f32 edge, f32 x)
Compares x to edge, returning 0 if x < edge; otherwise 1.0f;.
Definition: kmath.h:107
KINLINE f32 vec4_length(vec4 vector)
Returns the length of the provided vector.
Definition: kmath.h:1012
KINLINE mat4 mat4_euler_xyz(f32 x_radians, f32 y_radians, f32 z_radians)
Creates a rotation matrix from the provided x, y and z axis rotations.
Definition: kmath.h:1526
KINLINE vec3 vec3_left(void)
Creates and returns a 3-component vector pointing left (-1, 0, 0).
Definition: kmath.h:572
KINLINE mat4 quat_to_mat4(quat q)
Creates a rotation matrix from the given quaternion.
Definition: kmath.h:1817
KINLINE b8 kfloat_compare(f32 f_0, f32 f_1)
Compares the two floats and returns true if both are less than K_FLOAT_EPSILON apart; otherwise false...
Definition: kmath.h:285
KINLINE vec3 mat4_up(mat4 matrix)
Returns a upward vector relative to the provided matrix.
Definition: kmath.h:1571
KINLINE vec3 vec4_to_vec3(vec4 vector)
Returns a new vec3 containing the x, y and z components of the supplied vec4, essentially dropping th...
Definition: kmath.h:864
KINLINE vec3 vec3_div_scalar(vec3 vector_0, f32 scalar)
Definition: kmath.h:664
KINLINE vec4 vec4_mul_add(vec4 vector_0, vec4 vector_1, vec4 vector_2)
Multiplies vector_0 by vector_1, then adds the result to vector_2.
Definition: kmath.h:963
KINLINE vec3 vec3_from_vec2(vec2 vector, f32 z)
Definition: kmath.h:531
KINLINE vec4 vec4_sub(vec4 vector_0, vec4 vector_1)
Subtracts vector_1 from vector_0 and returns a copy of the result.
Definition: kmath.h:920
KINLINE vec3 vec3_rotate(vec3 v, quat q)
Definition: kmath.h:1800
KINLINE b8 vec3_compare(vec3 vector_0, vec3 vector_1, f32 tolerance)
Compares all elements of vector_0 and vector_1 and ensures the difference is less than tolerance.
Definition: kmath.h:756
KAPI plane_3d plane_3d_create(vec3 p1, vec3 norm)
KINLINE vec3 vec3_right(void)
Creates and returns a 3-component vector pointing right (1, 0, 0).
Definition: kmath.h:577
KAPI void frustum_corner_points_world_space(mat4 projection_view, vec4 *corners)
KINLINE quat quat_from_axis_angle(vec3 axis, f32 angle, b8 normalize)
Creates a quaternion from the given axis and angle.
Definition: kmath.h:1881
KINLINE vec3 extents_2d_half(extents_2d extents)
Definition: kmath.h:2154
KINLINE vec3 vec3_one(void)
Creates and returns a 3-component vector with all components set to 1.0f.
Definition: kmath.h:557
KINLINE vec4 mat4_mul_vec4(mat4 m, vec4 v)
Performs m * v.
Definition: kmath.h:1674
KINLINE b8 vec4_compare(vec4 vector_0, vec4 vector_1, f32 tolerance)
Compares all elements of vector_0 and vector_1 and ensures the difference is less than tolerance.
Definition: kmath.h:1071
KINLINE f32 vec3_dot(vec3 vector_0, vec3 vector_1)
Returns the dot product between the provided vectors. Typically used to calculate the difference in d...
Definition: kmath.h:723
KINLINE f32 vec3_distance(vec3 vector_0, vec3 vector_1)
Returns the distance between vector_0 and vector_1.
Definition: kmath.h:779
KINLINE void u32_to_rgb(u32 rgbu, u32 *out_r, u32 *out_g, u32 *out_b)
Converts the given 32-bit integer to rgb values [0-255].
Definition: kmath.h:2005
KAPI frustum frustum_create(const vec3 *position, const vec3 *target, const vec3 *up, f32 aspect, f32 fov, f32 near, f32 far)
Creates and returns a frustum based on the provided position, direction vectors, aspect,...
KINLINE vec3 vec3_transform(vec3 v, f32 w, mat4 m)
Transform v by m.
Definition: kmath.h:823
KINLINE vec3 vec3_div(vec3 vector_0, vec3 vector_1)
Divides vector_0 by vector_1 and returns a copy of the result.
Definition: kmath.h:659
KINLINE quat quat_slerp(quat q_0, quat q_1, f32 percentage)
Calculates spherical linear interpolation of a given percentage between two quaternions.
Definition: kmath.h:1903
KINLINE vec3 vec3_zero(void)
Creates and returns a 3-component vector with all components set to 0.0f.
Definition: kmath.h:551
KINLINE vec3 vec3_add(vec3 vector_0, vec3 vector_1)
Adds vector_1 to vector_0 and returns a copy of the result.
Definition: kmath.h:596
KINLINE f32 range_convert_f32(f32 value, f32 old_min, f32 old_max, f32 new_min, f32 new_max)
Converts value from the "old" range to the "new" range.
Definition: kmath.h:1979
#define K_DEG2RAD_MULTIPLIER
A multiplier used to convert degrees to radians.
Definition: kmath.h:54
KINLINE vec3 vec3_normalized(vec3 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:710
KINLINE vec4 vec4_clamped(vec4 vector, f32 min, f32 max)
Returns a clamped copy of the provided vector.
Definition: kmath.h:1114
This file contains the structures and functions of the memory system. This is responsible for memory ...
KAPI void * kzero_memory(void *block, u64 size)
Zeroes out the provided memory block.
Contains various math types required for the engine.
union vec2_u vec2
A 2-element vector.
union vec3_u vec3
A 3-element vector.
vec4 quat
A quaternion, used to represent rotational orientation.
Definition: math_types.h:135
Represents the extents of a 2d object.
Definition: math_types.h:203
vec2 max
The maximum extents of the object.
Definition: math_types.h:207
vec2 min
The minimum extents of the object.
Definition: math_types.h:205
Represents the extents of a 3d object.
Definition: math_types.h:213
vec3 min
The minimum extents of the object.
Definition: math_types.h:215
vec3 max
The maximum extents of the object.
Definition: math_types.h:217
Definition: math_types.h:272
Definition: math_types.h:395
Definition: math_types.h:256
Definition: math_types.h:391
vec3 verts[3]
Definition: math_types.h:392
A 3x3 matrix.
Definition: math_types.h:189
f32 data[12]
The matrix elements.
Definition: math_types.h:191
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:195
f32 data[16]
The matrix elements.
Definition: math_types.h:197
A 2-element vector.
Definition: math_types.h:19
f32 x
The first element.
Definition: math_types.h:25
f32 y
The second element.
Definition: math_types.h:35
A 3-element vector.
Definition: math_types.h:49
f32 elements[3]
An array of x, y, z.
Definition: math_types.h:51
f32 r
The first element.
Definition: math_types.h:57
f32 b
The third element.
Definition: math_types.h:77
f32 x
The first element.
Definition: math_types.h:55
f32 g
The second element.
Definition: math_types.h:67
f32 z
The third element.
Definition: math_types.h:75
f32 y
The second element.
Definition: math_types.h:65
A 4-element vector.
Definition: math_types.h:89
f32 elements[4]
An array of x, y, z, w.
Definition: math_types.h:91
f32 height
The fourth element.
Definition: math_types.h:128
f32 x
The first element.
Definition: math_types.h:96
f32 width
The third element.
Definition: math_types.h:118
f32 z
The third element.
Definition: math_types.h:112
f32 y
The second element.
Definition: math_types.h:104
f32 w
The fourth element.
Definition: math_types.h:122