From: Brendan Hansen Date: Mon, 19 Oct 2020 14:38:43 +0000 (-0500) Subject: changed to using c++ operator overloading for vec math funcs X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f5016b8eeb737aa933926843199ab712916114f4;p=csc718.git changed to using c++ operator overloading for vec math funcs --- diff --git a/include/vecmath.h b/include/vecmath.h index e3be4c3..4449d45 100644 --- a/include/vecmath.h +++ b/include/vecmath.h @@ -8,9 +8,9 @@ struct V2f f32 x, y; }; -V2f v2f_add (V2f a, V2f b); -V2f v2f_sub (V2f a, V2f b); -V2f v2f_mul (V2f a, f32 scalar); +V2f operator+(V2f a, V2f b); +V2f operator-(V2f a, V2f b); +V2f operator*(V2f a, f32 scalar); f32 v2f_dot (V2f a, V2f b); f32 v2f_smag(V2f a); f32 v2f_mag (V2f a); diff --git a/src/vecmath.cpp b/src/vecmath.cpp index 985da6a..4d653be 100644 --- a/src/vecmath.cpp +++ b/src/vecmath.cpp @@ -4,19 +4,19 @@ #include "utils.h" V2f -v2f_add(V2f a, V2f b) +operator+(V2f a, V2f b) { return (V2f) { .x = a.x + b.x, .y = a.y + b.y }; } V2f -v2f_sub(V2f a, V2f b) +operator-(V2f a, V2f b) { return (V2f) { .x = a.x - b.x, .y = a.y - b.y }; } V2f -v2f_mul(V2f a, f32 scalar) +operator*(V2f a, f32 scalar) { return (V2f) { .x = a.x * scalar, .y = a.y * scalar }; } @@ -43,14 +43,14 @@ V2f v2f_norm(V2f a) { const f32 mag = v2f_mag(a); - return v2f_mul(a, 1.0f / mag); + return a * (1.0f / mag); } V2f v2f_proj(V2f a, V2f onto) { const f32 dp = v2f_dot(a, onto) / v2f_mag(onto); - return v2f_mul(onto, dp); + return onto * dp; } @@ -92,7 +92,7 @@ mat4_mul(mat4 a, mat4 b, mat4* out) (*out)[row * 4 + col] = 0.0f; foreach(i, 0, 4) { - (*out)[row * 4 + col] += a[row * 4 + i] + b[i * 4 + col]; + (*out)[row * 4 + col] += a[row * 4 + i] * b[i * 4 + col]; } } }