From 954037b4ea3729b834237fe8b63c442c504e080f Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Sun, 15 Jun 2025 18:51:30 -0400 Subject: [PATCH] some utils jazz --- utils/math/gryphn_mat4.h | 49 ++++++++++++++++++++++++++++++++++++++++ utils/math/gryphn_math.h | 3 +++ utils/math/gryphn_vec2.h | 2 ++ utils/math/gryphn_vec3.h | 11 +++++++++ 4 files changed, 65 insertions(+) create mode 100644 utils/math/gryphn_math.h diff --git a/utils/math/gryphn_mat4.h b/utils/math/gryphn_mat4.h index 2a0b1d1..687730f 100644 --- a/utils/math/gryphn_mat4.h +++ b/utils/math/gryphn_mat4.h @@ -1,5 +1,6 @@ #pragma once #include "math.h" +#include "gryphn_vec3.h" typedef struct gnMat4x4 { float mat[4][4]; @@ -45,3 +46,51 @@ static inline const gnMat4x4 gnProjection( } }; } + +static inline const gnMat4x4 gnTranslate(gnVec3 translation) { + return (gnMat4x4) { + .mat = { + { 1.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, 1.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, 1.0f, 0.0f }, + { translation.x, translation.y, translation.z, 1.0f } + } + }; +} + +static inline const gnMat4x4 gnRotate(gnVec3 axis, float rotation) { + float c = cos(rotation), s = sin(rotation); + + axis = gnVec3Normalize(axis); + gnVec3 temp = { + ((1.0f - c) * axis.x), + ((1.0f - c) * axis.y), + ((1.0f - c) * axis.z) + }; + + gnMat4 rotate = { + .mat = { + { c + temp.x * axis.x, temp.x * axis.y + s * axis.z, temp.x * axis.z - s * axis.y, 0.0f }, + { temp.y * axis.x - s * axis.z, c + temp.y * axis.y, temp.y * axis.z + s * axis.x, 0.0f }, + { temp.z * axis.x + s * axis.y, temp.z * axis.y - s * axis.x, c + temp.z * axis.z, 0.0f }, + { 0.0f, 0.0f, 0.0f, 1.0f }, + } + }; + return rotate; +} + +// used GLMs lookAtLH from /glm/ext/matrix_transform.inl as a base +static inline const gnMat4x4 gnLookAt(gnVec3 eye, gnVec3 center, gnVec3 up) { + const gnVec3 f = gnVec3Normalize(gnVec3Subtract(center, eye)); + const gnVec3 s = gnVec3Normalize(gnVec3Cross(f, up)); + const gnVec3 u = gnVec3Cross(s, f); + + return (gnMat4x4) { + .mat = { + { s.x, u.x, -f.x, 0.0f }, + { s.y, u.y, -f.y, 0.0f }, + { s.z, u.z, -f.z, 0.0f }, + { -gnVec3Dot(s, eye), -gnVec3Dot(u, eye), gnVec3Dot(f, eye), 1.0f } + } + }; +} diff --git a/utils/math/gryphn_math.h b/utils/math/gryphn_math.h new file mode 100644 index 0000000..220031d --- /dev/null +++ b/utils/math/gryphn_math.h @@ -0,0 +1,3 @@ +#pragma once + +static const inline float gnRadians(const float degrees) { return degrees * (3.14159265358979323846f / 180.0f); } diff --git a/utils/math/gryphn_vec2.h b/utils/math/gryphn_vec2.h index c3c8541..31cf1ed 100644 --- a/utils/math/gryphn_vec2.h +++ b/utils/math/gryphn_vec2.h @@ -25,6 +25,8 @@ typedef struct gnVec2 { #endif } gnVec2; +static inline const gnVec2 gnVec2Subtract(gnVec2 a, gnVec2 b) { return (gnVec2){ a.x - b.x, a.y - b.y }; } + typedef gnVec2 gnFVec2; typedef gnVec2 gnFloat2; diff --git a/utils/math/gryphn_vec3.h b/utils/math/gryphn_vec3.h index 7f424e1..1f7a25f 100644 --- a/utils/math/gryphn_vec3.h +++ b/utils/math/gryphn_vec3.h @@ -1,5 +1,6 @@ #pragma once #include "stdint.h" +#include "math.h" typedef struct gnVec3 { union { @@ -26,6 +27,16 @@ typedef struct gnVec3 { #endif } gnVec3; +static const inline gnVec3 gnVec3Subtract(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x - b.x, a.y - b.y, a.z - b.z }; } +static const inline gnVec3 gnVec3Add(gnVec3 a, gnVec3 b) { return (gnVec3){ a.x + b.x, a.y + b.y, a.z + b.z }; } +static const inline gnVec3 gnVec3Normalize(gnVec3 in) { + float lengthOfVector = sqrt((in.x * in.x) + (in.y * in.y) + (in.z * in.z)); + return (gnVec3){in.x / lengthOfVector, in.y / lengthOfVector, in.z / lengthOfVector}; +} +static const inline gnVec3 gnVec3Cross(gnVec3 a, gnVec3 b) { return (gnVec3){a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } +static const inline float gnVec3Dot(gnVec3 a, gnVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } + + typedef gnVec3 gnFVec3; typedef gnVec3 gnFloat3;