diff options
author | bonmas14 <bonmas14@gmail.com> | 2025-08-23 01:28:41 +0300 |
---|---|---|
committer | bonmas14 <bonmas14@gmail.com> | 2025-08-23 01:28:41 +0300 |
commit | 8ebdc95621bc61fdf3c98cd7ae4ddca67398df23 (patch) | |
tree | 205413ed09ac001e37267889d429a363c37008f2 /src/un_math.c | |
parent | c96c355b360f18f982459e1a866dcbf5864efdb8 (diff) | |
download | ungrateful-8ebdc95621bc61fdf3c98cd7ae4ddca67398df23.tar.gz ungrateful-8ebdc95621bc61fdf3c98cd7ae4ddca67398df23.zip |
Diffstat (limited to 'src/un_math.c')
-rw-r--r-- | src/un_math.c | 220 |
1 files changed, 174 insertions, 46 deletions
diff --git a/src/un_math.c b/src/un_math.c index 42a1c4b..a2ff65e 100644 --- a/src/un_math.c +++ b/src/un_math.c @@ -1,50 +1,13 @@ -/* reals */ - -real un_m_lerpr(real a, real b, real t) { +/* lerps */ +f32 un_m_lerpf(f32 a, f32 b, f32 t) { return (1.0 - t) * a + b * t; } -real un_m_ease_isiner(real t) { -#if defined(UN_DOUBLE_PRECISION) - return 1.0 - cos((t * PI_D) / 2.0); -#else - return 1.0 - cosf((t * PI) / 2.0); -#endif -} - -real un_m_ease_iosiner(real t) { -#if defined(UN_DOUBLE_PRECISION) - return sin((t * PI_D) / 2.0f); -#else - return sinf((t * PI) / 2.0f); -#endif -} - -real un_m_ease_osiner(real t) { -#if defined(UN_DOUBLE_PRECISION) - return -(cos(t * PI_D) - 1.0) / 2.0; -#else - return -(cosf(t * PI) - 1.0) / 2.0; -#endif -} - -real un_m_ease_iquadr(real t) { - return t * t; -} - -real un_m_ease_ioquadr(real t) { - return 1.0 - (1.0 - t) * (1.0 - t); -} - -real un_m_ease_oquadr(real t) { - return t < 0.5 ? 2.0 * t * t : 1.0 - ((-2.0 * t + 2.0) * (-2.0 * t + 2.0)) / 2.0; -} - -/* floats */ -f32 un_m_lerpf(f32 a, f32 b, f32 t) { +f64 un_m_lerpd(f64 a, f64 b, f64 t) { return (1.0 - t) * a + b * t; } +/* easing */ f32 un_m_ease_isinef(f32 t) { return 1.0 - cosf((t * PI) / 2.0); } @@ -69,11 +32,6 @@ f32 un_m_ease_oquadf(f32 t) { return t < 0.5 ? 2.0 * t * t : 1.0 - ((-2.0 * t + 2.0) * (-2.0 * t + 2.0)) / 2.0; } -/* doubles */ -f64 un_m_lerpd(f64 a, f64 b, f64 t) { - return (1.0 - t) * a + b * t; -} - f64 un_m_ease_isined(f64 t) { return 1.0 - cos((t * PI) / 2.0); } @@ -97,3 +55,173 @@ f64 un_m_ease_ioquadd(f64 t) { f64 un_m_ease_oquadd(f64 t) { return t < 0.5 ? 2.0 * t * t : 1.0 - ((-2.0 * t + 2.0) * (-2.0 * t + 2.0)) / 2.0; } + +/* complex */ + +void un_m_complex_divf(f32 *v, f32 *a, f32 *b) { + f32 divisor; + + // un_m_complex_mulf(v, a, conjugated_b) + v[0] = a[0] * b[0] - a[1] * -b[1]; + v[1] = a[0] * -b[1] + a[1] * b[0]; + + divisor = b[0] * b[0] + b[1] * b[1]; + + v[0] /= divisor; + v[1] /= divisor; +} + +void un_m_complex_mulf(f32 *v, f32 *a, f32 *b) { + v[0] = a[0] * b[0] - a[1] * b[1]; + v[1] = a[0] * b[1] + a[1] * b[0]; +} + + +void un_m_complex_divd(f64 *v, f64 *a, f64 *b) { + f64 divisor; + + // un_m_complex_muld(v, a, conjugated_b) + v[0] = a[0] * b[0] - a[1] * -b[1]; + v[1] = a[0] * -b[1] + a[1] * b[0]; + + divisor = b[0] * b[0] + b[1] * b[1]; + + v[0] /= divisor; + v[1] /= divisor; +} + +void un_m_complex_muld(f64 *v, f64 *a, f64 *b) { + v[0] = a[0] * b[0] - a[1] * b[1]; + v[1] = a[0] * b[1] + a[1] * b[0]; +} + +/* splines */ + +void un_m_bezierf(f32 *v, f32 a, f32 q0, f32 q1, f32 b, f32 t) { + f32 i0, i1, i2, p0, p1, ti; + + ti = 1.0 - t; + + i0 = ti * a + q0 * t; + i1 = ti * q0 + q1 * t; + i2 = ti * q1 + b * t; + + p0 = ti * i0 + i1 * t; + p1 = ti * i1 + i2 * t; + + *v = ti * p0 + p1 * t; +} + +void un_m_bezierd(f64 *v, f64 a, f64 q0, f64 q1, f64 b, f64 t) { + f64 i0, i1, i2, p0, p1, ti; + + ti = 1.0 - t; + + i0 = ti * a + q0 * t; + i1 = ti * q0 + q1 * t; + i2 = ti * q1 + b * t; + + p0 = ti * i0 + i1 * t; + p1 = ti * i1 + i2 * t; + + *v = ti * p0 + p1 * t; +} + +void un_m_bezier2f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { + un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); + un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); +} + +void un_m_bezier3f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { + un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); + un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); + un_m_bezierf(v + 2, a[2], q0[2], q1[2], b[2], t); +} + +void un_m_bezier4f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t) { + un_m_bezierf(v + 0, a[0], q0[0], q1[0], b[0], t); + un_m_bezierf(v + 1, a[1], q0[1], q1[1], b[1], t); + un_m_bezierf(v + 2, a[2], q0[2], q1[2], b[2], t); + un_m_bezierf(v + 3, a[3], q0[3], q1[3], b[3], t); +} + + +void un_m_bezier2d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { + un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); + un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); +} + +void un_m_bezier3d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { + un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); + un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); + un_m_bezierd(v + 2, a[2], q0[2], q1[2], b[2], t); +} + +void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t) { + un_m_bezierd(v + 0, a[0], q0[0], q1[0], b[0], t); + un_m_bezierd(v + 1, a[1], q0[1], q1[1], b[1], t); + un_m_bezierd(v + 2, a[2], q0[2], q1[2], b[2], t); + un_m_bezierd(v + 3, a[3], q0[3], q1[3], b[3], t); +} + + + +/* matrix and quaternions */ + +/* convention of matrix is: Column-major + * + * stored in memory as x0 y0 z0 w0 x1 y1 z1 w1 ... + * + * transform + * inverse transform + * multiply + * reverse + * rotate + * quaternion -> matrix + * matrix -> quaternion + * matrix to components: un_mat_disassemble(f32 *pos3, f32 *scale3, f32 *quat4) + * un_mat_assemble(f32 *v, f32 *pos3, f32 *scale3, f32 *quat4) + * assembe_inverse + * + * */ + + + +void un_m_mat_identityf(f32 *v) { + un_memory_set((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); + + v[0 + 0 * UN_MATRIX_STRIPE] = 1.0f; + v[1 + 1 * UN_MATRIX_STRIPE] = 1.0f; + v[2 + 2 * UN_MATRIX_STRIPE] = 1.0f; + v[3 + 3 * UN_MATRIX_STRIPE] = 1.0f; +} + +void un_m_mat_mulf(f32 *v, f32 *left, f32 *right) { + u64 row, col, i; + un_memory_set((void*)v, 0, sizeof(*v) * UN_MATRIX_SIZE); + + for (col = 0; col < 4; col++) { + for (row = 0; row < 4; row++) { + for (i = 0; i < 4; i++) { + v[row + col * UN_MATRIX_STRIPE] += left[row + i * UN_MATRIX_STRIPE] * right[i + col * UN_MATRIX_STRIPE]; + } + } + } +} + +void un_mat_xformf(f32 *v, f32 *xform) { + un_m_mat_identityf(v); + + v[3 + 0 * UN_MATRIX_STRIPE] += xform[0]; + v[3 + 1 * UN_MATRIX_STRIPE] += xform[1]; + v[3 + 2 * UN_MATRIX_STRIPE] += xform[2]; +} + +void un_mat_scalef(f32 *v, f32 *scale) { + un_m_mat_identityf(v); + + v[0 + 0 * UN_MATRIX_STRIPE] = scale[0]; + v[1 + 1 * UN_MATRIX_STRIPE] = scale[1]; + v[2 + 2 * UN_MATRIX_STRIPE] = scale[2]; +} + |