fix: vector3 and vector4 functions

This commit is contained in:
2025-03-12 21:31:47 +01:00
committed by Loïc GUEZO
parent c734f39068
commit e10f9d8e75
2 changed files with 39 additions and 15 deletions

View File

@@ -53,7 +53,7 @@ Vec3* vec3Norm(Vec3* v)
if (length == 0.f) return vec3(0, 0, 0); if (length == 0.f) return vec3(0, 0, 0);
float invLength = 1.f / length; float invLength = 1.f / length;
return vec3(v->x * invLength, v->y * invLength, v->z * invLength); return vec3Scale(v, invLength);
} }
Vec3* vec3Lerp(Vec3* a, Vec3* b, float t) Vec3* vec3Lerp(Vec3* a, Vec3* b, float t)
@@ -90,18 +90,26 @@ Vec3* vec3Proj(Vec3* a, Vec3* b)
float dotAB = vec3Dot(a, b); float dotAB = vec3Dot(a, b);
float lenB2 = vec3Dot(b, b); float lenB2 = vec3Dot(b, b);
float scale = dotAB / lenB2; float scale = dotAB / lenB2;
return vec3(b->x * scale, b->y * scale, b->z * scale); return vec3Scale(b, scale);
} }
Vec3* vec3Refl(Vec3* v, Vec3* normal) Vec3* vec3Refl(Vec3* v, Vec3* normal)
{ {
if (!v || !normal) return NULL;
Vec3* proj = vec3Proj(v, normal); Vec3* proj = vec3Proj(v, normal);
return vec3Sub(v, vec3Scale(proj, 2.f)); Vec3* scal = vec3Scale(proj, 2.f);
Vec3* rlt = vec3Sub(v, scal);
vec3Free(proj);
vec3Free(scal);
return rlt;
} }
float vec3Dist(Vec3* a, Vec3* b) float vec3Dist(Vec3* a, Vec3* b)
{ {
return vec3Len(vec3Sub(a, b)); Vec3* vsub = vec3Sub(a, b);
float rlt = vec3Len(vsub);
vec3Free(vsub);
return rlt;
} }
Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle) Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle)
@@ -110,13 +118,22 @@ Vec3* vec3Rotate(Vec3* v, Vec3* axis, float angle)
float dot = vec3Dot(axis, v); float dot = vec3Dot(axis, v);
Vec3* cross = vec3Cross(axis, v); Vec3* cross = vec3Cross(axis, v);
return vec3Add( Vec3* vscal = vec3Scale(v, cosf(angle));
vec3Add( Vec3* cscal = vec3Scale(cross, sinf(angle));
vec3Scale(v, cosf(angle)),
vec3Scale(cross, sinf(angle)) Vec3* add = vec3Add(vscal, cscal);
), Vec3* dscal = vec3Scale(axis, dot * (1 - cosf(angle)));
vec3Scale(axis, dot * (1 - cosf(angle)))
); Vec3* rlt = vec3Add(add, dscal);
vec3Free(cross);
vec3Free(vscal);
vec3Free(cscal);
vec3Free(add);
vec3Free(dscal);
return rlt;
} }
void vec3Free(Vec3* v) void vec3Free(Vec3* v)

View File

@@ -54,7 +54,7 @@ Vec4* vec4Norm(Vec4* v)
if (length == 0.f) return vec4(0, 0, 0, 0); if (length == 0.f) return vec4(0, 0, 0, 0);
float invLength = 1.f / length; float invLength = 1.f / length;
return vec4(v->x * invLength, v->y * invLength, v->z * invLength, v->w * invLength); return vec4Scale(v, invLength);
} }
Vec4* vec4Lerp(Vec4* a, Vec4* b, float t) Vec4* vec4Lerp(Vec4* a, Vec4* b, float t)
@@ -85,19 +85,26 @@ Vec4* vec4Proj(Vec4* a, Vec4* b)
float dotAB = vec4Dot(a, b); float dotAB = vec4Dot(a, b);
float lenB2 = vec4Dot(b, b); float lenB2 = vec4Dot(b, b);
float scale = dotAB / lenB2; float scale = dotAB / lenB2;
return vec4(b->x * scale, b->y * scale, b->z * scale, 0); return vec4Scale(b, scale);
} }
Vec4* vec4Refl(Vec4* v, Vec4* normal) Vec4* vec4Refl(Vec4* v, Vec4* normal)
{ {
if (!v || !normal) return NULL; if (!v || !normal) return NULL;
Vec4* proj = vec4Proj(v, normal); Vec4* proj = vec4Proj(v, normal);
return vec4Sub(v, vec4Scale(proj, 2.f)); Vec4* scal = vec4Scale(proj, 2.f);
Vec4* rlt = vec4Sub(v, scal);
vec4Free(proj);
vec4Free(scal);
return rlt;
} }
float vec4Dist(Vec4* a, Vec4* b) float vec4Dist(Vec4* a, Vec4* b)
{ {
return vec4Len(vec4Sub(a, b)); Vec4* vsub = vec4Sub(a, b);
float rlt = vec4Len(vsub);
vec4Free(vsub);
return rlt;
} }
void vec4Free(Vec4* v) void vec4Free(Vec4* v)