From e056058ba32499a66e0895cdca58ce2f4ff1284f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Fri, 29 Aug 2025 21:40:30 +0200 Subject: [PATCH] feat: add lookAt function --- src/cam.c | 26 ++++++++++++++++ src/math/mat4.h | 11 +++++-- src/math/vec3.h | 14 ++++++--- src/math/vec4.c | 82 +++++++++++++++++++++++++++++-------------------- src/math/vec4.h | 16 ++++++++-- 5 files changed, 105 insertions(+), 44 deletions(-) diff --git a/src/cam.c b/src/cam.c index 85dec46..957f87a 100644 --- a/src/cam.c +++ b/src/cam.c @@ -33,4 +33,30 @@ Vec4f_t* viewport_r(Vec4f_t* vec, float width, float height) vec->x = (vec->x + 1.f) * 0.5f * width; vec->y = (1.f - vec->y) * 0.5f * height; return vec; +} + +Mat4f_t lookAt(Vec4f_t* eye, Vec4f_t* center, Vec4f_t* up) +{ + Vec4f_t f = vec4f_sub(*center, *eye); + f.data[3] = 0.f; + f = vec4f_scale(f, 1.f / vec4f_len(f)); + + Vec4f_t s = vec4f_cross(f, *up); + s.data[3] = 0.f; + s = vec4f_scale(s, 1.f / vec4f_len(s)); + + Vec4f_t u = vec4f_cross(s, f); + u.data[3] = 0.f; + + const float m[16] = { + s.x, u.x, -f.x, 0.f, + s.y, u.y, -f.y, 0.f, + s.z, u.z, -f.z, 0.f, + -vec4f_dot(s, *eye), + -vec4f_dot(u, *eye), + vec4f_dot(f, *eye), + 1.f + }; + + return mat4f_from_array(m); } \ No newline at end of file diff --git a/src/math/mat4.h b/src/math/mat4.h index 692910f..194a241 100644 --- a/src/math/mat4.h +++ b/src/math/mat4.h @@ -4,24 +4,29 @@ #define MAT_SIZE 16 #define MAT_DIM 4 +#ifndef SIMD #if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) #define SIMD_X86 + #elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) #define SIMD_ARCH + #else #endif +#define SIMD +#endif #ifdef _MSC_VER - #define ALIGN16 __declspec(align(MAT_SIZE)) + #define ALIGN16_MAT __declspec(align(MAT_SIZE)) #else - #define ALIGN16 __attribute__((aligned(MAT_SIZE))) + #define ALIGN16_MAT __attribute__((aligned(MAT_SIZE))) #endif typedef struct { float m[MAT_SIZE]; -} ALIGN16 Mat4f_t; +} ALIGN16_MAT Mat4f_t; Mat4f_t* mat4f_from_array_r(Mat4f_t *__restrict m, const float arr[MAT_SIZE]); diff --git a/src/math/vec3.h b/src/math/vec3.h index 260906e..7b6cf25 100644 --- a/src/math/vec3.h +++ b/src/math/vec3.h @@ -9,29 +9,33 @@ #define vec3_h +#ifndef SIMD #if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) #define SIMD_X86 - #include #elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) #define SIMD_ARCH - #include #else #endif +#define SIMD +#endif +#ifndef ALIGN16_VEC #ifdef _MSC_VER - #define ALIGN16 __declspec(align(16)) + #define ALIGN16_VEC __declspec(align(16)) #else - #define ALIGN16 __attribute__((aligned(16))) + #define ALIGN16_VEC __attribute__((aligned(16))) #endif +#endif + #define VEC_SIZE 3 typedef union { struct { float x, y, z; }; float data[4]; -} ALIGN16 Vec3f_t; +} ALIGN16_VEC Vec3f_t; Vec3f_t vec3f_from_array(const float *__restrict val); Vec3f_t vec3f(float x, float y, float z); diff --git a/src/math/vec4.c b/src/math/vec4.c index 61e26de..c9858be 100644 --- a/src/math/vec4.c +++ b/src/math/vec4.c @@ -209,45 +209,61 @@ Vec4f_t vec4f_lerp(Vec4f_t a, Vec4f_t b, float t) return a; } - float vec4f_angle(Vec4f_t a, Vec4f_t b) - { - float lenA = vec4f_len(a); - float lenB = vec4f_len(b); +float vec4f_angle(Vec4f_t a, Vec4f_t b) +{ + float lenA = vec4f_len(a); + float lenB = vec4f_len(b); - if (isnan(lenA) || isnan(lenB) - || lenA < FLT_EPSILON - || lenB < FLT_EPSILON) - return NAN; + if (isnan(lenA) || isnan(lenB) + || lenA < FLT_EPSILON + || lenB < FLT_EPSILON) + return NAN; - float dot = vec4f_dot(a, b); - float cosTheta = dot / (lenA * lenB); + float dot = vec4f_dot(a, b); + float cosTheta = dot / (lenA * lenB); - cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f)); + cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f)); - return acosf(cosTheta); - } + return acosf(cosTheta); +} - Vec4f_t vec4f_proj(Vec4f_t a, Vec4f_t b) - { - float dotA = vec4f_dot(a, b); - float dotB = vec4f_dot(b, b); +Vec4f_t vec4f_proj(Vec4f_t a, Vec4f_t b) +{ + float dotA = vec4f_dot(a, b); + float dotB = vec4f_dot(b, b); - float scale = dotA / dotB; - return vec4f_scale(b, scale); - } + float scale = dotA / dotB; + return vec4f_scale(b, scale); +} - Vec4f_t vec4f_refl(Vec4f_t v, Vec4f_t normal) - { - Vec4f_t proj = vec4f_proj(v, normal); - Vec4f_t scal = vec4f_scale(proj, 2.f); - Vec4f_t rlt = vec4f_sub(v, scal); - return rlt; - } +Vec4f_t vec4f_refl(Vec4f_t v, Vec4f_t normal) +{ + Vec4f_t proj = vec4f_proj(v, normal); + Vec4f_t scal = vec4f_scale(proj, 2.f); + Vec4f_t rlt = vec4f_sub(v, scal); + return rlt; +} - float vec4f_dist(Vec4f_t a, Vec4f_t b) - { - Vec4f_t vsub = vec4f_sub(a, b); - float rlt = vec4f_len(vsub); - return rlt; - } +float vec4f_dist(Vec4f_t a, Vec4f_t b) +{ + Vec4f_t vsub = vec4f_sub(a, b); + float rlt = vec4f_len(vsub); + return rlt; +} + +Vec4f_t* vec4f_cross_r(Vec4f_t* __restrict out, Vec4f_t a, Vec4f_t b) +{ + out->x = a.y * b.z - a.z * b.y; + out->y = a.z * b.x - a.x * b.z; + out->z = a.x * b.y - a.y * b.x; + out->w = 0.f; + return out; +} + +Vec4f_t vec4f_cross(Vec4f_t a, Vec4f_t b) +{ + Vec4f_t out; + vec4f_cross_r(&out, a, b); + return out; +} diff --git a/src/math/vec4.h b/src/math/vec4.h index 956fc4c..e726bdf 100644 --- a/src/math/vec4.h +++ b/src/math/vec4.h @@ -1,6 +1,7 @@ #ifndef VECTOR4_H #define VECTOR4_H +#ifndef SIMD #if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64) #define SIMD_X86 @@ -9,11 +10,16 @@ #else #endif +#define SIMD +#endif + +#ifndef ALIGN16_VEC #ifdef _MSC_VER - #define ALIGN16 __declspec(align(16)) + #define ALIGN16_VEC __declspec(align(16)) #else - #define ALIGN16 __attribute__((aligned(16))) + #define ALIGN16_VEC __attribute__((aligned(16))) +#endif #endif #define VEC_SIZE 4 @@ -25,7 +31,7 @@ typedef union { struct { float x, y, z, w; }; float data[VEC_SIZE]; -} ALIGN16 Vec4f_t; +} ALIGN16_VEC Vec4f_t; Vec4f_t vec4f_from_array(const float *__restrict val); Vec4f_t vec4f(float x, float y, float z, float w); @@ -66,6 +72,10 @@ Vec4f_t vec4f_refl(Vec4f_t v, Vec4f_t normal); float vec4f_dist(Vec4f_t a, Vec4f_t b); +Vec4f_t* vec4f_cross_r(Vec4f_t* __restrict out, Vec4f_t a, Vec4f_t b); + +Vec4f_t vec4f_cross(Vec4f_t a, Vec4f_t b); + #undef VEC_SIZE #endif // VECTOR4_H