22 #define K_PI 3.14159265358979323846f
25 #define K_2PI (2.0f * K_PI)
28 #define K_4PI (4.0f * K_PI)
31 #define K_HALF_PI (0.5f * K_PI)
34 #define K_QUARTER_PI (0.25f * K_PI)
37 #define K_ONE_OVER_PI (1.0f / K_PI)
40 #define K_ONE_OVER_TWO_PI (1.0f / K_2PI)
43 #define K_SQRT_TWO 1.41421356237309504880f
46 #define K_SQRT_THREE 1.73205080756887729352f
49 #define K_SQRT_ONE_OVER_TWO 0.70710678118654752440f
52 #define K_SQRT_ONE_OVER_THREE 0.57735026918962576450f
55 #define K_DEG2RAD_MULTIPLIER (K_PI / 180.0f)
58 #define K_RAD2DEG_MULTIPLIER (180.0f / K_PI)
61 #define K_SEC_TO_US_MULTIPLIER (1000.0f * 1000.0f)
64 #define K_SEC_TO_MS_MULTIPLIER 1000.0f
67 #define K_MS_TO_SEC_MULTIPLIER 0.001f
70 #define K_INFINITY (1e30f * 1e30f)
73 #define K_FLOAT_EPSILON 1.192092896e-07f
75 #define K_FLOAT_MAX 3.40282e+38F
76 #define K_FLOAT_MIN -K_FLOAT_MAX
95 return x == 0.0f ? 0.0f : x < 0.0f ? -1.0f
101 return x < edge ? 0.0f : 1.0f;
241 return a + t * (b - a);
251 return (value != 0) && ((value & (value - 1)) == 0);
302 f32 t =
KCLAMP((x - edge_0) / (edge_1 - edge_0), 0.0f, 1.0f);
303 return t * t * (3.0 - 2.0 * t);
387 return (
vec2){vector_0.
x + vector_1.
x, vector_0.
y + vector_1.
y};
398 return (
vec2){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y};
409 return (
vec2){vector_0.
x * vector_1.
x, vector_0.
y * vector_1.
y};
421 return (
vec2){vector_0.
x * scalar, vector_0.
y * scalar};
434 vector_0.
x * vector_1.
x + vector_2.
x,
435 vector_0.
y * vector_1.
y + vector_2.
y};
446 return (
vec2){vector_0.
x / vector_1.
x, vector_0.
y / vector_1.
y};
456 return vector.
x * vector.
x + vector.
y * vector.
y;
502 if (
kabs(vector_0.
x - vector_1.
x) > tolerance) {
506 if (
kabs(vector_0.
y - vector_1.
y) > tolerance) {
522 for (
u8 i = 0; i < 2; ++i) {
550 for (
u8 i = 0; i < 2; ++i) {
577 vec2 d = (
vec2){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y};
590 vec2 d = (
vec2){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y};
596 KMIN(vector_0.
x, vector_1.
x),
597 KMIN(vector_0.
y, vector_1.
y));
602 KMAX(vector_0.
x, vector_1.
x),
603 KMAX(vector_0.
y, vector_1.
y));
618 #define vec3_create(x, y, z) \
637 return (
vec3){vector.
x, vector.
y, vector.
z};
649 return (
vec3){vector.
x, vector.
y, z};
661 return (
vec4){vector.
x, vector.
y, vector.
z, w};
714 return (
vec3){vector_0.
x + vector_1.
x, vector_0.
y + vector_1.
y,
715 vector_0.
z + vector_1.
z};
726 return (
vec3){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y,
727 vector_0.
z - vector_1.
z};
738 return (
vec3){vector_0.
x * vector_1.
x, vector_0.
y * vector_1.
y,
739 vector_0.
z * vector_1.
z};
751 return (
vec3){vector_0.
x * scalar, vector_0.
y * scalar, vector_0.
z * scalar};
764 vector_0.
x * vector_1.
x + vector_2.
x,
765 vector_0.
y * vector_1.
y + vector_2.
y,
766 vector_0.
z * vector_1.
z + vector_2.
z};
777 return (
vec3){vector_0.
x / vector_1.
x, vector_0.
y / vector_1.
y,
778 vector_0.
z / vector_1.
z};
783 for (
u64 i = 0; i < 3; ++i) {
796 return vector.
x * vector.
x + vector.
y * vector.
y + vector.
z * vector.
z;
842 p += vector_0.
x * vector_1.
x;
843 p += vector_0.
y * vector_1.
y;
844 p += vector_0.
z * vector_1.
z;
858 return (
vec3){vector_0.
y * vector_1.
z - vector_0.
z * vector_1.
y,
859 vector_0.
z * vector_1.
x - vector_0.
x * vector_1.
z,
860 vector_0.
x * vector_1.
y - vector_0.
y * vector_1.
x};
874 if (
kabs(vector_0.
x - vector_1.
x) > tolerance) {
878 if (
kabs(vector_0.
y - vector_1.
y) > tolerance) {
882 if (
kabs(vector_0.
z - vector_1.
z) > tolerance) {
898 for (
u8 i = 0; i < 3; ++i) {
926 for (
u8 i = 0; i < 3; ++i) {
953 vec3 d = (
vec3){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y,
954 vector_0.
z - vector_1.
z};
967 vec3 d = (
vec3){vector_0.
x - vector_1.
x, vector_0.
y - vector_1.
y,
968 vector_0.
z - vector_1.
z};
981 if (length_sq == 0.0f) {
1006 KMIN(vector_0.
x, vector_1.
x),
1007 KMIN(vector_0.
y, vector_1.
y),
1008 KMIN(vector_0.
z, vector_1.
z));
1013 KMAX(vector_0.
x, vector_1.
x),
1014 KMAX(vector_0.
y, vector_1.
y),
1015 KMAX(vector_0.
z, vector_1.
z));
1048 #if defined(KUSE_SIMD)
1049 out_vector.data = _mm_setr_ps(x, y, z, w);
1075 return (
vec3){vector.
x, vector.
y, vector.
z};
1087 #if defined(KUSE_SIMD)
1089 out_vector.data = _mm_setr_ps(x, y, z, w);
1092 return (
vec4){vector.
x, vector.
y, vector.
z, w};
1117 for (
u64 i = 0; i < 4; ++i) {
1132 for (
u64 i = 0; i < 4; ++i) {
1147 for (
u64 i = 0; i < 4; ++i) {
1162 return (
vec4){vector_0.
x * scalar, vector_0.
y * scalar, vector_0.
z * scalar, vector_0.
w * scalar};
1175 vector_0.
x * vector_1.
x + vector_2.
x,
1176 vector_0.
y * vector_1.
y + vector_2.
y,
1177 vector_0.
z * vector_1.
z + vector_2.
z,
1178 vector_0.
w * vector_1.
w + vector_2.
w,
1191 for (
u64 i = 0; i < 4; ++i) {
1199 for (
u64 i = 0; i < 4; ++i) {
1212 return vector.
x * vector.
x + vector.
y * vector.
y + vector.
z * vector.
z +
1213 vector.
w * vector.
w;
1233 vector->
x /= length;
1234 vector->
y /= length;
1235 vector->
z /= length;
1236 vector->
w /= length;
1267 p = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
1282 if (
kabs(vector_0.
x - vector_1.
x) > tolerance) {
1286 if (
kabs(vector_0.
y - vector_1.
y) > tolerance) {
1290 if (
kabs(vector_0.
z - vector_1.
z) > tolerance) {
1294 if (
kabs(vector_0.
w - vector_1.
w) > tolerance) {
1310 for (
u8 i = 0; i < 4; ++i) {
1338 for (
u8 i = 0; i < 4; ++i) {
1372 out_matrix.
data[0] = 1.0f;
1373 out_matrix.
data[5] = 1.0f;
1374 out_matrix.
data[10] = 1.0f;
1375 out_matrix.
data[15] = 1.0f;
1389 const f32* m1_ptr = matrix_0.
data;
1390 const f32* m2_ptr = matrix_1.
data;
1391 f32* dst_ptr = out_matrix.
data;
1393 for (
i32 i = 0; i < 4; ++i) {
1394 for (
i32 j = 0; j < 4; ++j) {
1395 *dst_ptr = m1_ptr[0] * m2_ptr[0 + j] + m1_ptr[1] * m2_ptr[4 + j] +
1396 m1_ptr[2] * m2_ptr[8 + j] + m1_ptr[3] * m2_ptr[12 + j];
1417 f32 near_clip,
f32 far_clip) {
1420 f32 lr = 1.0f / (left - right);
1421 f32 bt = 1.0f / (bottom - top);
1422 f32 nf = 1.0f / (near_clip - far_clip);
1424 out_matrix.
data[0] = -2.0f * lr;
1425 out_matrix.
data[5] = -2.0f * bt;
1426 out_matrix.
data[10] = nf;
1428 out_matrix.
data[12] = (left + right) * lr;
1429 out_matrix.
data[13] = (top + bottom) * bt;
1430 out_matrix.
data[14] = -near_clip * nf;
1446 f32 half_tan_fov =
ktan(fov_radians * 0.5f);
1449 out_matrix.
data[0] = 1.0f / (aspect_ratio * half_tan_fov);
1450 out_matrix.
data[5] = 1.0f / half_tan_fov;
1451 out_matrix.
data[10] = far_clip / (near_clip - far_clip);
1452 out_matrix.
data[11] = -1.0f;
1453 out_matrix.
data[14] = (far_clip * near_clip) / (near_clip - far_clip);
1472 out_matrix.
data[0] = x_axis.
x;
1473 out_matrix.
data[1] = y_axis.
x;
1474 out_matrix.
data[2] = -z_axis.
x;
1475 out_matrix.
data[3] = 0;
1476 out_matrix.
data[4] = x_axis.
y;
1477 out_matrix.
data[5] = y_axis.
y;
1478 out_matrix.
data[6] = -z_axis.
y;
1479 out_matrix.
data[7] = 0;
1480 out_matrix.
data[8] = x_axis.
z;
1481 out_matrix.
data[9] = y_axis.
z;
1482 out_matrix.
data[10] = -z_axis.
z;
1483 out_matrix.
data[11] = 0;
1487 out_matrix.
data[15] = 1.0f;
1499 out_matrix.
data[0] = matrix.
data[0];
1500 out_matrix.
data[1] = matrix.
data[4];
1501 out_matrix.
data[2] = matrix.
data[8];
1502 out_matrix.
data[3] = matrix.
data[12];
1503 out_matrix.
data[4] = matrix.
data[1];
1504 out_matrix.
data[5] = matrix.
data[5];
1505 out_matrix.
data[6] = matrix.
data[9];
1506 out_matrix.
data[7] = matrix.
data[13];
1507 out_matrix.
data[8] = matrix.
data[2];
1508 out_matrix.
data[9] = matrix.
data[6];
1509 out_matrix.
data[10] = matrix.
data[10];
1510 out_matrix.
data[11] = matrix.
data[14];
1511 out_matrix.
data[12] = matrix.
data[3];
1512 out_matrix.
data[13] = matrix.
data[7];
1513 out_matrix.
data[14] = matrix.
data[11];
1514 out_matrix.
data[15] = matrix.
data[15];
1527 f32 t0 = m[10] * m[15];
1528 f32 t1 = m[14] * m[11];
1529 f32 t2 = m[6] * m[15];
1530 f32 t3 = m[14] * m[7];
1531 f32 t4 = m[6] * m[11];
1532 f32 t5 = m[10] * m[7];
1533 f32 t6 = m[2] * m[15];
1534 f32 t7 = m[14] * m[3];
1535 f32 t8 = m[2] * m[11];
1536 f32 t9 = m[10] * m[3];
1537 f32 t10 = m[2] * m[7];
1538 f32 t11 = m[6] * m[3];
1543 o[0] = (t0 * m[5] + t3 * m[9] + t4 * m[13]) -
1544 (t1 * m[5] + t2 * m[9] + t5 * m[13]);
1545 o[1] = (t1 * m[1] + t6 * m[9] + t9 * m[13]) -
1546 (t0 * m[1] + t7 * m[9] + t8 * m[13]);
1547 o[2] = (t2 * m[1] + t7 * m[5] + t10 * m[13]) -
1548 (t3 * m[1] + t6 * m[5] + t11 * m[13]);
1549 o[3] = (t5 * m[1] + t8 * m[5] + t11 * m[9]) -
1550 (t4 * m[1] + t9 * m[5] + t10 * m[9]);
1552 f32 determinant = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * o[3]);
1565 f32 t0 = m[10] * m[15];
1566 f32 t1 = m[14] * m[11];
1567 f32 t2 = m[6] * m[15];
1568 f32 t3 = m[14] * m[7];
1569 f32 t4 = m[6] * m[11];
1570 f32 t5 = m[10] * m[7];
1571 f32 t6 = m[2] * m[15];
1572 f32 t7 = m[14] * m[3];
1573 f32 t8 = m[2] * m[11];
1574 f32 t9 = m[10] * m[3];
1575 f32 t10 = m[2] * m[7];
1576 f32 t11 = m[6] * m[3];
1577 f32 t12 = m[8] * m[13];
1578 f32 t13 = m[12] * m[9];
1579 f32 t14 = m[4] * m[13];
1580 f32 t15 = m[12] * m[5];
1581 f32 t16 = m[4] * m[9];
1582 f32 t17 = m[8] * m[5];
1583 f32 t18 = m[0] * m[13];
1584 f32 t19 = m[12] * m[1];
1585 f32 t20 = m[0] * m[9];
1586 f32 t21 = m[8] * m[1];
1587 f32 t22 = m[0] * m[5];
1588 f32 t23 = m[4] * m[1];
1593 o[0] = (t0 * m[5] + t3 * m[9] + t4 * m[13]) -
1594 (t1 * m[5] + t2 * m[9] + t5 * m[13]);
1595 o[1] = (t1 * m[1] + t6 * m[9] + t9 * m[13]) -
1596 (t0 * m[1] + t7 * m[9] + t8 * m[13]);
1597 o[2] = (t2 * m[1] + t7 * m[5] + t10 * m[13]) -
1598 (t3 * m[1] + t6 * m[5] + t11 * m[13]);
1599 o[3] = (t5 * m[1] + t8 * m[5] + t11 * m[9]) -
1600 (t4 * m[1] + t9 * m[5] + t10 * m[9]);
1602 f32 d = 1.0f / (m[0] * o[0] + m[4] * o[1] + m[8] * o[2] + m[12] * o[3]);
1605 if (
kabs(d) < 1e-6f) {
1614 o[4] = d * ((t1 * m[4] + t2 * m[8] + t5 * m[12]) -
1615 (t0 * m[4] + t3 * m[8] + t4 * m[12]));
1616 o[5] = d * ((t0 * m[0] + t7 * m[8] + t8 * m[12]) -
1617 (t1 * m[0] + t6 * m[8] + t9 * m[12]));
1618 o[6] = d * ((t3 * m[0] + t6 * m[4] + t11 * m[12]) -
1619 (t2 * m[0] + t7 * m[4] + t10 * m[12]));
1620 o[7] = d * ((t4 * m[0] + t9 * m[4] + t10 * m[8]) -
1621 (t5 * m[0] + t8 * m[4] + t11 * m[8]));
1622 o[8] = d * ((t12 * m[7] + t15 * m[11] + t16 * m[15]) -
1623 (t13 * m[7] + t14 * m[11] + t17 * m[15]));
1624 o[9] = d * ((t13 * m[3] + t18 * m[11] + t21 * m[15]) -
1625 (t12 * m[3] + t19 * m[11] + t20 * m[15]));
1626 o[10] = d * ((t14 * m[3] + t19 * m[7] + t22 * m[15]) -
1627 (t15 * m[3] + t18 * m[7] + t23 * m[15]));
1628 o[11] = d * ((t17 * m[3] + t20 * m[7] + t23 * m[11]) -
1629 (t16 * m[3] + t21 * m[7] + t22 * m[11]));
1630 o[12] = d * ((t14 * m[10] + t17 * m[14] + t13 * m[6]) -
1631 (t16 * m[14] + t12 * m[6] + t15 * m[10]));
1632 o[13] = d * ((t20 * m[14] + t12 * m[2] + t19 * m[10]) -
1633 (t18 * m[10] + t21 * m[14] + t13 * m[2]));
1634 o[14] = d * ((t18 * m[6] + t23 * m[14] + t15 * m[2]) -
1635 (t22 * m[14] + t14 * m[2] + t19 * m[6]));
1636 o[15] = d * ((t22 * m[10] + t16 * m[2] + t21 * m[6]) -
1637 (t20 * m[6] + t23 * m[10] + t17 * m[2]));
1650 out_matrix.
data[12] = position.
x;
1651 out_matrix.
data[13] = position.
y;
1652 out_matrix.
data[14] = position.
z;
1664 out_matrix.
data[0] = scale.
x;
1665 out_matrix.
data[5] = scale.
y;
1666 out_matrix.
data[10] = scale.
z;
1681 out_matrix.
data[0] = (1.0f - 2.0f * (r.
y * r.
y + r.
z * r.
z)) * s.
x;
1682 out_matrix.
data[1] = (r.
x * r.
y + r.
z * r.
w) * s.
x * 2.0f;
1683 out_matrix.
data[2] = (r.
x * r.
z - r.
y * r.
w) * s.
x * 2.0f;
1684 out_matrix.
data[3] = 0.0f;
1685 out_matrix.
data[4] = (r.
x * r.
y - r.
z * r.
w) * s.
y * 2.0f;
1686 out_matrix.
data[5] = (1.0f - 2.0f * (r.
x * r.
x + r.
z * r.
z)) * s.
y;
1687 out_matrix.
data[6] = (r.
y * r.
z + r.
x * r.
w) * s.
y * 2.0f;
1688 out_matrix.
data[7] = 0.0f;
1689 out_matrix.
data[8] = (r.
x * r.
z + r.
y * r.
w) * s.
z * 2.0f;
1690 out_matrix.
data[9] = (r.
y * r.
z - r.
x * r.
w) * s.
z * 2.0f;
1691 out_matrix.
data[10] = (1.0f - 2.0f * (r.
x * r.
x + r.
y * r.
y)) * s.
z;
1692 out_matrix.
data[11] = 0.0f;
1693 out_matrix.
data[12] = t.
x;
1694 out_matrix.
data[13] = t.
y;
1695 out_matrix.
data[14] = t.
z;
1696 out_matrix.
data[15] = 1.0f;
1712 out_matrix.
data[5] = c;
1713 out_matrix.
data[6] = s;
1714 out_matrix.
data[9] = -s;
1715 out_matrix.
data[10] = c;
1730 out_matrix.
data[0] = c;
1731 out_matrix.
data[2] = -s;
1732 out_matrix.
data[8] = s;
1733 out_matrix.
data[10] = c;
1749 out_matrix.
data[0] = c;
1750 out_matrix.
data[1] = s;
1751 out_matrix.
data[4] = -s;
1752 out_matrix.
data[5] = c;
1769 out_matrix =
mat4_mul(out_matrix, rz);
1781 forward.
x = -matrix.
data[8];
1782 forward.
y = -matrix.
data[9];
1783 forward.
z = -matrix.
data[10];
1796 backward.
x = matrix.
data[8];
1797 backward.
y = matrix.
data[9];
1798 backward.
z = matrix.
data[10];
1811 up.
x = matrix.
data[1];
1812 up.
y = matrix.
data[5];
1813 up.
z = matrix.
data[9];
1826 down.
x = -matrix.
data[1];
1827 down.
y = -matrix.
data[5];
1828 down.
z = -matrix.
data[9];
1841 left.
x = -matrix.
data[0];
1842 left.
y = -matrix.
data[1];
1843 left.
z = -matrix.
data[2];
1856 right.
x = matrix.
data[0];
1857 right.
y = matrix.
data[1];
1858 right.
z = matrix.
data[2];
1871 pos.
x = matrix.
data[12];
1872 pos.
y = matrix.
data[13];
1873 pos.
z = matrix.
data[14];
1968 return ksqrt(q.
x * q.
x + q.
y * q.
y + q.
z * q.
z + q.
w * q.
w);
1979 return (
quat){q.
x / normal, q.
y / normal, q.
z / normal, q.
w / normal};
2007 quat out_quaternion;
2010 q_0.
x * q_1.
w + q_0.
y * q_1.
z - q_0.
z * q_1.
y + q_0.
w * q_1.
x;
2013 -q_0.
x * q_1.
z + q_0.
y * q_1.
w + q_0.
z * q_1.
x + q_0.
w * q_1.
y;
2016 q_0.
x * q_1.
y - q_0.
y * q_1.
x + q_0.
z * q_1.
w + q_0.
w * q_1.
z;
2019 -q_0.
x * q_1.
x - q_0.
y * q_1.
y - q_0.
z * q_1.
z + q_0.
w * q_1.
w;
2021 return out_quaternion;
2032 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;
2049 out_matrix.
data[0] = 1.0f - 2.0f * n.
y * n.
y - 2.0f * n.
z * n.
z;
2050 out_matrix.
data[1] = 2.0f * n.
x * n.
y - 2.0f * n.
z * n.
w;
2051 out_matrix.
data[2] = 2.0f * n.
x * n.
z + 2.0f * n.
y * n.
w;
2053 out_matrix.
data[4] = 2.0f * n.
x * n.
y + 2.0f * n.
z * n.
w;
2054 out_matrix.
data[5] = 1.0f - 2.0f * n.
x * n.
x - 2.0f * n.
z * n.
z;
2055 out_matrix.
data[6] = 2.0f * n.
y * n.
z - 2.0f * n.
x * n.
w;
2057 out_matrix.
data[8] = 2.0f * n.
x * n.
z - 2.0f * n.
y * n.
w;
2058 out_matrix.
data[9] = 2.0f * n.
y * n.
z + 2.0f * n.
x * n.
w;
2059 out_matrix.
data[10] = 1.0f - 2.0f * n.
x * n.
x - 2.0f * n.
y * n.
y;
2128 float x = n.
x, y = n.
y, z = n.
z, w = n.
w;
2133 m.
data[0] = 1 - 2 * y * y - 2 * z * z;
2134 m.
data[1] = 2 * x * y + 2 * w * z;
2135 m.
data[2] = 2 * x * z - 2 * w * y;
2139 m.
data[4] = 2 * x * y - 2 * w * z;
2140 m.
data[5] = 1 - 2 * x * x - 2 * z * z;
2141 m.
data[6] = 2 * y * z + 2 * w * x;
2145 m.
data[8] = -(2 * x * z + 2 * w * y);
2146 m.
data[9] = -(2 * y * z - 2 * w * x);
2147 m.
data[10] = -(1 - 2 * x * x - 2 * y * y);
2172 o[0] = (q.
x * q.
x) - (q.
y * q.
y) - (q.
z * q.
z) + (q.
w * q.
w);
2173 o[1] = 2.0f * ((q.
x * q.
y) + (q.
z * q.
w));
2174 o[2] = 2.0f * ((q.
x * q.
z) - (q.
y * q.
w));
2175 o[3] = center.
x - center.
x * o[0] - center.
y * o[1] - center.
z * o[2];
2177 o[4] = 2.0f * ((q.
x * q.
y) - (q.
z * q.
w));
2178 o[5] = -(q.
x * q.
x) + (q.
y * q.
y) - (q.
z * q.
z) + (q.
w * q.
w);
2179 o[6] = 2.0f * ((q.
y * q.
z) + (q.
x * q.
w));
2180 o[7] = center.
y - center.
x * o[4] - center.
y * o[5] - center.
z * o[6];
2182 o[8] = 2.0f * ((q.
x * q.
z) + (q.
y * q.
w));
2183 o[9] = 2.0f * ((q.
y * q.
z) - (q.
x * q.
w));
2184 o[10] = -(q.
x * q.
x) - (q.
y * q.
y) + (q.
z * q.
z) + (q.
w * q.
w);
2185 o[11] = center.
z - center.
x * o[8] - center.
y * o[9] - center.
z * o[10];
2203 const f32 half_angle = 0.5f * angle;
2207 quat q = (
quat){s * axis.
x, s * axis.
y, s * axis.
z, c};
2225 quat out_quaternion;
2247 const f32 DOT_THRESHOLD = 0.9995f;
2248 if (dot > DOT_THRESHOLD) {
2251 out_quaternion = (
quat){v0.
x + ((v1.
x - v0.
x) * percentage),
2252 v0.
y + ((v1.
y - v0.
y) * percentage),
2253 v0.
z + ((v1.
z - v0.
z) * percentage),
2254 v0.
w + ((v1.
w - v0.
w) * percentage)};
2261 f32 theta = theta_0 * percentage;
2263 f32 sin_theta_0 =
ksin(theta_0);
2267 dot * sin_theta / sin_theta_0;
2268 f32 s1 = sin_theta / sin_theta_0;
2270 return (
quat){(v0.
x * s0) + (v1.
x * s1), (v0.
y * s0) + (v1.
y * s1),
2271 (v0.
z * s0) + (v1.
z * s1), (v0.
w * s0) + (v1.
w * s1)};
2302 return (((value - old_min) * (new_max - new_min)) / (old_max - old_min)) +
2315 *out_u32 = (((r & 0x0FF) << 16) | ((g & 0x0FF) << 8) | (b & 0x0FF));
2327 *out_r = (rgbu >> 16) & 0x0FF;
2328 *out_g = (rgbu >> 8) & 0x0FF;
2329 *out_b = (rgbu) & 0x0FF;
2342 out_v->
r = r / 255.0f;
2343 out_v->
g = g / 255.0f;
2344 out_v->
b = b / 255.0f;
2460 const vec3* extents);
2463 return (point.
x >= rect.
x && point.
x <= rect.
x + rect.
width) && (point.
y >= rect.
y && point.
y <= rect.
y + rect.
height);
2470 (extents.
min.
x + extents.
max.
x) * 0.5f,
2471 (extents.
min.
y + extents.
max.
y) * 0.5f,
2508 (extents.
min.
x + extents.
max.
x) * 0.5f,
2509 (extents.
min.
y + extents.
max.
y) * 0.5f,
2510 (extents.
min.
z + extents.
max.
z) * 0.5f,
2542 (v_0.
x - v_1.
x) * 0.5f,
2543 (v_0.
y - v_1.
y) * 0.5f};
2548 (v_0.
x - v_1.
x) * 0.5f,
2549 (v_0.
y - v_1.
y) * 0.5f,
2550 (v_0.
z - v_1.
z) * 0.5f};
2574 if (dot > 0.9999f) {
2579 if (dot < -0.9999f) {
2614 return 2.0f * (dx * dy + dy * dz + dz * dx);
2649 mat.
data[0] * local_center.
x + mat.
data[4] * local_center.
y + mat.
data[8] * local_center.
z + mat.
data[12],
2650 mat.
data[1] * local_center.
x + mat.
data[5] * local_center.
y + mat.
data[9] * local_center.
z + mat.
data[13],
2651 mat.
data[2] * local_center.
x + mat.
data[6] * local_center.
y + mat.
data[10] * local_center.
z + mat.
data[14]};
2666 vec3 half = {ax, ay, az};
2681 return (point.
x >= box.
min.
x && point.
x <= box.
max.
x) &&
2682 (point.
y >= box.
min.
y && point.
y <= box.
max.
y) &&
2683 (point.
z >= box.
min.
z && point.
z <= box.
max.
z);
2706 .direction = direction};
2742 for (
u8 i = 0; i < 3; ++i) {
2756 f32 overlap = (ra + rb) - dist;
2758 if (overlap < 0.0f) {
2762 if (overlap < *min_overlap) {
2763 *min_overlap = overlap;
2776 for (
u8 i = 0; i < 3; ++i) {
2781 for (
u8 i = 0; i < 3; ++i) {
2787 for (
u8 i = 0; i < 3; ++i) {
2788 for (
u8 j = 0; j < 3; ++j) {
2798 if (!
sat_overlap(axis, t, a, b, &min_overlap, &best_axis)) {
2812 out_result->
depth = min_overlap;
2827 for (
u8 i = 0; i < 3; ++i) {
2830 f32 diff = projected - clamped;
2831 dist_sq += (diff * diff);
2841 return dist_sq < combined_radii_sq;
This file contains global type definitions which are used throughout the entire engine and applicatio...
#define KAPI
Import/export qualifier.
Definition: defines.h:209
unsigned int u32
Unsigned 32-bit integer.
Definition: defines.h:27
_Bool b8
8-bit boolean type
Definition: defines.h:60
float f32
32-bit floating point number
Definition: defines.h:49
#define KMIN(x, y)
Definition: defines.h:302
signed int i32
Signed 32-bit integer.
Definition: defines.h:41
#define KMAX(x, y)
Definition: defines.h:303
#define KINLINE
Inline qualifier.
Definition: defines.h:256
#define KCLAMP(value, min, max)
Clamps value to a range of min and max (inclusive).
Definition: defines.h:236
unsigned long long u64
Unsigned 64-bit integer.
Definition: defines.h:30
unsigned char u8
Unsigned 8-bit integer.
Definition: defines.h:21
KINLINE ray ray_create(vec3 position, vec3 direction)
Definition: kmath.h:2703
KINLINE aabb aabb_from_mat4_extents(vec3 local_min, vec3 local_max, mat4 mat)
Definition: kmath.h:2642
KINLINE mat4 mat4_inverse(mat4 matrix)
Creates and returns an inverse of the provided matrix.
Definition: kmath.h:1562
#define vec3_create(x, y, z)
Creates and returns a new 3-element vector using the supplied values.
Definition: kmath.h:618
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:857
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:1989
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:408
KINLINE vec4 vec4_one(void)
Creates and returns a 4-component vector with all components set to 1.0f.
Definition: kmath.h:1106
KINLINE mat4 mat4_mul(mat4 matrix_0, mat4 matrix_1)
Returns the result of multiplying matrix_0 and matrix_1.
Definition: kmath.h:1386
KINLINE vec3 vec3_sign(vec3 v)
Definition: kmath.h:1018
KAPI f32 kmod(f32 x, f32 y)
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:1678
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_clamped(vec4 vector, vec4 min, vec4 max)
Returns a clamped copy of the provided vector.
Definition: kmath.h:1324
KINLINE vec2 vec2_max(vec2 vector_0, vec2 vector_1)
Definition: kmath.h:600
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:1189
KINLINE b8 aabbs_intersect(aabb a_0, aabb a_1)
Definition: kmath.h:2603
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:357
KINLINE mat4 mat4_translation(vec3 position)
Creates and returns a translation matrix from the given position.
Definition: kmath.h:1648
KINLINE vec2 vec2_up(void)
Creates and returns a 2-component vector pointing up (0, 1).
Definition: kmath.h:362
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:2314
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:589
KINLINE vec3 extents_2d_center(extents_2d extents)
Definition: kmath.h:2468
KINLINE vec3 vec3_backward(void)
Creates and returns a 3-component vector pointing backward (0, 0, 1).
Definition: kmath.h:704
KINLINE vec3 obb_closest_point(const struct obb *o, vec3 p)
Definition: kmath.h:2738
KINLINE vec2 vec2_from_scalar(f32 scalar)
Creates and returns a new 2-element vector using the supplied scalar for all components.
Definition: kmath.h:345
KINLINE extents_3d extents_3d_zero(void)
Definition: kmath.h:2482
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:660
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:1445
KINLINE vec4 vec4_mul_mat4(vec4 v, mat4 m)
Performs v * m.
Definition: kmath.h:1937
struct kintersect_result kintersect_result
KINLINE vec3 vec3_max(vec3 vector_0, vec3 vector_1)
Definition: kmath.h:1011
KINLINE f32 klerp(f32 a, f32 b, f32 t)
Definition: kmath.h:240
KINLINE vec4 vec4_clamped_scalar(vec4 vector, f32 min, f32 max)
Returns a clamped copy of the provided vector.
Definition: kmath.h:1352
KAPI b8 ray_intersects_aabb(aabb box, vec3 origin, vec3 direction, f32 max, f32 *out_min, f32 *out_max)
KINLINE f32 vec2_distance(vec2 vector_0, vec2 vector_1)
Returns the distance between vector_0 and vector_1.
Definition: kmath.h:576
KINLINE vec3 mat4_scale_get(mat4 matrix)
Returns the scale relative to the provided matrix.
Definition: kmath.h:1883
KINLINE vec3 vec3_lerp(vec3 v_0, vec3 v_1, f32 t)
Definition: kmath.h:2553
KINLINE obb aabb_to_obb(const aabb a, mat4 m)
Definition: kmath.h:2844
KINLINE mat4 mat4_identity(void)
Creates and returns an identity matrix:
Definition: kmath.h:1369
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:1466
KINLINE vec3 vec3_mid(vec3 v_0, vec3 v_1)
Definition: kmath.h:2546
KAPI ray ray_from_screen(vec2i screen_pos, rect_2di viewport_rect, vec3 origin, mat4 view, mat4 projection)
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:1854
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:1264
KINLINE void vec4_normalize(vec4 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:1231
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:397
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:1662
KINLINE extents_3d extents_3d_from_size(vec3 size)
Definition: kmath.h:2500
KAPI kfrustum kfrustum_create(vec3 position, vec3 target, 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_forward(void)
Creates and returns a 3-component vector pointing forward (0, 0, -1).
Definition: kmath.h:699
KINLINE b8 aabb_contains_point(vec3 point, aabb box)
Indicates if point is inside the provided AABB.
Definition: kmath.h:2680
KAPI f32 kfrandom_in_range(f32 min, f32 max)
Returns a random floating-point number that is within the given range (inclusive).
KINLINE aabb aabb_expand(aabb a, f32 amount)
Expand the AABB by amount on all axes.
Definition: kmath.h:2620
KINLINE vec3 vec3_from_scalar(f32 scalar)
Creates and returns a new 3-element vector using the supplied scalar for all components.
Definition: kmath.h:627
KINLINE f32 vec4_length_squared(vec4 vector)
Returns the squared length of the provided vector.
Definition: kmath.h:1211
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.
KAPI b8 raycast_plane_3d(const ray *r, const plane_3d *p, vec3 *out_point, f32 *out_distance)
KINLINE vec3 triangle_get_normal(const triangle *tri)
Definition: kmath.h:2557
KINLINE vec3 mat4_backward(mat4 matrix)
Returns a backward vector relative to the provided matrix.
Definition: kmath.h:1794
KINLINE extents_3d extents_3d_from_scalar(f32 scalar)
Definition: kmath.h:2494
KINLINE void vec2_normalize(vec2 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:474
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:737
KINLINE vec2 vec2_right(void)
Creates and returns a 2-component vector pointing right (1, 0).
Definition: kmath.h:377
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:432
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:1416
KINLINE quat quat_inverse(quat q)
Returns an inverse copy of the provided quaternion.
Definition: kmath.h:1997
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:762
KINLINE f32 vec3_length(vec3 vector)
Returns the length of the provided vector.
Definition: kmath.h:805
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:725
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:420
KAPI f32 katan(f32 x)
Calculates the arctangent of x.
KINLINE aabb aabb_combine(aabb a_0, aabb a_1)
Definition: kmath.h:2597
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:1894
KINLINE aabb aabb_create(vec3 min, vec3 max)
Definition: kmath.h:2591
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:2341
KAPI f32 kfrandom(void)
Returns a random floating-point number.
KAPI kfrustum kfrustum_from_view_projection(mat4 view_projection)
KINLINE vec2 vec2_create(f32 x, f32 y)
Creates and returns a new 2-element vector using the supplied values.
Definition: kmath.h:335
KINLINE f32 ksmoothstep(f32 edge_0, f32 edge_1, f32 x)
Perform Hermite interpolation between two values.
Definition: kmath.h:301
#define K_RAD2DEG_MULTIPLIER
A multiplier used to convert radians to degrees.
Definition: kmath.h:58
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:2168
KINLINE vec2 vec2_down(void)
Creates and returns a 2-component vector pointing down (0, -1).
Definition: kmath.h:367
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:250
KINLINE vec3 vec3_clamped_scalar(vec3 vector, f32 min, f32 max)
Returns a clamped copy of the provided vector.
Definition: kmath.h:940
KAPI f32 vec3_distance_to_line(vec3 point, vec3 line_start, vec3 line_direction)
KINLINE b8 obb_intersects_sphere(const struct obb *o, const struct ksphere *s)
Definition: kmath.h:2822
KINLINE vec3 mat4_left(mat4 matrix)
Returns a left vector relative to the provided matrix.
Definition: kmath.h:1839
KINLINE f32 deg_to_rad(f32 degrees)
Converts provided degrees to radians.
Definition: kmath.h:2280
KAPI b8 kfrustum_intersects_ksphere(const kfrustum *f, const ksphere *sphere)
Indicates if the frustum intersects (or contains) a sphere constructed via center and radius.
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:795
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:1145
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:1115
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:1161
KINLINE quat quat_normalize(quat q)
Returns a normalized copy of the provided quaternion.
Definition: kmath.h:1977
KAPI b8 raycast_disc_3d(const ray *r, vec3 center, vec3 normal, f32 outer_radius, f32 inner_radius, vec3 *out_point, f32 *out_distance)
KINLINE f32 vec2_length_squared(vec2 vector)
Returns the squared length of the provided vector.
Definition: kmath.h:455
KAPI i32 krandom(void)
Returns a random integer.
KINLINE void vec4_clamp_scalar(vec4 *vector, f32 min, f32 max)
Clamps the provided vector in-place to the given min/max values.
Definition: kmath.h:1336
KINLINE mat4 mat4_euler_x(f32 angle_radians)
Creates a rotation matrix from the provided x angle.
Definition: kmath.h:1707
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:1245
KINLINE vec2 vec2_normalized(vec2 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:486
KINLINE vec3 vec3_from_vec4(vec4 vector)
Definition: kmath.h:636
KAPI void kfrustum_corner_points_world_space(mat4 projection_view, vec4 *corners)
KINLINE f32 quat_dot(quat q_0, quat q_1)
Calculates the dot product of the provided quaternions.
Definition: kmath.h:2031
KAPI f32 kcos(f32 x)
Calculates the cosine of x.
KINLINE vec3 extents_3d_half(extents_3d extents)
Definition: kmath.h:2514
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:1869
KAPI ray ray_transformed(const ray *r, mat4 transform)
KINLINE quat quat_from_surface_normal(vec3 normal, vec3 reference_up)
Definition: kmath.h:2565
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:1086
KINLINE void vec3_normalize(vec3 *vector)
Normalizes the provided vector in place to a unit vector.
Definition: kmath.h:814
KAPI b8 ray_intersects_triangle(const ray *r, const triangle *t)
KINLINE vec2 vec2_clamped_scalar(vec2 vector, f32 min, f32 max)
Returns a clamped copy of the provided vector.
Definition: kmath.h:564
KINLINE vec3 mat4_down(mat4 matrix)
Returns a downward vector relative to the provided matrix.
Definition: kmath.h:1824
KINLINE vec4 vec4_zero(void)
Creates and returns a 4-component vector with all components set to 0.0f.
Definition: kmath.h:1100
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:445
KINLINE f32 vec2_length(vec2 vector)
Returns the length of the provided vector.
Definition: kmath.h:465
KINLINE f32 quat_normal(quat q)
Returns the normal of the provided quaternion.
Definition: kmath.h:1967
KINLINE b8 rect_2d_contains_point(rect_2d rect, vec2 point)
Definition: kmath.h:2462
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:386
KINLINE mat4 mat4_euler_y(f32 angle_radians)
Creates a rotation matrix from the provided y angle.
Definition: kmath.h:1725
KINLINE vec3 vec3_min(vec3 vector_0, vec3 vector_1)
Definition: kmath.h:1004
KINLINE f32 mat4_determinant(mat4 matrix)
Calculates the determinant of the given matrix.
Definition: kmath.h:1524
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:966
KINLINE mat4 mat4_transposed(mat4 matrix)
Returns a transposed copy of the provided matrix (rows->colums)
Definition: kmath.h:1497
KAPI f32 katan2(f32 x, f32 y)
KINLINE f32 obb_project_extents(const struct obb *o, vec3 axis)
Definition: kmath.h:2732
KINLINE quat quat_identity(void)
Creates an identity quaternion.
Definition: kmath.h:1954
#define K_FLOAT_EPSILON
Smallest positive number where 1.0 + FLOAT_EPSILON != 0.
Definition: kmath.h:73
KINLINE f32 rad_to_deg(f32 radians)
Converts provided radians to degrees.
Definition: kmath.h:2288
KINLINE quat quat_mul(quat q_0, quat q_1)
Multiplies the provided quaternions.
Definition: kmath.h:2006
KAPI f32 klog(f32 x)
Computes the logarithm of x.
KAPI b8 ray_pick_triangle(const ray *r, b8 backface_cull, u32 vertex_count, u32 vertex_element_size, void *vertices, u32 index_count, u32 *indices, triangle *out_triangle, vec3 *out_hit_pos, vec3 *out_hit_normal)
KINLINE mat4 mat4_euler_z(f32 angle_radians)
Creates a rotation matrix from the provided z angle.
Definition: kmath.h:1743
KINLINE vec3 vec3_up(void)
Creates and returns a 3-component vector pointing up (0, 1, 0).
Definition: kmath.h:679
KINLINE vec3 vec3_mul_mat4(vec3 v, mat4 m)
Performs v * m.
Definition: kmath.h:1908
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:1046
KINLINE vec4 vec4_div_scalar(vec4 vector_0, f32 scalar)
Definition: kmath.h:1197
KINLINE vec3 vec3_down(void)
Creates and returns a 3-component vector pointing down (0, -1, 0).
Definition: kmath.h:684
KINLINE vec2 vec2_zero(void)
Creates and returns a 2-component vector with all components set to 0.0f.
Definition: kmath.h:351
KINLINE vec2 vec2_mid(vec2 v_0, vec2 v_1)
Definition: kmath.h:2540
KINLINE b8 extents_3d_is_zero(extents_3d extents)
Definition: kmath.h:2536
KAPI b8 quat_is_identity(quat q)
Indicates if the provided quaterion is identity. (0, 0, 0, 1)
KINLINE vec3 vec3_project(vec3 v_0, vec3 v_1)
Projects v_0 onto v_1.
Definition: kmath.h:979
KINLINE vec3 mat4_forward(mat4 matrix)
Returns a forward vector relative to the provided matrix.
Definition: kmath.h:1779
KINLINE vec4 vec4_from_scalar(f32 scalar)
Creates and returns a new 4-element vector using the supplied scalar for all components.
Definition: kmath.h:1065
KINLINE f32 ksign(f32 x)
Returns 0.0f if x == 0.0f, -1.0f if negative, otherwise 1.0f.
Definition: kmath.h:94
KINLINE b8 sat_overlap(vec3 axis, vec3 t, const struct obb *a, const struct obb *b, f32 *min_overlap, vec3 *best_axis)
Definition: kmath.h:2752
KINLINE vec2 vec2_left(void)
Creates and returns a 2-component vector pointing left (-1, 0).
Definition: kmath.h:372
KINLINE vec3 extents_3d_center(extents_3d extents)
Definition: kmath.h:2506
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:501
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:750
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:2355
KINLINE f32 kstep(f32 edge, f32 x)
Compares x to edge, returning 0 if x < edge; otherwise 1.0f;.
Definition: kmath.h:100
KINLINE f32 vec4_length(vec4 vector)
Returns the length of the provided vector.
Definition: kmath.h:1222
KINLINE void vec3_clamp(vec3 *vector, vec3 min, vec3 max)
Clamps the provided vector in-place to the given min/max values.
Definition: kmath.h:896
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:1764
KINLINE vec3 vec3_left(void)
Creates and returns a 3-component vector pointing left (-1, 0, 0).
Definition: kmath.h:689
KINLINE mat4 quat_to_mat4(quat q)
Creates a rotation matrix from the given quaternion.
Definition: kmath.h:2041
KINLINE vec2 vec2_min(vec2 vector_0, vec2 vector_1)
Definition: kmath.h:594
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:320
KINLINE void vec2_clamp(vec2 *vector, vec2 min, vec2 max)
Clamps the provided vector in-place to the given min/max values.
Definition: kmath.h:520
KINLINE vec2 vec2_clamped(vec2 vector, vec2 min, vec2 max)
Returns a clamped copy of the provided vector.
Definition: kmath.h:536
KINLINE vec3 mat4_up(mat4 matrix)
Returns a upward vector relative to the provided matrix.
Definition: kmath.h:1809
KINLINE vec3 vec3_clamped(vec3 vector, vec3 min, vec3 max)
Returns a clamped copy of the provided vector.
Definition: kmath.h:912
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:1074
KINLINE vec3 vec3_div_scalar(vec3 vector_0, f32 scalar)
Definition: kmath.h:781
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:1173
KINLINE vec3 vec3_from_vec2(vec2 vector, f32 z)
Definition: kmath.h:648
KINLINE b8 obb_intersects_obb(const struct obb *a, const struct obb *b, kintersect_result *out_result)
Definition: kmath.h:2770
KINLINE aabb aabb_from_mat4(vec3 half_extents, mat4 mat)
Constructs a tight, world-space AABB from an OBB defined by a center point (the translation of mat) a...
Definition: kmath.h:2633
KINLINE extents_3d extents_3d_one(void)
Definition: kmath.h:2488
KAPI b8 kfrustum_intersects_sphere(const kfrustum *f, const vec3 *center, f32 radius)
Indicates if the frustum intersects (or contains) a sphere constructed via center and radius.
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:1130
KINLINE vec3 vec3_rotate(vec3 v, quat q)
Definition: kmath.h:1022
KINLINE void vec4_clamp(vec4 *vector, vec4 min, vec4 max)
Clamps the provided vector in-place to the given min/max values.
Definition: kmath.h:1308
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:873
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:694
KINLINE vec3 size_from_extents_3d(extents_3d extents)
Definition: kmath.h:2522
KINLINE quat quat_from_axis_angle(vec3 axis, f32 angle, b8 normalize)
Creates a quaternion from the given axis and angle.
Definition: kmath.h:2202
KINLINE vec3 extents_2d_half(extents_2d extents)
Definition: kmath.h:2475
KINLINE vec3 vec3_one(void)
Creates and returns a 3-component vector with all components set to 1.0f.
Definition: kmath.h:674
KAPI b8 kfrustum_intersects_aabb(const kfrustum *f, const vec3 *center, const vec3 *extents)
Indicates if frustum f intersects an axis-aligned bounding box constructed via center and extents.
KINLINE vec4 mat4_mul_vec4(mat4 m, vec4 v)
Performs m * v.
Definition: kmath.h:1922
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:1281
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:840
KINLINE f32 vec3_distance(vec3 vector_0, vec3 vector_1)
Returns the distance between vector_0 and vector_1.
Definition: kmath.h:952
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:2326
KINLINE vec3 vec3_transform(vec3 v, f32 w, mat4 m)
Transform v by m.
Definition: kmath.h:996
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:776
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:2224
KINLINE b8 aabb_contains_aabb(aabb a, aabb b)
Indicates if AABB b is completely inside AABB a.
Definition: kmath.h:2693
KINLINE vec3 vec3_zero(void)
Creates and returns a 3-component vector with all components set to 0.0f.
Definition: kmath.h:668
KINLINE extents_3d extents_combine(extents_3d a, extents_3d b)
Definition: kmath.h:2530
KINLINE f32 aabb_surface_area(aabb a)
Definition: kmath.h:2610
KINLINE void vec2_clamp_scalar(vec2 *vector, f32 min, f32 max)
Clamps the provided vector in-place to the given min/max values.
Definition: kmath.h:548
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:713
#define K_FLOAT_MAX
Definition: kmath.h:75
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:2300
#define K_DEG2RAD_MULTIPLIER
A multiplier used to convert degrees to radians.
Definition: kmath.h:55
KAPI b8 ray_intersects_sphere(const ray *r, vec3 center, f32 radius, vec3 *out_point, f32 *out_distance)
KINLINE b8 sphere_intersects_sphere(const ksphere a, const ksphere b)
Definition: kmath.h:2837
KINLINE vec3 vec3_normalized(vec3 vector)
Returns a normalized copy of the supplied vector.
Definition: kmath.h:827
KINLINE void vec3_clamp_scalar(vec3 *vector, f32 min, f32 max)
Clamps the provided vector in-place to the given min/max values.
Definition: kmath.h:924
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:275
Represents the extents of a 2d object.
Definition: math_types.h:391
vec2 max
The maximum extents of the object.
Definition: math_types.h:395
vec2 min
The minimum extents of the object.
Definition: math_types.h:393
Represents the extents of a 3d object.
Definition: math_types.h:401
vec3 min
The minimum extents of the object.
Definition: math_types.h:403
vec3 max
The maximum extents of the object.
Definition: math_types.h:405
Definition: math_types.h:500
vec3 closest_point_b
Definition: kmath.h:2728
vec3 closest_point_a
Definition: kmath.h:2727
vec3 normal
Definition: kmath.h:2726
f32 depth
Definition: kmath.h:2729
Definition: math_types.h:665
vec3 position
Definition: math_types.h:666
f32 radius
Definition: math_types.h:667
Definition: math_types.h:413
vec3 center
Definition: math_types.h:414
vec3 axis[3]
Definition: math_types.h:416
vec3 half_extents
Definition: math_types.h:417
Definition: math_types.h:484
Represents a line which starts at an origin and proceed infinitely in the given direction....
Definition: math_types.h:638
vec3 origin
Definition: math_types.h:639
Definition: math_types.h:622
vec3 verts[3]
Definition: math_types.h:623
A 3x3 matrix.
Definition: math_types.h:377
f32 data[12]
The matrix elements.
Definition: math_types.h:379
a 4x4 matrix, typically used to represent object transformations.
Definition: math_types.h:383
f32 data[16]
The matrix elements.
Definition: math_types.h:385
A 2-element vector.
Definition: math_types.h:31
f32 x
The first element.
Definition: math_types.h:37
f32 y
The second element.
Definition: math_types.h:47
f32 elements[2]
An array of x, y.
Definition: math_types.h:33
A 2-element integer-based vector.
Definition: math_types.h:508
A 3-element vector.
Definition: math_types.h:117
f32 elements[3]
An array of x, y, z.
Definition: math_types.h:119
f32 r
The first element.
Definition: math_types.h:125
f32 b
The third element.
Definition: math_types.h:145
f32 x
The first element.
Definition: math_types.h:123
f32 g
The second element.
Definition: math_types.h:135
f32 z
The third element.
Definition: math_types.h:143
f32 y
The second element.
Definition: math_types.h:133
A 4-element vector.
Definition: math_types.h:229
f32 elements[4]
An array of x, y, z, w.
Definition: math_types.h:231
f32 height
The fourth element.
Definition: math_types.h:268
f32 x
The first element.
Definition: math_types.h:236
f32 width
The third element.
Definition: math_types.h:258
f32 z
The third element.
Definition: math_types.h:252
f32 y
The second element.
Definition: math_types.h:244
f32 w
The fourth element.
Definition: math_types.h:262
A 4-element integer-based vector.
Definition: math_types.h:574