Kohi Game Engine
kmath.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include "core/kmemory.h"
16 #include "defines.h"
17 #include "math_types.h"
18 
20 #define K_PI 3.14159265358979323846f
21 
23 #define K_2PI (2.0f * K_PI)
24 
26 #define K_4PI (4.0f * K_PI)
27 
29 #define K_HALF_PI (0.5f * K_PI)
30 
32 #define K_QUARTER_PI (0.25f * K_PI)
33 
35 #define K_ONE_OVER_PI (1.0f / K_PI)
36 
38 #define K_ONE_OVER_TWO_PI (1.0f / K_2PI)
39 
41 #define K_SQRT_TWO 1.41421356237309504880f
42 
44 #define K_SQRT_THREE 1.73205080756887729352f
45 
47 #define K_SQRT_ONE_OVER_TWO 0.70710678118654752440f
48 
50 #define K_SQRT_ONE_OVER_THREE 0.57735026918962576450f
51 
53 #define K_DEG2RAD_MULTIPLIER (K_PI / 180.0f)
54 
56 #define K_RAD2DEG_MULTIPLIER (180.0f / K_PI)
57 
59 #define K_SEC_TO_US_MULTIPLIER (1000.0f * 1000.0f)
60 
62 #define K_SEC_TO_MS_MULTIPLIER 1000.0f
63 
65 #define K_MS_TO_SEC_MULTIPLIER 0.001f
66 
68 #define K_INFINITY (1e30f * 1e30f)
69 
71 #define K_FLOAT_EPSILON 1.192092896e-07f
72 
73 // ------------------------------------------
74 // General math functions
75 // ------------------------------------------
76 
82 KINLINE void kswapf(f32 *a, f32 *b) {
83  f32 temp = *a;
84  *a = *b;
85  *b = temp;
86 }
87 
88 #define KSWAP(type, a, b) \
89  { \
90  type temp = a; \
91  a = b; \
92  b = temp; \
93  }
94 
97  return x == 0.0f ? 0.0f : x < 0.0f ? -1.0f
98  : 1.0f;
99 }
100 
102 KINLINE f32 kstep(f32 edge, f32 x) {
103  return x < edge ? 0.0f : 1.0f;
104 }
105 
113 
121 
129 
137 
145 
153 
161 
169 
177  return (value != 0) && ((value & (value - 1)) == 0);
178 }
179 
186 
195 
202 
212 
221 KINLINE f32 ksmoothstep(f32 edge_0, f32 edge_1, f32 x) {
222  f32 t = KCLAMP((x - edge_0) / (edge_1 - edge_0), 0.0f, 1.0f);
223  return t * t * (3.0 - 2.0 * t);
224 }
225 
231  return kabs(f_0 - f_1) < K_FLOAT_EPSILON;
232 }
233 
234 // ------------------------------------------
235 // Vector 2
236 // ------------------------------------------
237 
246  vec2 out_vector;
247  out_vector.x = x;
248  out_vector.y = y;
249  return out_vector;
250 }
251 
256 KINLINE vec2 vec2_zero(void) { return (vec2){0.0f, 0.0f}; }
257 
262 KINLINE vec2 vec2_one(void) { return (vec2){1.0f, 1.0f}; }
263 
267 KINLINE vec2 vec2_up(void) { return (vec2){0.0f, 1.0f}; }
268 
272 KINLINE vec2 vec2_down(void) { return (vec2){0.0f, -1.0f}; }
273 
277 KINLINE vec2 vec2_left(void) { return (vec2){-1.0f, 0.0f}; }
278 
282 KINLINE vec2 vec2_right(void) { return (vec2){1.0f, 0.0f}; }
283 
291 KINLINE vec2 vec2_add(vec2 vector_0, vec2 vector_1) {
292  return (vec2){vector_0.x + vector_1.x, vector_0.y + vector_1.y};
293 }
294 
302 KINLINE vec2 vec2_sub(vec2 vector_0, vec2 vector_1) {
303  return (vec2){vector_0.x - vector_1.x, vector_0.y - vector_1.y};
304 }
305 
313 KINLINE vec2 vec2_mul(vec2 vector_0, vec2 vector_1) {
314  return (vec2){vector_0.x * vector_1.x, vector_0.y * vector_1.y};
315 }
316 
325 KINLINE vec2 vec2_mul_scalar(vec2 vector_0, f32 scalar) {
326  return (vec2){vector_0.x * scalar, vector_0.y * scalar};
327 }
328 
337 KINLINE vec2 vec2_mul_add(vec2 vector_0, vec2 vector_1, vec2 vector_2) {
338  return (vec2){
339  vector_0.x * vector_1.x + vector_2.x,
340  vector_0.y * vector_1.y + vector_2.y};
341 }
342 
350 KINLINE vec2 vec2_div(vec2 vector_0, vec2 vector_1) {
351  return (vec2){vector_0.x / vector_1.x, vector_0.y / vector_1.y};
352 }
353 
361  return vector.x * vector.x + vector.y * vector.y;
362 }
363 
371  return ksqrt(vec2_length_squared(vector));
372 }
373 
379 KINLINE void vec2_normalize(vec2 *vector) {
380  const f32 length = vec2_length(*vector);
381  vector->x /= length;
382  vector->y /= length;
383 }
384 
392  vec2_normalize(&vector);
393  return vector;
394 }
395 
406 KINLINE b8 vec2_compare(vec2 vector_0, vec2 vector_1, f32 tolerance) {
407  if (kabs(vector_0.x - vector_1.x) > tolerance) {
408  return false;
409  }
410 
411  if (kabs(vector_0.y - vector_1.y) > tolerance) {
412  return false;
413  }
414 
415  return true;
416 }
417 
425 KINLINE f32 vec2_distance(vec2 vector_0, vec2 vector_1) {
426  vec2 d = (vec2){vector_0.x - vector_1.x, vector_0.y - vector_1.y};
427  return vec2_length(d);
428 }
429 
439  vec2 d = (vec2){vector_0.x - vector_1.x, vector_0.y - vector_1.y};
440  return vec2_length_squared(d);
441 }
442 
443 // ------------------------------------------
444 // Vector 3
445 // ------------------------------------------
446 
455 KINLINE vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){x, y, z}; }
456 
465  return (vec3){vector.x, vector.y, vector.z};
466 }
467 
477  return (vec4){vector.x, vector.y, vector.z, w};
478 }
479 
484 KINLINE vec3 vec3_zero(void) { return (vec3){0.0f, 0.0f, 0.0f}; }
485 
490 KINLINE vec3 vec3_one(void) { return (vec3){1.0f, 1.0f, 1.0f}; }
491 
495 KINLINE vec3 vec3_up(void) { return (vec3){0.0f, 1.0f, 0.0f}; }
496 
500 KINLINE vec3 vec3_down(void) { return (vec3){0.0f, -1.0f, 0.0f}; }
501 
505 KINLINE vec3 vec3_left(void) { return (vec3){-1.0f, 0.0f, 0.0f}; }
506 
510 KINLINE vec3 vec3_right(void) { return (vec3){1.0f, 0.0f, 0.0f}; }
511 
515 KINLINE vec3 vec3_forward(void) { return (vec3){0.0f, 0.0f, -1.0f}; }
516 
520 KINLINE vec3 vec3_back(void) { return (vec3){0.0f, 0.0f, 1.0f}; }
521 
529 KINLINE vec3 vec3_add(vec3 vector_0, vec3 vector_1) {
530  return (vec3){vector_0.x + vector_1.x, vector_0.y + vector_1.y,
531  vector_0.z + vector_1.z};
532 }
533 
541 KINLINE vec3 vec3_sub(vec3 vector_0, vec3 vector_1) {
542  return (vec3){vector_0.x - vector_1.x, vector_0.y - vector_1.y,
543  vector_0.z - vector_1.z};
544 }
545 
553 KINLINE vec3 vec3_mul(vec3 vector_0, vec3 vector_1) {
554  return (vec3){vector_0.x * vector_1.x, vector_0.y * vector_1.y,
555  vector_0.z * vector_1.z};
556 }
557 
566 KINLINE vec3 vec3_mul_scalar(vec3 vector_0, f32 scalar) {
567  return (vec3){vector_0.x * scalar, vector_0.y * scalar, vector_0.z * scalar};
568 }
569 
578 KINLINE vec3 vec3_mul_add(vec3 vector_0, vec3 vector_1, vec3 vector_2) {
579  return (vec3){
580  vector_0.x * vector_1.x + vector_2.x,
581  vector_0.y * vector_1.y + vector_2.y,
582  vector_0.z * vector_1.z + vector_2.z};
583 }
584 
592 KINLINE vec3 vec3_div(vec3 vector_0, vec3 vector_1) {
593  return (vec3){vector_0.x / vector_1.x, vector_0.y / vector_1.y,
594  vector_0.z / vector_1.z};
595 }
596 
604  return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z;
605 }
606 
614  return ksqrt(vec3_length_squared(vector));
615 }
616 
622 KINLINE void vec3_normalize(vec3 *vector) {
623  const f32 length = vec3_length(*vector);
624  vector->x /= length;
625  vector->y /= length;
626  vector->z /= length;
627 }
628 
636  vec3_normalize(&vector);
637  return vector;
638 }
639 
648 KINLINE f32 vec3_dot(vec3 vector_0, vec3 vector_1) {
649  f32 p = 0;
650  p += vector_0.x * vector_1.x;
651  p += vector_0.y * vector_1.y;
652  p += vector_0.z * vector_1.z;
653  return p;
654 }
655 
665 KINLINE vec3 vec3_cross(vec3 vector_0, vec3 vector_1) {
666  return (vec3){vector_0.y * vector_1.z - vector_0.z * vector_1.y,
667  vector_0.z * vector_1.x - vector_0.x * vector_1.z,
668  vector_0.x * vector_1.y - vector_0.y * vector_1.x};
669 }
670 
681 KINLINE b8 vec3_compare(vec3 vector_0, vec3 vector_1, f32 tolerance) {
682  if (kabs(vector_0.x - vector_1.x) > tolerance) {
683  return false;
684  }
685 
686  if (kabs(vector_0.y - vector_1.y) > tolerance) {
687  return false;
688  }
689 
690  if (kabs(vector_0.z - vector_1.z) > tolerance) {
691  return false;
692  }
693 
694  return true;
695 }
696 
704 KINLINE f32 vec3_distance(vec3 vector_0, vec3 vector_1) {
705  vec3 d = (vec3){vector_0.x - vector_1.x, vector_0.y - vector_1.y,
706  vector_0.z - vector_1.z};
707  return vec3_length(d);
708 }
709 
719  vec3 d = (vec3){vector_0.x - vector_1.x, vector_0.y - vector_1.y,
720  vector_0.z - vector_1.z};
721  return vec3_length_squared(d);
722 }
723 
732  vec3 out;
733  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];
734  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];
735  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];
736  return out;
737 }
738 
739 // ------------------------------------------
740 // Vector 4
741 // ------------------------------------------
742 
753  vec4 out_vector;
754 #if defined(KUSE_SIMD)
755  out_vector.data = _mm_setr_ps(x, y, z, w);
756 #else
757  out_vector.x = x;
758  out_vector.y = y;
759  out_vector.z = z;
760  out_vector.w = w;
761 #endif
762  return out_vector;
763 }
764 
773  return (vec3){vector.x, vector.y, vector.z};
774 }
775 
785 #if defined(KUSE_SIMD)
786  vec4 out_vector;
787  out_vector.data = _mm_setr_ps(x, y, z, w);
788  return out_vector;
789 #else
790  return (vec4){vector.x, vector.y, vector.z, w};
791 #endif
792 }
793 
798 KINLINE vec4 vec4_zero(void) { return (vec4){0.0f, 0.0f, 0.0f, 0.0f}; }
799 
804 KINLINE vec4 vec4_one(void) { return (vec4){1.0f, 1.0f, 1.0f, 1.0f}; }
805 
813 KINLINE vec4 vec4_add(vec4 vector_0, vec4 vector_1) {
814  vec4 result;
815  for (u64 i = 0; i < 4; ++i) {
816  result.elements[i] = vector_0.elements[i] + vector_1.elements[i];
817  }
818  return result;
819 }
820 
828 KINLINE vec4 vec4_sub(vec4 vector_0, vec4 vector_1) {
829  vec4 result;
830  for (u64 i = 0; i < 4; ++i) {
831  result.elements[i] = vector_0.elements[i] - vector_1.elements[i];
832  }
833  return result;
834 }
835 
843 KINLINE vec4 vec4_mul(vec4 vector_0, vec4 vector_1) {
844  vec4 result;
845  for (u64 i = 0; i < 4; ++i) {
846  result.elements[i] = vector_0.elements[i] * vector_1.elements[i];
847  }
848  return result;
849 }
850 
859 KINLINE vec4 vec4_mul_scalar(vec4 vector_0, f32 scalar) {
860  return (vec4){vector_0.x * scalar, vector_0.y * scalar, vector_0.z * scalar, vector_0.w * scalar};
861 }
862 
871 KINLINE vec4 vec4_mul_add(vec4 vector_0, vec4 vector_1, vec4 vector_2) {
872  return (vec4){
873  vector_0.x * vector_1.x + vector_2.x,
874  vector_0.y * vector_1.y + vector_2.y,
875  vector_0.z * vector_1.z + vector_2.z,
876  vector_0.w * vector_1.w + vector_2.w,
877  };
878 }
879 
887 KINLINE vec4 vec4_div(vec4 vector_0, vec4 vector_1) {
888  vec4 result;
889  for (u64 i = 0; i < 4; ++i) {
890  result.elements[i] = vector_0.elements[i] / vector_1.elements[i];
891  }
892  return result;
893 }
894 
902  return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z +
903  vector.w * vector.w;
904 }
905 
913  return ksqrt(vec4_length_squared(vector));
914 }
915 
921 KINLINE void vec4_normalize(vec4 *vector) {
922  const f32 length = vec4_length(*vector);
923  vector->x /= length;
924  vector->y /= length;
925  vector->z /= length;
926  vector->w /= length;
927 }
928 
936  vec4_normalize(&vector);
937  return vector;
938 }
939 
954 KINLINE f32 vec4_dot_f32(f32 a0, f32 a1, f32 a2, f32 a3, f32 b0, f32 b1, f32 b2,
955  f32 b3) {
956  f32 p;
957  p = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
958  return p;
959 }
960 
971 KINLINE b8 vec4_compare(vec4 vector_0, vec4 vector_1, f32 tolerance) {
972  if (kabs(vector_0.x - vector_1.x) > tolerance) {
973  return false;
974  }
975 
976  if (kabs(vector_0.y - vector_1.y) > tolerance) {
977  return false;
978  }
979 
980  if (kabs(vector_0.z - vector_1.z) > tolerance) {
981  return false;
982  }
983 
984  if (kabs(vector_0.w - vector_1.w) > tolerance) {
985  return false;
986  }
987 
988  return true;
989 }
990 
1004  mat4 out_matrix;
1005  kzero_memory(out_matrix.data, sizeof(f32) * 16);
1006  out_matrix.data[0] = 1.0f;
1007  out_matrix.data[5] = 1.0f;
1008  out_matrix.data[10] = 1.0f;
1009  out_matrix.data[15] = 1.0f;
1010  return out_matrix;
1011 }
1012 
1020 KINLINE mat4 mat4_mul(mat4 matrix_0, mat4 matrix_1) {
1021  mat4 out_matrix = mat4_identity();
1022 
1023  const f32 *m1_ptr = matrix_0.data;
1024  const f32 *m2_ptr = matrix_1.data;
1025  f32 *dst_ptr = out_matrix.data;
1026 
1027  for (i32 i = 0; i < 4; ++i) {
1028  for (i32 j = 0; j < 4; ++j) {
1029  *dst_ptr = m1_ptr[0] * m2_ptr[0 + j] + m1_ptr[1] * m2_ptr[4 + j] +
1030  m1_ptr[2] * m2_ptr[8 + j] + m1_ptr[3] * m2_ptr[12 + j];
1031  dst_ptr++;
1032  }
1033  m1_ptr += 4;
1034  }
1035  return out_matrix;
1036 }
1037 
1050 KINLINE mat4 mat4_orthographic(f32 left, f32 right, f32 bottom, f32 top,
1051  f32 near_clip, f32 far_clip) {
1052  mat4 out_matrix = mat4_identity();
1053 
1054  f32 lr = 1.0f / (left - right);
1055  f32 bt = 1.0f / (bottom - top);
1056  f32 nf = 1.0f / (near_clip - far_clip);
1057 
1058  out_matrix.data[0] = -2.0f * lr;
1059  out_matrix.data[5] = -2.0f * bt;
1060  out_matrix.data[10] = 2.0f * nf;
1061 
1062  out_matrix.data[12] = (left + right) * lr;
1063  out_matrix.data[13] = (top + bottom) * bt;
1064  out_matrix.data[14] = (far_clip + near_clip) * nf;
1065  return out_matrix;
1066 }
1067 
1078 KINLINE mat4 mat4_perspective(f32 fov_radians, f32 aspect_ratio, f32 near_clip,
1079  f32 far_clip) {
1080  f32 half_tan_fov = ktan(fov_radians * 0.5f);
1081  mat4 out_matrix;
1082  kzero_memory(out_matrix.data, sizeof(f32) * 16);
1083  out_matrix.data[0] = 1.0f / (aspect_ratio * half_tan_fov);
1084  out_matrix.data[5] = 1.0f / half_tan_fov;
1085  out_matrix.data[10] = -((far_clip + near_clip) / (far_clip - near_clip));
1086  out_matrix.data[11] = -1.0f;
1087  out_matrix.data[14] =
1088  -((2.0f * far_clip * near_clip) / (far_clip - near_clip));
1089  return out_matrix;
1090 }
1091 
1101 KINLINE mat4 mat4_look_at(vec3 position, vec3 target, vec3 up) {
1102  mat4 out_matrix;
1103  vec3 z_axis;
1104  z_axis.x = target.x - position.x;
1105  z_axis.y = target.y - position.y;
1106  z_axis.z = target.z - position.z;
1107 
1108  z_axis = vec3_normalized(z_axis);
1109  vec3 x_axis = vec3_normalized(vec3_cross(z_axis, up));
1110  vec3 y_axis = vec3_cross(x_axis, z_axis);
1111 
1112  out_matrix.data[0] = x_axis.x;
1113  out_matrix.data[1] = y_axis.x;
1114  out_matrix.data[2] = -z_axis.x;
1115  out_matrix.data[3] = 0;
1116  out_matrix.data[4] = x_axis.y;
1117  out_matrix.data[5] = y_axis.y;
1118  out_matrix.data[6] = -z_axis.y;
1119  out_matrix.data[7] = 0;
1120  out_matrix.data[8] = x_axis.z;
1121  out_matrix.data[9] = y_axis.z;
1122  out_matrix.data[10] = -z_axis.z;
1123  out_matrix.data[11] = 0;
1124  out_matrix.data[12] = -vec3_dot(x_axis, position);
1125  out_matrix.data[13] = -vec3_dot(y_axis, position);
1126  out_matrix.data[14] = vec3_dot(z_axis, position);
1127  out_matrix.data[15] = 1.0f;
1128 
1129  return out_matrix;
1130 }
1131 
1139  mat4 out_matrix = mat4_identity();
1140  out_matrix.data[0] = matrix.data[0];
1141  out_matrix.data[1] = matrix.data[4];
1142  out_matrix.data[2] = matrix.data[8];
1143  out_matrix.data[3] = matrix.data[12];
1144  out_matrix.data[4] = matrix.data[1];
1145  out_matrix.data[5] = matrix.data[5];
1146  out_matrix.data[6] = matrix.data[9];
1147  out_matrix.data[7] = matrix.data[13];
1148  out_matrix.data[8] = matrix.data[2];
1149  out_matrix.data[9] = matrix.data[6];
1150  out_matrix.data[10] = matrix.data[10];
1151  out_matrix.data[11] = matrix.data[14];
1152  out_matrix.data[12] = matrix.data[3];
1153  out_matrix.data[13] = matrix.data[7];
1154  out_matrix.data[14] = matrix.data[11];
1155  out_matrix.data[15] = matrix.data[15];
1156  return out_matrix;
1157 }
1158 
1166  const f32 *m = matrix.data;
1167 
1168  f32 t0 = m[10] * m[15];
1169  f32 t1 = m[14] * m[11];
1170  f32 t2 = m[6] * m[15];
1171  f32 t3 = m[14] * m[7];
1172  f32 t4 = m[6] * m[11];
1173  f32 t5 = m[10] * m[7];
1174  f32 t6 = m[2] * m[15];
1175  f32 t7 = m[14] * m[3];
1176  f32 t8 = m[2] * m[11];
1177  f32 t9 = m[10] * m[3];
1178  f32 t10 = m[2] * m[7];
1179  f32 t11 = m[6] * m[3];
1180 
1181  mat3 temp_mat;
1182  f32 *o = temp_mat.data;
1183 
1184  o[0] = (t0 * m[5] + t3 * m[9] + t4 * m[13]) -
1185  (t1 * m[5] + t2 * m[9] + t5 * m[13]);
1186  o[1] = (t1 * m[1] + t6 * m[9] + t9 * m[13]) -
1187  (t0 * m[1] + t7 * m[9] + t8 * m[13]);
1188  o[2] = (t2 * m[1] + t7 * m[5] + t10 * m[13]) -
1189  (t3 * m[1] + t6 * m[5] + t11 * m[13]);
1190  o[3] = (t5 * m[1] + t8 * m[5] + t11 * m[9]) -
1191  (t4 * m[1] + t9 * m[5] + t10 * m[9]);
1192 
1193  f32 determinant = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * o[3]);
1194  return determinant;
1195 }
1196 
1204  const f32 *m = matrix.data;
1205 
1206  f32 t0 = m[10] * m[15];
1207  f32 t1 = m[14] * m[11];
1208  f32 t2 = m[6] * m[15];
1209  f32 t3 = m[14] * m[7];
1210  f32 t4 = m[6] * m[11];
1211  f32 t5 = m[10] * m[7];
1212  f32 t6 = m[2] * m[15];
1213  f32 t7 = m[14] * m[3];
1214  f32 t8 = m[2] * m[11];
1215  f32 t9 = m[10] * m[3];
1216  f32 t10 = m[2] * m[7];
1217  f32 t11 = m[6] * m[3];
1218  f32 t12 = m[8] * m[13];
1219  f32 t13 = m[12] * m[9];
1220  f32 t14 = m[4] * m[13];
1221  f32 t15 = m[12] * m[5];
1222  f32 t16 = m[4] * m[9];
1223  f32 t17 = m[8] * m[5];
1224  f32 t18 = m[0] * m[13];
1225  f32 t19 = m[12] * m[1];
1226  f32 t20 = m[0] * m[9];
1227  f32 t21 = m[8] * m[1];
1228  f32 t22 = m[0] * m[5];
1229  f32 t23 = m[4] * m[1];
1230 
1231  mat4 out_matrix;
1232  f32 *o = out_matrix.data;
1233 
1234  o[0] = (t0 * m[5] + t3 * m[9] + t4 * m[13]) -
1235  (t1 * m[5] + t2 * m[9] + t5 * m[13]);
1236  o[1] = (t1 * m[1] + t6 * m[9] + t9 * m[13]) -
1237  (t0 * m[1] + t7 * m[9] + t8 * m[13]);
1238  o[2] = (t2 * m[1] + t7 * m[5] + t10 * m[13]) -
1239  (t3 * m[1] + t6 * m[5] + t11 * m[13]);
1240  o[3] = (t5 * m[1] + t8 * m[5] + t11 * m[9]) -
1241  (t4 * m[1] + t9 * m[5] + t10 * m[9]);
1242 
1243  f32 d = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * o[3]);
1244 
1245  o[0] = d * o[0];
1246  o[1] = d * o[1];
1247  o[2] = d * o[2];
1248  o[3] = d * o[3];
1249  o[4] = d * ((t1 * m[4] + t2 * m[8] + t5 * m[12]) -
1250  (t0 * m[4] + t3 * m[8] + t4 * m[12]));
1251  o[5] = d * ((t0 * m[0] + t7 * m[8] + t8 * m[12]) -
1252  (t1 * m[0] + t6 * m[8] + t9 * m[12]));
1253  o[6] = d * ((t3 * m[0] + t6 * m[4] + t11 * m[12]) -
1254  (t2 * m[0] + t7 * m[4] + t10 * m[12]));
1255  o[7] = d * ((t4 * m[0] + t9 * m[4] + t10 * m[8]) -
1256  (t5 * m[0] + t8 * m[4] + t11 * m[8]));
1257  o[8] = d * ((t12 * m[7] + t15 * m[11] + t16 * m[15]) -
1258  (t13 * m[7] + t14 * m[11] + t17 * m[15]));
1259  o[9] = d * ((t13 * m[3] + t18 * m[11] + t21 * m[15]) -
1260  (t12 * m[3] + t19 * m[11] + t20 * m[15]));
1261  o[10] = d * ((t14 * m[3] + t19 * m[7] + t22 * m[15]) -
1262  (t15 * m[3] + t18 * m[7] + t23 * m[15]));
1263  o[11] = d * ((t17 * m[3] + t20 * m[7] + t23 * m[11]) -
1264  (t16 * m[3] + t21 * m[7] + t22 * m[11]));
1265  o[12] = d * ((t14 * m[10] + t17 * m[14] + t13 * m[6]) -
1266  (t16 * m[14] + t12 * m[6] + t15 * m[10]));
1267  o[13] = d * ((t20 * m[14] + t12 * m[2] + t19 * m[10]) -
1268  (t18 * m[10] + t21 * m[14] + t13 * m[2]));
1269  o[14] = d * ((t18 * m[6] + t23 * m[14] + t15 * m[2]) -
1270  (t22 * m[14] + t14 * m[2] + t19 * m[6]));
1271  o[15] = d * ((t22 * m[10] + t16 * m[2] + t21 * m[6]) -
1272  (t20 * m[6] + t23 * m[10] + t17 * m[2]));
1273 
1274  return out_matrix;
1275 }
1276 
1284  mat4 out_matrix = mat4_identity();
1285  out_matrix.data[12] = position.x;
1286  out_matrix.data[13] = position.y;
1287  out_matrix.data[14] = position.z;
1288  return out_matrix;
1289 }
1290 
1298  mat4 out_matrix = mat4_identity();
1299  out_matrix.data[0] = scale.x;
1300  out_matrix.data[5] = scale.y;
1301  out_matrix.data[10] = scale.z;
1302  return out_matrix;
1303 }
1304 
1311 KINLINE mat4 mat4_euler_x(f32 angle_radians) {
1312  mat4 out_matrix = mat4_identity();
1313  f32 c = kcos(angle_radians);
1314  f32 s = ksin(angle_radians);
1315 
1316  out_matrix.data[5] = c;
1317  out_matrix.data[6] = s;
1318  out_matrix.data[9] = -s;
1319  out_matrix.data[10] = c;
1320  return out_matrix;
1321 }
1322 
1329 KINLINE mat4 mat4_euler_y(f32 angle_radians) {
1330  mat4 out_matrix = mat4_identity();
1331  f32 c = kcos(angle_radians);
1332  f32 s = ksin(angle_radians);
1333 
1334  out_matrix.data[0] = c;
1335  out_matrix.data[2] = -s;
1336  out_matrix.data[8] = s;
1337  out_matrix.data[10] = c;
1338  return out_matrix;
1339 }
1340 
1347 KINLINE mat4 mat4_euler_z(f32 angle_radians) {
1348  mat4 out_matrix = mat4_identity();
1349 
1350  f32 c = kcos(angle_radians);
1351  f32 s = ksin(angle_radians);
1352 
1353  out_matrix.data[0] = c;
1354  out_matrix.data[1] = s;
1355  out_matrix.data[4] = -s;
1356  out_matrix.data[5] = c;
1357  return out_matrix;
1358 }
1359 
1368 KINLINE mat4 mat4_euler_xyz(f32 x_radians, f32 y_radians, f32 z_radians) {
1369  mat4 rx = mat4_euler_x(x_radians);
1370  mat4 ry = mat4_euler_y(y_radians);
1371  mat4 rz = mat4_euler_z(z_radians);
1372  mat4 out_matrix = mat4_mul(rx, ry);
1373  out_matrix = mat4_mul(out_matrix, rz);
1374  return out_matrix;
1375 }
1376 
1384  vec3 forward;
1385  forward.x = -matrix.data[2];
1386  forward.y = -matrix.data[6];
1387  forward.z = -matrix.data[10];
1388  vec3_normalize(&forward);
1389  return forward;
1390 }
1391 
1399  vec3 backward;
1400  backward.x = matrix.data[2];
1401  backward.y = matrix.data[6];
1402  backward.z = matrix.data[10];
1403  vec3_normalize(&backward);
1404  return backward;
1405 }
1406 
1414  vec3 up;
1415  up.x = matrix.data[1];
1416  up.y = matrix.data[5];
1417  up.z = matrix.data[9];
1418  vec3_normalize(&up);
1419  return up;
1420 }
1421 
1429  vec3 down;
1430  down.x = -matrix.data[1];
1431  down.y = -matrix.data[5];
1432  down.z = -matrix.data[9];
1433  vec3_normalize(&down);
1434  return down;
1435 }
1436 
1444  vec3 left;
1445  left.x = -matrix.data[0];
1446  left.y = -matrix.data[4];
1447  left.z = -matrix.data[8];
1448  vec3_normalize(&left);
1449  return left;
1450 }
1451 
1459  vec3 right;
1460  right.x = matrix.data[0];
1461  right.y = matrix.data[4];
1462  right.z = matrix.data[8];
1463  vec3_normalize(&right);
1464  return right;
1465 }
1466 
1475  return (vec3){v.x * m.data[0] + v.y * m.data[1] + v.z * m.data[2] + m.data[3],
1476  v.x * m.data[4] + v.y * m.data[5] + v.z * m.data[6] + m.data[7],
1477  v.x * m.data[8] + v.y * m.data[9] + v.z * m.data[10] +
1478  m.data[11]};
1479 }
1480 
1489  return (vec3){
1490  v.x * m.data[0] + v.y * m.data[4] + v.z * m.data[8] + m.data[12],
1491  v.x * m.data[1] + v.y * m.data[5] + v.z * m.data[9] + m.data[13],
1492  v.x * m.data[2] + v.y * m.data[6] + v.z * m.data[10] + m.data[14]};
1493 }
1494 
1503  return (vec4){
1504  v.x * m.data[0] + v.y * m.data[1] + v.z * m.data[2] + v.w * m.data[3],
1505  v.x * m.data[4] + v.y * m.data[5] + v.z * m.data[6] + v.w * m.data[7],
1506  v.x * m.data[8] + v.y * m.data[9] + v.z * m.data[10] + v.w * m.data[11],
1507  v.x * m.data[12] + v.y * m.data[13] + v.z * m.data[14] +
1508  v.w * m.data[15]};
1509 }
1510 
1519  return (vec4){
1520  v.x * m.data[0] + v.y * m.data[4] + v.z * m.data[8] + v.w * m.data[12],
1521  v.x * m.data[1] + v.y * m.data[5] + v.z * m.data[9] + v.w * m.data[13],
1522  v.x * m.data[2] + v.y * m.data[6] + v.z * m.data[10] + v.w * m.data[14],
1523  v.x * m.data[3] + v.y * m.data[7] + v.z * m.data[11] + v.w * m.data[15]};
1524 }
1525 
1526 // ------------------------------------------
1527 // Quaternion
1528 // ------------------------------------------
1529 
1535 KINLINE quat quat_identity(void) { return (quat){0, 0, 0, 1.0f}; }
1536 
1544  return ksqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
1545 }
1546 
1554  f32 normal = quat_normal(q);
1555  return (quat){q.x / normal, q.y / normal, q.z / normal, q.w / normal};
1556 }
1557 
1565 KINLINE quat quat_conjugate(quat q) { return (quat){-q.x, -q.y, -q.z, q.w}; }
1566 
1574 
1583  quat out_quaternion;
1584 
1585  out_quaternion.x =
1586  q_0.x * q_1.w + q_0.y * q_1.z - q_0.z * q_1.y + q_0.w * q_1.x;
1587 
1588  out_quaternion.y =
1589  -q_0.x * q_1.z + q_0.y * q_1.w + q_0.z * q_1.x + q_0.w * q_1.y;
1590 
1591  out_quaternion.z =
1592  q_0.x * q_1.y - q_0.y * q_1.x + q_0.z * q_1.w + q_0.w * q_1.z;
1593 
1594  out_quaternion.w =
1595  -q_0.x * q_1.x - q_0.y * q_1.y - q_0.z * q_1.z + q_0.w * q_1.w;
1596 
1597  return out_quaternion;
1598 }
1599 
1608  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;
1609 }
1610 
1611 KINLINE vec3 vec3_min(vec3 vector_0, vec3 vector_1) {
1612  return vec3_create(
1613  KMIN(vector_0.x, vector_1.y),
1614  KMIN(vector_0.y, vector_1.y),
1615  KMIN(vector_0.z, vector_1.z));
1616 }
1617 
1618 KINLINE vec3 vec3_max(vec3 vector_0, vec3 vector_1) {
1619  return vec3_create(
1620  KMAX(vector_0.x, vector_1.y),
1621  KMAX(vector_0.y, vector_1.y),
1622  KMAX(vector_0.z, vector_1.z));
1623 }
1624 
1626  return vec3_create(ksign(v.x), ksign(v.y), ksign(v.z));
1627 }
1628 
1630  vec3 u = vec3_create(q.x, q.y, q.z);
1631  f32 s = q.w;
1632 
1633  return vec3_add(
1634  vec3_add(
1635  vec3_mul_scalar(u, 2.0f * vec3_dot(u, v)),
1636  vec3_mul_scalar(v, (s * s - vec3_dot(u, u)))),
1637  vec3_mul_scalar(vec3_cross(u, v), 2.0f * s));
1638 }
1639 
1647  mat4 out_matrix = mat4_identity();
1648 
1649  // https://stackoverflow.com/questions/1556260/convert-quaternion-rotation-to-rotation-matrix
1650 
1651  quat n = quat_normalize(q);
1652 
1653  out_matrix.data[0] = 1.0f - 2.0f * n.y * n.y - 2.0f * n.z * n.z;
1654  out_matrix.data[1] = 2.0f * n.x * n.y - 2.0f * n.z * n.w;
1655  out_matrix.data[2] = 2.0f * n.x * n.z + 2.0f * n.y * n.w;
1656 
1657  out_matrix.data[4] = 2.0f * n.x * n.y + 2.0f * n.z * n.w;
1658  out_matrix.data[5] = 1.0f - 2.0f * n.x * n.x - 2.0f * n.z * n.z;
1659  out_matrix.data[6] = 2.0f * n.y * n.z - 2.0f * n.x * n.w;
1660 
1661  out_matrix.data[8] = 2.0f * n.x * n.z - 2.0f * n.y * n.w;
1662  out_matrix.data[9] = 2.0f * n.y * n.z + 2.0f * n.x * n.w;
1663  out_matrix.data[10] = 1.0f - 2.0f * n.x * n.x - 2.0f * n.y * n.y;
1664 
1665  return out_matrix;
1666 }
1667 
1677  mat4 out_matrix;
1678 
1679  f32 *o = out_matrix.data;
1680  o[0] = (q.x * q.x) - (q.y * q.y) - (q.z * q.z) + (q.w * q.w);
1681  o[1] = 2.0f * ((q.x * q.y) + (q.z * q.w));
1682  o[2] = 2.0f * ((q.x * q.z) - (q.y * q.w));
1683  o[3] = center.x - center.x * o[0] - center.y * o[1] - center.z * o[2];
1684 
1685  o[4] = 2.0f * ((q.x * q.y) - (q.z * q.w));
1686  o[5] = -(q.x * q.x) + (q.y * q.y) - (q.z * q.z) + (q.w * q.w);
1687  o[6] = 2.0f * ((q.y * q.z) + (q.x * q.w));
1688  o[7] = center.y - center.x * o[4] - center.y * o[5] - center.z * o[6];
1689 
1690  o[8] = 2.0f * ((q.x * q.z) + (q.y * q.w));
1691  o[9] = 2.0f * ((q.y * q.z) - (q.x * q.w));
1692  o[10] = -(q.x * q.x) - (q.y * q.y) + (q.z * q.z) + (q.w * q.w);
1693  o[11] = center.z - center.x * o[8] - center.y * o[9] - center.z * o[10];
1694 
1695  o[12] = 0.0f;
1696  o[13] = 0.0f;
1697  o[14] = 0.0f;
1698  o[15] = 1.0f;
1699  return out_matrix;
1700 }
1701 
1710 KINLINE quat quat_from_axis_angle(vec3 axis, f32 angle, b8 normalize) {
1711  const f32 half_angle = 0.5f * angle;
1712  f32 s = ksin(half_angle);
1713  f32 c = kcos(half_angle);
1714 
1715  quat q = (quat){s * axis.x, s * axis.y, s * axis.z, c};
1716  if (normalize) {
1717  return quat_normalize(q);
1718  }
1719  return q;
1720 }
1721 
1732 KINLINE quat quat_slerp(quat q_0, quat q_1, f32 percentage) {
1733  quat out_quaternion;
1734  // Source: https://en.wikipedia.org/wiki/Slerp
1735  // Only unit quaternions are valid rotations.
1736  // Normalize to avoid undefined behavior.
1737  quat v0 = quat_normalize(q_0);
1738  quat v1 = quat_normalize(q_1);
1739 
1740  // Compute the cosine of the angle between the two vectors.
1741  f32 dot = quat_dot(v0, v1);
1742 
1743  // If the dot product is negative, slerp won't take
1744  // the shorter path. Note that v1 and -v1 are equivalent when
1745  // the negation is applied to all four components. Fix by
1746  // reversing one quaternion.
1747  if (dot < 0.0f) {
1748  v1.x = -v1.x;
1749  v1.y = -v1.y;
1750  v1.z = -v1.z;
1751  v1.w = -v1.w;
1752  dot = -dot;
1753  }
1754 
1755  const f32 DOT_THRESHOLD = 0.9995f;
1756  if (dot > DOT_THRESHOLD) {
1757  // If the inputs are too close for comfort, linearly interpolate
1758  // and normalize the result.
1759  out_quaternion = (quat){v0.x + ((v1.x - v0.x) * percentage),
1760  v0.y + ((v1.y - v0.y) * percentage),
1761  v0.z + ((v1.z - v0.z) * percentage),
1762  v0.w + ((v1.w - v0.w) * percentage)};
1763 
1764  return quat_normalize(out_quaternion);
1765  }
1766 
1767  // Since dot is in range [0, DOT_THRESHOLD], acos is safe
1768  f32 theta_0 = kacos(dot); // theta_0 = angle between input vectors
1769  f32 theta = theta_0 * percentage; // theta = angle between v0 and result
1770  f32 sin_theta = ksin(theta); // compute this value only once
1771  f32 sin_theta_0 = ksin(theta_0); // compute this value only once
1772 
1773  f32 s0 =
1774  kcos(theta) -
1775  dot * sin_theta / sin_theta_0; // == sin(theta_0 - theta) / sin(theta_0)
1776  f32 s1 = sin_theta / sin_theta_0;
1777 
1778  return (quat){(v0.x * s0) + (v1.x * s1), (v0.y * s0) + (v1.y * s1),
1779  (v0.z * s0) + (v1.z * s1), (v0.w * s0) + (v1.w * s1)};
1780 }
1781 
1788 KINLINE f32 deg_to_rad(f32 degrees) { return degrees * K_DEG2RAD_MULTIPLIER; }
1789 
1796 KINLINE f32 rad_to_deg(f32 radians) { return radians * K_RAD2DEG_MULTIPLIER; }
1797 
1808 KINLINE f32 range_convert_f32(f32 value, f32 old_min, f32 old_max, f32 new_min,
1809  f32 new_max) {
1810  return (((value - old_min) * (new_max - new_min)) / (old_max - old_min)) +
1811  new_min;
1812 }
1813 
1822 KINLINE void rgbu_to_u32(u32 r, u32 g, u32 b, u32 *out_u32) {
1823  *out_u32 = (((r & 0x0FF) << 16) | ((g & 0x0FF) << 8) | (b & 0x0FF));
1824 }
1825 
1834 KINLINE void u32_to_rgb(u32 rgbu, u32 *out_r, u32 *out_g, u32 *out_b) {
1835  *out_r = (rgbu >> 16) & 0x0FF;
1836  *out_g = (rgbu >> 8) & 0x0FF;
1837  *out_b = (rgbu)&0x0FF;
1838 }
1839 
1849 KINLINE void rgb_u32_to_vec3(u32 r, u32 g, u32 b, vec3 *out_v) {
1850  out_v->r = r / 255.0f;
1851  out_v->g = g / 255.0f;
1852  out_v->b = b / 255.0f;
1853 }
1854 
1863 KINLINE void vec3_to_rgb_u32(vec3 v, u32 *out_r, u32 *out_g, u32 *out_b) {
1864  *out_r = v.r * 255;
1865  *out_g = v.g * 255;
1866  *out_b = v.b * 255;
1867 }
1868 
1870 
1887 KAPI frustum frustum_create(const vec3 *position, const vec3 *forward,
1888  const vec3 *right, const vec3 *up, f32 aspect,
1889  f32 fov, f32 near, f32 far);
1890 
1899 KAPI f32 plane_signed_distance(const plane_3d *p, const vec3 *position);
1900 
1911 KAPI b8 plane_intersects_sphere(const plane_3d *p, const vec3 *center,
1912  f32 radius);
1913 
1926  f32 radius);
1927 
1939 KAPI b8 plane_intersects_aabb(const plane_3d *p, const vec3 *center,
1940  const vec3 *extents);
1941 
1953 KAPI b8 frustum_intersects_aabb(const frustum *f, const vec3 *center,
1954  const vec3 *extents);
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:177
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:236
signed int i32
Signed 32-bit integer.
Definition: defines.h:39
#define KMAX(x, y)
Definition: defines.h:237
#define KINLINE
Inline qualifier.
Definition: defines.h:208
#define KCLAMP(value, min, max)
Clamps value to a range of min and max (inclusive).
Definition: defines.h:188
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:28
KINLINE mat4 mat4_inverse(mat4 matrix)
Creates and returns an inverse of the provided matrix.
Definition: kmath.h:1203
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:665
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:1565
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:313
KINLINE vec4 vec4_one(void)
Creates and returns a 4-component vector with all components set to 1.0f.
Definition: kmath.h:804
KINLINE mat4 mat4_mul(mat4 matrix_0, mat4 matrix_1)
Returns the result of multiplying matrix_0 and matrix_1.
Definition: kmath.h:1020
KINLINE vec3 vec3_sign(vec3 v)
Definition: kmath.h:1625
KINLINE vec3 vec3_back(void)
Creates and returns a 3-component vector pointing backward (0, 0, 1).
Definition: kmath.h:520
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:887
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:262
KINLINE mat4 mat4_translation(vec3 position)
Creates and returns a translation matrix from the given position.
Definition: kmath.h:1283
KINLINE vec2 vec2_up(void)
Creates and returns a 2-component vector pointing up (0, 1).
Definition: kmath.h:267
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:1822
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:438
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:476
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:1078
KINLINE vec4 vec4_mul_mat4(vec4 v, mat4 m)
Performs v * m.
Definition: kmath.h:1518
KINLINE vec3 vec3_max(vec3 vector_0, vec3 vector_1)
Definition: kmath.h:1618
KAPI frustum frustum_create(const vec3 *position, const vec3 *forward, const vec3 *right, 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 f32 vec2_distance(vec2 vector_0, vec2 vector_1)
Returns the distance between vector_0 and vector_1.
Definition: kmath.h:425
KINLINE mat4 mat4_identity(void)
Creates and returns an identity matrix:
Definition: kmath.h:1003
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:1101
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:82
KINLINE vec3 mat4_right(mat4 matrix)
Returns a right vector relative to the provided matrix.
Definition: kmath.h:1458
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:954
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:921
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:302
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:1297
KINLINE vec3 vec3_forward(void)
Creates and returns a 3-component vector pointing forward (0, 0, -1).
Definition: kmath.h:515
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:901
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 mat4_backward(mat4 matrix)
Returns a backward vector relative to the provided matrix.
Definition: kmath.h:1398
KINLINE void vec2_normalize(vec2 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:379
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:553
KINLINE vec2 vec2_right(void)
Creates and returns a 2-component vector pointing right (1, 0).
Definition: kmath.h:282
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:337
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:1050
KINLINE quat quat_inverse(quat q)
Returns an inverse copy of the provided quaternion.
Definition: kmath.h:1573
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:578
KINLINE f32 vec3_length(vec3 vector)
Returns the length of the provided vector.
Definition: kmath.h:613
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:541
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:325
KINLINE vec3 mat4_mul_vec3(mat4 m, vec3 v)
Performs m * v.
Definition: kmath.h:1474
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:1849
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:245
KINLINE f32 ksmoothstep(f32 edge_0, f32 edge_1, f32 x)
Perform Hermite interpolation between two values.
Definition: kmath.h:221
#define K_RAD2DEG_MULTIPLIER
A multiplier used to convert radians to degrees.
Definition: kmath.h:56
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:1676
KINLINE vec2 vec2_down(void)
Creates and returns a 2-component vector pointing down (0, -1).
Definition: kmath.h:272
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:176
KINLINE vec3 mat4_left(mat4 matrix)
Returns a left vector relative to the provided matrix.
Definition: kmath.h:1443
KINLINE f32 deg_to_rad(f32 degrees)
Converts provided degrees to radians.
Definition: kmath.h:1788
KINLINE f32 vec3_length_squared(vec3 vector)
Returns the squared length of the provided vector.
Definition: kmath.h:603
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:843
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:813
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:859
KINLINE quat quat_normalize(quat q)
Returns a normalized copy of the provided quaternion.
Definition: kmath.h:1553
KINLINE f32 vec2_length_squared(vec2 vector)
Returns the squared length of the provided vector.
Definition: kmath.h:360
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:1311
KINLINE vec4 vec4_normalized(vec4 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:935
KINLINE vec2 vec2_normalized(vec2 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:391
KINLINE vec3 vec3_from_vec4(vec4 vector)
Returns a new vec3 containing the x, y and z components of the supplied vec4, essentially dropping th...
Definition: kmath.h:464
KINLINE f32 quat_dot(quat q_0, quat q_1)
Calculates the dot product of the provided quaternions.
Definition: kmath.h:1607
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:455
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:784
KINLINE void vec3_normalize(vec3 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:622
KINLINE vec3 mat4_down(mat4 matrix)
Returns a downward vector relative to the provided matrix.
Definition: kmath.h:1428
KINLINE vec4 vec4_zero(void)
Creates and returns a 4-component vector with all components set to 0.0f.
Definition: kmath.h:798
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:350
KINLINE f32 vec2_length(vec2 vector)
Returns the length of the provided vector.
Definition: kmath.h:370
KINLINE f32 quat_normal(quat q)
Returns the normal of the provided quaternion.
Definition: kmath.h:1543
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:291
KINLINE mat4 mat4_euler_y(f32 angle_radians)
Creates a rotation matrix from the provided y angle.
Definition: kmath.h:1329
KINLINE vec3 vec3_min(vec3 vector_0, vec3 vector_1)
Definition: kmath.h:1611
KINLINE f32 mat4_determinant(mat4 matrix)
Calculates the determinant of the given matrix.
Definition: kmath.h:1165
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:718
KINLINE mat4 mat4_transposed(mat4 matrix)
Returns a transposed copy of the provided matrix (rows->colums)
Definition: kmath.h:1138
KINLINE quat quat_identity(void)
Creates an identity quaternion.
Definition: kmath.h:1535
#define K_FLOAT_EPSILON
Smallest positive number where 1.0 + FLOAT_EPSILON != 0.
Definition: kmath.h:71
KINLINE f32 rad_to_deg(f32 radians)
Converts provided radians to degrees.
Definition: kmath.h:1796
KINLINE quat quat_mul(quat q_0, quat q_1)
Multiplies the provided quaternions.
Definition: kmath.h:1582
KINLINE mat4 mat4_euler_z(f32 angle_radians)
Creates a rotation matrix from the provided z angle.
Definition: kmath.h:1347
KINLINE vec3 vec3_up(void)
Creates and returns a 3-component vector pointing up (0, 1, 0).
Definition: kmath.h:495
KINLINE vec3 vec3_mul_mat4(vec3 v, mat4 m)
Performs v * m.
Definition: kmath.h:1488
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:752
KINLINE vec3 vec3_down(void)
Creates and returns a 3-component vector pointing down (0, -1, 0).
Definition: kmath.h:500
KINLINE vec2 vec2_zero(void)
Creates and returns a 2-component vector with all components set to 0.0f.
Definition: kmath.h:256
KINLINE vec3 mat4_forward(mat4 matrix)
Returns a forward vector relative to the provided matrix.
Definition: kmath.h:1383
KINLINE f32 ksign(f32 x)
Returns 0.0f if x == 0.0f, -1.0f if negative, otherwise 1.0f.
Definition: kmath.h:96
KINLINE vec2 vec2_left(void)
Creates and returns a 2-component vector pointing left (-1, 0).
Definition: kmath.h:277
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:406
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:566
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:1863
KINLINE f32 kstep(f32 edge, f32 x)
Compares x to edge, returning 0 if x < edge; otherwise 1.0f;.
Definition: kmath.h:102
KINLINE f32 vec4_length(vec4 vector)
Returns the length of the provided vector.
Definition: kmath.h:912
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:1368
KINLINE vec3 vec3_left(void)
Creates and returns a 3-component vector pointing left (-1, 0, 0).
Definition: kmath.h:505
KINLINE mat4 quat_to_mat4(quat q)
Creates a rotation matrix from the given quaternion.
Definition: kmath.h:1646
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:230
KINLINE vec3 mat4_up(mat4 matrix)
Returns a upward vector relative to the provided matrix.
Definition: kmath.h:1413
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:772
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:871
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:828
KINLINE vec3 vec3_rotate(vec3 v, quat q)
Definition: kmath.h:1629
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:681
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:510
KINLINE quat quat_from_axis_angle(vec3 axis, f32 angle, b8 normalize)
Creates a quaternion from the given axis and angle.
Definition: kmath.h:1710
KINLINE vec3 vec3_one(void)
Creates and returns a 3-component vector with all components set to 1.0f.
Definition: kmath.h:490
KINLINE vec4 mat4_mul_vec4(mat4 m, vec4 v)
Performs m * v.
Definition: kmath.h:1502
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:971
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:648
KINLINE f32 vec3_distance(vec3 vector_0, vec3 vector_1)
Returns the distance between vector_0 and vector_1.
Definition: kmath.h:704
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:1834
KINLINE vec3 vec3_transform(vec3 v, f32 w, mat4 m)
Transform v by m.
Definition: kmath.h:731
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:592
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:1732
KINLINE vec3 vec3_zero(void)
Creates and returns a 3-component vector with all components set to 0.0f.
Definition: kmath.h:484
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:529
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:1808
#define K_DEG2RAD_MULTIPLIER
A multiplier used to convert degrees to radians.
Definition: kmath.h:53
KINLINE vec3 vec3_normalized(vec3 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:635
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
Definition: math_types.h:244
Definition: math_types.h:239
A 3x3 matrix.
Definition: math_types.h:141
f32 data[12]
The matrix elements.
Definition: math_types.h:143
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:147
f32 data[16]
The matrix elements.
Definition: math_types.h:149
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 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 x
The first element.
Definition: math_types.h:96
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