20 #define K_PI 3.14159265358979323846f
23 #define K_2PI (2.0f * K_PI)
26 #define K_4PI (4.0f * K_PI)
29 #define K_HALF_PI (0.5f * K_PI)
32 #define K_QUARTER_PI (0.25f * K_PI)
35 #define K_ONE_OVER_PI (1.0f / K_PI)
38 #define K_ONE_OVER_TWO_PI (1.0f / K_2PI)
41 #define K_SQRT_TWO 1.41421356237309504880f
44 #define K_SQRT_THREE 1.73205080756887729352f
47 #define K_SQRT_ONE_OVER_TWO 0.70710678118654752440f
50 #define K_SQRT_ONE_OVER_THREE 0.57735026918962576450f
53 #define K_DEG2RAD_MULTIPLIER (K_PI / 180.0f)
56 #define K_RAD2DEG_MULTIPLIER (180.0f / K_PI)
59 #define K_SEC_TO_US_MULTIPLIER (1000.0f * 1000.0f)
62 #define K_SEC_TO_MS_MULTIPLIER 1000.0f
65 #define K_MS_TO_SEC_MULTIPLIER 0.001f
68 #define K_INFINITY (1e30f * 1e30f)
71 #define K_FLOAT_EPSILON 1.192092896e-07f
88 #define KSWAP(type, a, b) \
97 return x == 0.0f ? 0.0f : x < 0.0f ? -1.0f
103 return x < edge ? 0.0f : 1.0f;
177 return (value != 0) && ((value & (value - 1)) == 0);
222 f32 t =
KCLAMP((x - edge_0) / (edge_1 - edge_0), 0.0f, 1.0f);
223 return t * t * (3.0 - 2.0 * t);
292 return (
vec2){vector_0.
x + vector_1.
x, vector_0.
y + vector_1.
y};
303 return (
vec2){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y};
314 return (
vec2){vector_0.
x * vector_1.
x, vector_0.
y * vector_1.
y};
326 return (
vec2){vector_0.
x * scalar, vector_0.
y * scalar};
339 vector_0.
x * vector_1.
x + vector_2.
x,
340 vector_0.
y * vector_1.
y + vector_2.
y};
351 return (
vec2){vector_0.
x / vector_1.
x, vector_0.
y / vector_1.
y};
361 return vector.
x * vector.
x + vector.
y * vector.
y;
407 if (
kabs(vector_0.
x - vector_1.
x) > tolerance) {
411 if (
kabs(vector_0.
y - vector_1.
y) > tolerance) {
426 vec2 d = (
vec2){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y};
439 vec2 d = (
vec2){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y};
465 return (
vec3){vector.
x, vector.
y, vector.
z};
477 return (
vec4){vector.
x, vector.
y, vector.
z, w};
530 return (
vec3){vector_0.
x + vector_1.
x, vector_0.
y + vector_1.
y,
531 vector_0.
z + vector_1.
z};
542 return (
vec3){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y,
543 vector_0.
z - vector_1.
z};
554 return (
vec3){vector_0.
x * vector_1.
x, vector_0.
y * vector_1.
y,
555 vector_0.
z * vector_1.
z};
567 return (
vec3){vector_0.
x * scalar, vector_0.
y * scalar, vector_0.
z * scalar};
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};
593 return (
vec3){vector_0.
x / vector_1.
x, vector_0.
y / vector_1.
y,
594 vector_0.
z / vector_1.
z};
604 return vector.
x * vector.
x + vector.
y * vector.
y + vector.
z * vector.
z;
650 p += vector_0.
x * vector_1.
x;
651 p += vector_0.
y * vector_1.
y;
652 p += vector_0.
z * vector_1.
z;
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};
682 if (
kabs(vector_0.
x - vector_1.
x) > tolerance) {
686 if (
kabs(vector_0.
y - vector_1.
y) > tolerance) {
690 if (
kabs(vector_0.
z - vector_1.
z) > tolerance) {
705 vec3 d = (
vec3){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y,
706 vector_0.
z - vector_1.
z};
719 vec3 d = (
vec3){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y,
720 vector_0.
z - vector_1.
z};
754 #if defined(KUSE_SIMD)
755 out_vector.data = _mm_setr_ps(x, y, z, w);
773 return (
vec3){vector.
x, vector.
y, vector.
z};
785 #if defined(KUSE_SIMD)
787 out_vector.data = _mm_setr_ps(x, y, z, w);
790 return (
vec4){vector.
x, vector.
y, vector.
z, w};
815 for (
u64 i = 0; i < 4; ++i) {
830 for (
u64 i = 0; i < 4; ++i) {
845 for (
u64 i = 0; i < 4; ++i) {
860 return (
vec4){vector_0.
x * scalar, vector_0.
y * scalar, vector_0.
z * scalar, vector_0.
w * scalar};
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,
889 for (
u64 i = 0; i < 4; ++i) {
902 return vector.
x * vector.
x + vector.
y * vector.
y + vector.
z * vector.
z +
957 p = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
972 if (
kabs(vector_0.
x - vector_1.
x) > tolerance) {
976 if (
kabs(vector_0.
y - vector_1.
y) > tolerance) {
980 if (
kabs(vector_0.
z - vector_1.
z) > tolerance) {
984 if (
kabs(vector_0.
w - vector_1.
w) > tolerance) {
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;
1023 const f32 *m1_ptr = matrix_0.
data;
1024 const f32 *m2_ptr = matrix_1.
data;
1025 f32 *dst_ptr = out_matrix.
data;
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];
1051 f32 near_clip,
f32 far_clip) {
1054 f32 lr = 1.0f / (left - right);
1055 f32 bt = 1.0f / (bottom - top);
1056 f32 nf = 1.0f / (near_clip - far_clip);
1058 out_matrix.
data[0] = -2.0f * lr;
1059 out_matrix.
data[5] = -2.0f * bt;
1060 out_matrix.
data[10] = 2.0f * nf;
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;
1080 f32 half_tan_fov =
ktan(fov_radians * 0.5f);
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));
1104 z_axis.
x = target.
x - position.
x;
1105 z_axis.
y = target.
y - position.
y;
1106 z_axis.
z = target.
z - position.
z;
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;
1127 out_matrix.
data[15] = 1.0f;
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];
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];
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]);
1193 f32 determinant = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * o[3]);
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];
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]);
1243 f32 d = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * 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]));
1285 out_matrix.
data[12] = position.
x;
1286 out_matrix.
data[13] = position.
y;
1287 out_matrix.
data[14] = position.
z;
1299 out_matrix.
data[0] = scale.
x;
1300 out_matrix.
data[5] = scale.
y;
1301 out_matrix.
data[10] = scale.
z;
1316 out_matrix.
data[5] = c;
1317 out_matrix.
data[6] = s;
1318 out_matrix.
data[9] = -s;
1319 out_matrix.
data[10] = c;
1334 out_matrix.
data[0] = c;
1335 out_matrix.
data[2] = -s;
1336 out_matrix.
data[8] = s;
1337 out_matrix.
data[10] = c;
1353 out_matrix.
data[0] = c;
1354 out_matrix.
data[1] = s;
1355 out_matrix.
data[4] = -s;
1356 out_matrix.
data[5] = c;
1373 out_matrix =
mat4_mul(out_matrix, rz);
1385 forward.
x = -matrix.
data[2];
1386 forward.
y = -matrix.
data[6];
1387 forward.
z = -matrix.
data[10];
1400 backward.
x = matrix.
data[2];
1401 backward.
y = matrix.
data[6];
1402 backward.
z = matrix.
data[10];
1415 up.
x = matrix.
data[1];
1416 up.
y = matrix.
data[5];
1417 up.
z = matrix.
data[9];
1430 down.
x = -matrix.
data[1];
1431 down.
y = -matrix.
data[5];
1432 down.
z = -matrix.
data[9];
1445 left.
x = -matrix.
data[0];
1446 left.
y = -matrix.
data[4];
1447 left.
z = -matrix.
data[8];
1460 right.
x = matrix.
data[0];
1461 right.
y = matrix.
data[4];
1462 right.
z = matrix.
data[8];
1544 return ksqrt(q.
x * q.
x + q.
y * q.
y + q.
z * q.
z + q.
w * q.
w);
1555 return (
quat){q.
x / normal, q.
y / normal, q.
z / normal, q.
w / normal};
1583 quat out_quaternion;
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;
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;
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;
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;
1597 return out_quaternion;
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;
1613 KMIN(vector_0.
x, vector_1.
y),
1614 KMIN(vector_0.
y, vector_1.
y),
1615 KMIN(vector_0.
z, vector_1.
z));
1620 KMAX(vector_0.
x, vector_1.
y),
1621 KMAX(vector_0.
y, vector_1.
y),
1622 KMAX(vector_0.
z, vector_1.
z));
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;
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;
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;
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];
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];
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];
1711 const f32 half_angle = 0.5f * angle;
1715 quat q = (
quat){s * axis.
x, s * axis.
y, s * axis.
z, c};
1733 quat out_quaternion;
1755 const f32 DOT_THRESHOLD = 0.9995f;
1756 if (dot > DOT_THRESHOLD) {
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)};
1769 f32 theta = theta_0 * percentage;
1771 f32 sin_theta_0 =
ksin(theta_0);
1775 dot * sin_theta / sin_theta_0;
1776 f32 s1 = sin_theta / sin_theta_0;
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)};
1810 return (((value - old_min) * (new_max - new_min)) / (old_max - old_min)) +
1823 *out_u32 = (((r & 0x0FF) << 16) | ((g & 0x0FF) << 8) | (b & 0x0FF));
1835 *out_r = (rgbu >> 16) & 0x0FF;
1836 *out_g = (rgbu >> 8) & 0x0FF;
1837 *out_b = (rgbu)&0x0FF;
1850 out_v->
r = r / 255.0f;
1851 out_v->
g = g / 255.0f;
1852 out_v->
b = b / 255.0f;
1940 const vec3 *extents);
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