feat: add lookAt function

This commit is contained in:
2025-08-29 21:40:30 +02:00
parent d94ccfbbaa
commit e056058ba3
5 changed files with 105 additions and 44 deletions

View File

@@ -34,3 +34,29 @@ Vec4f_t* viewport_r(Vec4f_t* vec, float width, float height)
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);
}

View File

@@ -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]);

View File

@@ -9,29 +9,33 @@
#define vec3_h
#ifndef SIMD
#if defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
#define SIMD_X86
#include <xmmintrin.h>
#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64)
#define SIMD_ARCH
#include <arm_neon.h>
#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);

View File

@@ -209,8 +209,8 @@ Vec4f_t vec4f_lerp(Vec4f_t a, Vec4f_t b, float t)
return a;
}
float vec4f_angle(Vec4f_t a, Vec4f_t b)
{
float vec4f_angle(Vec4f_t a, Vec4f_t b)
{
float lenA = vec4f_len(a);
float lenB = vec4f_len(b);
@@ -225,29 +225,45 @@ Vec4f_t vec4f_lerp(Vec4f_t a, Vec4f_t b, float t)
cosTheta = fmaxf(-1.f, fminf(cosTheta, 1.f));
return acosf(cosTheta);
}
}
Vec4f_t vec4f_proj(Vec4f_t a, Vec4f_t 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);
}
}
Vec4f_t vec4f_refl(Vec4f_t v, Vec4f_t normal)
{
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)
{
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;
}

View File

@@ -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