diff options
| author | realtradam <[email protected]> | 2023-06-16 01:18:13 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2023-06-16 01:18:13 -0400 |
| commit | a47cc44ef7191d03f8ca1b8d4e6b9dd34fba1807 (patch) | |
| tree | f7e358545f5043df20695d0cf51dcf8caa10cb12 /src/math | |
| parent | 425516a9c53183179c43517f1b6501a790378a05 (diff) | |
| download | RodeoKit-a47cc44ef7191d03f8ca1b8d4e6b9dd34fba1807.tar.gz RodeoKit-a47cc44ef7191d03f8ca1b8d4e6b9dd34fba1807.zip | |
matrix math wrapper as well as various refactors for consistant and cleaner code
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/irodeo_mat4.h | 8 | ||||
| -rw-r--r-- | src/math/irodeo_math.h | 16 | ||||
| -rw-r--r-- | src/math/irodeo_math_t.h | 1 | ||||
| -rw-r--r-- | src/math/irodeo_rng_t.h | 23 | ||||
| -rw-r--r-- | src/math/irodeo_vec2.h | 14 | ||||
| -rw-r--r-- | src/math/irodeo_vec3.h | 15 | ||||
| -rw-r--r-- | src/math/rodeo_mat4.c | 171 | ||||
| -rw-r--r-- | src/math/rodeo_math.c | 20 | ||||
| -rw-r--r-- | src/math/rodeo_rng.c | 81 | ||||
| -rw-r--r-- | src/math/rodeo_vec2.c | 189 | ||||
| -rw-r--r-- | src/math/rodeo_vec3.c | 191 |
11 files changed, 729 insertions, 0 deletions
diff --git a/src/math/irodeo_mat4.h b/src/math/irodeo_mat4.h new file mode 100644 index 0000000..ff625a7 --- /dev/null +++ b/src/math/irodeo_mat4.h @@ -0,0 +1,8 @@ +#pragma once + +// -- internal -- +// public +#include "rodeo/math/mat4_t.h" + +void +irodeo_print_matrix(rodeo_math_mat4_t mat); diff --git a/src/math/irodeo_math.h b/src/math/irodeo_math.h new file mode 100644 index 0000000..beed68f --- /dev/null +++ b/src/math/irodeo_math.h @@ -0,0 +1,16 @@ +#pragma once + +// external needs to be first here +// -- external -- +/*#define CGLM_FORCE_LEFT_HANDED*/ +#define CGLM_FORCE_DEPTH_ZERO_TO_ONE +/*#define CGLM_CLIPSPACE_INCLUDE_ALL*/ +#include "cglm/struct.h" + +// -- internal -- +// private +#include "math/irodeo_vec2.h" +#include "math/irodeo_vec3.h" +//#include "math/irodeo_mat4.h" + + diff --git a/src/math/irodeo_math_t.h b/src/math/irodeo_math_t.h new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/math/irodeo_math_t.h @@ -0,0 +1 @@ + diff --git a/src/math/irodeo_rng_t.h b/src/math/irodeo_rng_t.h new file mode 100644 index 0000000..0419496 --- /dev/null +++ b/src/math/irodeo_rng_t.h @@ -0,0 +1,23 @@ +#pragma once + +// -- internal -- +// public +#include "rodeo/math/rng_t.h" +// private +#include "math/irodeo_rng_t.h" + +// -- external -- +#include "stc/crand.h" + +typedef +struct +{ + rodeo_math_rng_generator_t global_generator; +} +irodeo_math_rng_state_t; + +struct +irodeo_math_rng_generator +{ + crand_t crand; +}; diff --git a/src/math/irodeo_vec2.h b/src/math/irodeo_vec2.h new file mode 100644 index 0000000..0b79032 --- /dev/null +++ b/src/math/irodeo_vec2.h @@ -0,0 +1,14 @@ +#pragma once + +// -- internal -- +// public +#include "rodeo/math/vec2_t.h" +// private +#include "math/irodeo_math.h" + +rodeo_math_vec2_t +irodeo_math_cglmVec2_to_rodeoVec2(vec2s in); + +vec2s +irodeo_math_rodeoVec2_to_cglmVec2(rodeo_math_vec2_t in); + diff --git a/src/math/irodeo_vec3.h b/src/math/irodeo_vec3.h new file mode 100644 index 0000000..bbfffac --- /dev/null +++ b/src/math/irodeo_vec3.h @@ -0,0 +1,15 @@ + +#pragma once + +// -- internal -- +// public +#include "rodeo/math/vec3_t.h" +// private +#include "math/irodeo_math.h" + +rodeo_math_vec3_t +irodeo_math_cglmVec3_to_rodeoVec3(vec3s in); + +vec3s +irodeo_math_rodeoVec3_to_cglmVec3(rodeo_math_vec3_t in); + diff --git a/src/math/rodeo_mat4.c b/src/math/rodeo_mat4.c new file mode 100644 index 0000000..70d0226 --- /dev/null +++ b/src/math/rodeo_mat4.c @@ -0,0 +1,171 @@ + +// -- internal -- +// public +#include "rodeo/math.h" +#include "rodeo/log.h" +// private +#include "math/irodeo_math.h" + +static inline +rodeo_math_mat4_t +irodeo_math_cglmMat4_to_rodeoMat4(mat4s in) +{ + return (rodeo_math_mat4_t){ + .val = { + .m00 = in.raw[0][0], .m01 = in.raw[0][1], .m02 = in.raw[0][2], .m03 = in.raw[0][3], + .m10 = in.raw[1][0], .m11 = in.raw[1][1], .m12 = in.raw[1][2], .m13 = in.raw[1][3], + .m20 = in.raw[2][0], .m21 = in.raw[2][1], .m22 = in.raw[2][2], .m23 = in.raw[2][3], + .m30 = in.raw[3][0], .m31 = in.raw[3][1], .m32 = in.raw[3][2], .m33 = in.raw[3][3] + } + }; +} + +static inline +mat4s +irodeo_math_rodeoMat4_to_cglmMat4(rodeo_math_mat4_t in) +{ + return (mat4s){ + .raw = { + { + in.val.m00, in.val.m01, in.val.m02, in.val.m03 + }, + { + in.val.m10, in.val.m11, in.val.m12, in.val.m13 + }, + { + in.val.m20, in.val.m21, in.val.m22, in.val.m23 + }, + { + in.val.m30, in.val.m31, in.val.m32, in.val.m33 + }, + } + }; +} + +void +irodeo_print_matrix(rodeo_math_mat4_t mat) +{ + rodeo_log( + rodeo_logLevel_warning, + "%.05f, %.05f, %.05f, %.05f", + mat.raw[0][0], + mat.raw[0][1], + mat.raw[0][2], + mat.raw[0][3] + ); + rodeo_log( + rodeo_logLevel_info, + "%.05f, %.05f, %.05f, %.05f", + mat.raw[1][0], + mat.raw[1][1], + mat.raw[1][2], + mat.raw[1][3] + ); + rodeo_log( + rodeo_logLevel_info, + "%.05f, %.05f, %.05f, %.05f", + mat.raw[2][0], + mat.raw[2][1], + mat.raw[2][2], + mat.raw[2][3] + ); + rodeo_log( + rodeo_logLevel_info, + "%.05f, %.05f, %.05f, %.05f", + mat.raw[3][0], + mat.raw[3][1], + mat.raw[3][2], + mat.raw[3][3] + ); +} + + +rodeo_math_mat4_t +rodeo_math_mat4_identity(void) +{ + return irodeo_math_cglmMat4_to_rodeoMat4(glms_mat4_identity()); +} + +rodeo_math_mat4_t +rodeo_math_mat4_zero(void) +{ + return irodeo_math_cglmMat4_to_rodeoMat4(glms_mat4_zero()); +} + +rodeo_math_mat4_t +rodeo_math_mat4_transpose(rodeo_math_mat4_t m) +{ + return irodeo_math_cglmMat4_to_rodeoMat4( + glms_mat4_transpose( + irodeo_math_rodeoMat4_to_cglmMat4(m) + ) + ); +} + +rodeo_math_mat4_t +rodeo_math_mat4_translate(rodeo_math_mat4_t m, rodeo_math_vec3_t v) +{ + return irodeo_math_cglmMat4_to_rodeoMat4( + glms_translate( + irodeo_math_rodeoMat4_to_cglmMat4(m), + irodeo_math_rodeoVec3_to_cglmVec3(v) + ) + ); +} + +rodeo_math_mat4_t +rodeo_math_mat4_scale(rodeo_math_mat4_t m, rodeo_math_vec3_t v) +{ + return irodeo_math_cglmMat4_to_rodeoMat4( + glms_scale( + irodeo_math_rodeoMat4_to_cglmMat4(m), + irodeo_math_rodeoVec3_to_cglmVec3(v) + ) + ); +} + +rodeo_math_mat4_t +rodeo_math_mat4_rotate(rodeo_math_mat4_t m, float turns, rodeo_math_vec3_t v) +{ + return irodeo_math_cglmMat4_to_rodeoMat4( + glms_rotate( + irodeo_math_rodeoMat4_to_cglmMat4(m), + turns, + irodeo_math_rodeoVec3_to_cglmVec3(v) + ) + ); +} + +rodeo_math_mat4_t +rodeo_math_mat4_multiply(rodeo_math_mat4_t a, rodeo_math_mat4_t b) +{ + return irodeo_math_cglmMat4_to_rodeoMat4( + glms_mat4_mul( + irodeo_math_rodeoMat4_to_cglmMat4(a), + irodeo_math_rodeoMat4_to_cglmMat4(b) + ) + ); +} + +rodeo_math_mat4_t +rodeo_math_mat4_orthographic( + float left, + float right, + float bottom, + float top, + float near, + float far +) +{ + return irodeo_math_cglmMat4_to_rodeoMat4( + glms_ortho_rh_zo( + left, + right, + bottom, + top, + near, + far + ) + ); +} + diff --git a/src/math/rodeo_math.c b/src/math/rodeo_math.c new file mode 100644 index 0000000..20516e3 --- /dev/null +++ b/src/math/rodeo_math.c @@ -0,0 +1,20 @@ + +// -- internal -- +// public +#include "rodeo/math.h" + +// -- system -- +#include "math.h" + +float +rodeo_math_radians_to_turns(float radians) +{ + return (radians / mrodeo_math_pi) / 2.0f; +} + +float +rodeo_math_turns_to_radians(float turns) +{ + return turns * mrodeo_math_pi * 2.0f; +} + diff --git a/src/math/rodeo_rng.c b/src/math/rodeo_rng.c new file mode 100644 index 0000000..bf94e0b --- /dev/null +++ b/src/math/rodeo_rng.c @@ -0,0 +1,81 @@ + +// internal +// public +#include "rodeo/math/rng.h" +// private +#include "math/irodeo_rng_t.h" + +// external +#include "stc/crand.h" +#include "SDL.h" + +// system +#include <inttypes.h> + +void +rodeo_math_rng_init(void) +{ + csrand(SDL_GetTicks64()); +} + +void +rodeo_math_rng_deinit(void) +{ + // no need to do anything +} + +rodeo_math_rng_generator_t +rodeo_math_rng_generator_create(uint64_t seed) +{ + rodeo_math_rng_generator_t result = { + .seed = seed, + .data = malloc(sizeof(*(rodeo_math_rng_generator_t){0}.data)) + }; + result.data->crand = crand_init(seed); + return result; +} + +void +rodeo_math_rng_generator_destroy(rodeo_math_rng_generator_t generator) +{ + free(generator.data); +} + +double +rodeo_math_rng_double_get(rodeo_math_rng_generator_t generator) +{ + if(generator.seed == 0) + { + return crandf(); + } + else + { + return crand_f64(&generator.data->crand); + } +} + +float +rodeo_math_rng_float_get(rodeo_math_rng_generator_t generator) +{ + return (float)rodeo_math_rng_double_get(generator); +} + +uint64_t +rodeo_math_rng_uint64_get(rodeo_math_rng_generator_t generator) +{ + if(generator.seed == 0) + { + return crand(); + } + else + { + return crand_u64(&generator.data->crand); + } +} + +uint32_t +rodeo_math_rng_uint32_get(rodeo_math_rng_generator_t generator) +{ + return (uint32_t)rodeo_math_rng_uint64_get(generator); +} + diff --git a/src/math/rodeo_vec2.c b/src/math/rodeo_vec2.c new file mode 100644 index 0000000..be9d7ff --- /dev/null +++ b/src/math/rodeo_vec2.c @@ -0,0 +1,189 @@ + +// -- internal -- +// public +#include "rodeo/math.h" +#include "rodeo/math/vec2.h" +// private +#include "math/irodeo_math.h" +#include "math/irodeo_vec2.h" + +rodeo_math_vec2_t +irodeo_math_cglmVec2_to_rodeoVec2(vec2s in) +{ + return (rodeo_math_vec2_t){ + .raw[0] = in.raw[0], + .raw[1] = in.raw[1] + }; +} + +vec2s +irodeo_math_rodeoVec2_to_cglmVec2(rodeo_math_vec2_t in) +{ + return (vec2s){ + .raw[0] = in.raw[0], + .raw[1] = in.raw[1] + }; +} + + +rodeo_math_vec2_t +rodeo_math_vec2_zero(void) +{ + return irodeo_math_cglmVec2_to_rodeoVec2(glms_vec2_zero()); +} + +rodeo_math_vec2_t +rodeo_math_vec2_one(void) +{ + return irodeo_math_cglmVec2_to_rodeoVec2(glms_vec2_one()); +} + +float +rodeo_math_vec2_dot(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return glms_vec2_dot( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ); +} + +float +rodeo_math_vec2_cross(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return glms_vec2_dot( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_add(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_add( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_subtract(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_sub( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_multiply(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_mul( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_scale(rodeo_math_vec2_t a, float b) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_scale( + irodeo_math_rodeoVec2_to_cglmVec2(a), + b + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_divide(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_div( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_negate(rodeo_math_vec2_t a) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_negate( + irodeo_math_rodeoVec2_to_cglmVec2(a) + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_normalize(rodeo_math_vec2_t a) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_normalize( + irodeo_math_rodeoVec2_to_cglmVec2(a) + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_rotate(rodeo_math_vec2_t a, float turns) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_rotate( + irodeo_math_rodeoVec2_to_cglmVec2(a), + rodeo_math_radians_to_turns(turns) + ) + ); +} + +float +rodeo_math_vec2_distance(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return ( + glms_vec2_distance( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ) + ); +} + +float +rodeo_math_vec2_distanceSq(rodeo_math_vec2_t a, rodeo_math_vec2_t b) +{ + return ( + glms_vec2_distance2( + irodeo_math_rodeoVec2_to_cglmVec2(a), + irodeo_math_rodeoVec2_to_cglmVec2(b) + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_clamp(rodeo_math_vec2_t a, float minimum, float maximum) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_clamp( + irodeo_math_rodeoVec2_to_cglmVec2(a), + minimum, + maximum + ) + ); +} + +rodeo_math_vec2_t +rodeo_math_vec2_lerp(rodeo_math_vec2_t from, rodeo_math_vec2_t to, float t) +{ + return irodeo_math_cglmVec2_to_rodeoVec2( + glms_vec2_lerp( + irodeo_math_rodeoVec2_to_cglmVec2(from), + irodeo_math_rodeoVec2_to_cglmVec2(to), + t + ) + ); +} diff --git a/src/math/rodeo_vec3.c b/src/math/rodeo_vec3.c new file mode 100644 index 0000000..c94ecd3 --- /dev/null +++ b/src/math/rodeo_vec3.c @@ -0,0 +1,191 @@ + +// -- internal -- +// public +#include "rodeo/math.h" +#include "rodeo/math/vec3.h" +// private +#include "math/irodeo_math.h" +#include "math/irodeo_vec3.h" + +rodeo_math_vec3_t +irodeo_math_cglmVec3_to_rodeoVec3(vec3s in) +{ + return (rodeo_math_vec3_t){ + .raw[0] = in.raw[0], + .raw[1] = in.raw[1], + .raw[2] = in.raw[2] + }; +} + +vec3s +irodeo_math_rodeoVec3_to_cglmVec3(rodeo_math_vec3_t in) +{ + return (vec3s){ + .raw[0] = in.raw[0], + .raw[1] = in.raw[1], + .raw[2] = in.raw[2], + }; +} + +rodeo_math_vec3_t +rodeo_math_vec3_zero(void) +{ + return irodeo_math_cglmVec3_to_rodeoVec3(glms_vec3_zero()); +} + +rodeo_math_vec3_t +rodeo_math_vec3_one(void) +{ + return irodeo_math_cglmVec3_to_rodeoVec3(glms_vec3_one()); +} + +float +rodeo_math_vec3_dot(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return glms_vec3_dot( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ); +} + +float +rodeo_math_vec3_cross(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return glms_vec3_dot( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_add(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_add( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_subtract(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_sub( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_multiply(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_mul( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_scale(rodeo_math_vec3_t a, float b) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_scale( + irodeo_math_rodeoVec3_to_cglmVec3(a), + b + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_divide(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_div( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_negate(rodeo_math_vec3_t a) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_negate( + irodeo_math_rodeoVec3_to_cglmVec3(a) + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_normalize(rodeo_math_vec3_t a) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_normalize( + irodeo_math_rodeoVec3_to_cglmVec3(a) + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_rotate(rodeo_math_vec3_t a, float turns, rodeo_math_vec3_t axis) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_rotate( + irodeo_math_rodeoVec3_to_cglmVec3(a), + rodeo_math_radians_to_turns(turns), + irodeo_math_rodeoVec3_to_cglmVec3(axis) + ) + ); +} + +float +rodeo_math_vec3_distance(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return ( + glms_vec3_distance( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ) + ); +} + +float +rodeo_math_vec3_distanceSq(rodeo_math_vec3_t a, rodeo_math_vec3_t b) +{ + return ( + glms_vec3_distance2( + irodeo_math_rodeoVec3_to_cglmVec3(a), + irodeo_math_rodeoVec3_to_cglmVec3(b) + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_clamp(rodeo_math_vec3_t a, float minimum, float maximum) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_clamp( + irodeo_math_rodeoVec3_to_cglmVec3(a), + minimum, + maximum + ) + ); +} + +rodeo_math_vec3_t +rodeo_math_vec3_lerp(rodeo_math_vec3_t from, rodeo_math_vec3_t to, float t) +{ + return irodeo_math_cglmVec3_to_rodeoVec3( + glms_vec3_lerp( + irodeo_math_rodeoVec3_to_cglmVec3(from), + irodeo_math_rodeoVec3_to_cglmVec3(to), + t + ) + ); +} |
