X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=gph-math;a=blobdiff_plain;f=src%2Fvector.cc;h=9532ccf3951571b24c8e7de4b31cb8099e580efa;hp=4af8dab34586f766137d1d06c0dbd9c4cb2794a7;hb=016badf08fbd4db92e6ee6e2ad74e7e61c5505e3;hpb=bdeb1048917667c00f540c17f397443e8f6f76bf diff --git a/src/vector.cc b/src/vector.cc index 4af8dab..9532ccf 100644 --- a/src/vector.cc +++ b/src/vector.cc @@ -8,6 +8,8 @@ Vector2::Vector2(const Vector3 &v) { } +// ---- Vector3 ---- + Vector3::Vector3(const Vector4 &v) : x(v.x), y(v.y), z(v.z) { @@ -29,9 +31,30 @@ Vector3 operator *(const Matrix4x4 &m, const Vector3 &v) return Vector3(x, y, z); } +// ---- Vector4 ---- + Vector4::Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) { } +Vector4 operator *(const Vector4 &v, const Matrix4x4 &m) +{ + float x = v.x * m[0][0] + v.y * m[1][0] + v.z * m[2][0] + v.w * m[3][0]; + float y = v.x * m[0][1] + v.y * m[1][1] + v.z * m[2][1] + v.w * m[3][1]; + float z = v.x * m[0][2] + v.y * m[1][2] + v.z * m[2][2] + v.w * m[3][2]; + float w = v.x * m[0][3] + v.y * m[1][3] + v.z * m[2][3] + v.w * m[3][3]; + return Vector4(x, y, z, w); +} + +Vector4 operator *(const Matrix4x4 &m, const Vector4 &v) +{ + float x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w; + float y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w; + float z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w; + float w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w; + return Vector4(x, y, z, w); +} + + } // namespace gph