#pragma once #include #include "vec4.h" namespace egm { template struct tmat4x4 { //a = Row 1 //b = Row 2 //c = Row 3 //d = Row 4 //x = Column 1 //y = Column 2 //z = Column 3 //w = Column 4 tvec4 a; tvec4 b; tvec4 c; tvec4 d; //Identity Matrix tmat4x4() : a(1, 0, 0, 0), b(0, 1, 0, 0), c(0, 0, 1, 0), d(0, 0, 0, 1) {} //Sets row by row tmat4x4(const tvec4& a, const tvec4& b, const tvec4& c, const tvec4& d) : a(a), b(b), c(c), d(d) {} //Sets by value (letter = row, number = column) tmat4x4( const T& a1, const T& a2, const T& a3, const T& a4, const T& b1, const T& b2, const T& b3, const T& b4, const T& c1, const T& c2, const T& c3, const T& c4, const T& d1, const T& d2, const T& d3, const T& d4 ) : a(a1, a2, a3, a4), b(b1, b2, b3, b4), c(c1, c2, c3, c4), d(d1, d2, d3, d4) {} tvec4& operator[](const unsigned int& index) { switch (index) { case 0: return a; case 1: return b; case 2: return c; case 3: return d; default: throw "Index Out Of Bounds"; } } bool operator==(const tmat4x4& o) { return (o.a == a && o.b == b && o.c == c && o.d == d); } tmat4x4& operator=(const tmat4x4& o) { a = o.a; b = o.b; c = o.c; d = o.d; return *this; } tmat4x4 operator+(const tmat4x4& o) const { return { a + o.a, b + o.b, c + o.c, d + o.d } } tmat4x4& operator+=(const tmat4x4& o) { a += o.a; b += o.b; c += o.c; d += o.d; return *this; } tmat4x4 operator-(const tmat4x4& o) const { return { a - o.a, b - o.b, c - o.c, d - o.d } } tmat4x4& operator-=(const tmat4x4& o) { a -= o.a; b -= o.b; c -= o.c; d -= o.d; return *this; } tvec4 operator*(const tvec4& v) const { return { a.x * v.x + a.y * v.y + a.z * v.z + a.w * v.w,//mRow 1 dot v b.x * v.x + b.y * v.y + b.z * v.z + b.w * v.w,//mRow 2 dot v c.x * v.x + c.y * v.y + c.z * v.z + c.w * v.w,//mRow 3 dot v d.x * v.x + d.y * v.y + d.z * v.z + d.w * v.w //mRow 4 dot v }; } tmat4x4 operator*(const tmat4x4& m) const { return { //Row 1a m.a.x * a.x + m.b.x * a.y + m.c.x * a.z + m.d.x * a.w,//Column 1b m.a.y * a.x + m.b.y * a.y + m.c.y * a.z + m.d.y * a.w,//Column 2b m.a.z * a.x + m.b.z * a.y + m.c.z * a.z + m.d.z * a.w,//Column 3b m.a.w * a.x + m.b.w * a.y + m.c.w * a.z + m.d.w * a.w,//Column 4b //Row 2a m.a.x * b.x + m.b.x * b.y + m.c.x * b.z + m.d.x * b.w,//Column 1b m.a.y * b.x + m.b.y * b.y + m.c.y * b.z + m.d.y * b.w,//Column 2b m.a.z * b.x + m.b.z * b.y + m.c.z * b.z + m.d.z * b.w,//Column 3b m.a.w * b.x + m.b.w * b.y + m.c.w * b.z + m.d.w * b.w,//Column 4b //Row 3a m.a.x * c.x + m.b.x * c.y + m.c.x * c.z + m.d.x * c.w,//Column 1b m.a.y * c.x + m.b.y * c.y + m.c.y * c.z + m.d.y * c.w,//Column 2b m.a.z * c.x + m.b.z * c.y + m.c.z * c.z + m.d.z * c.w,//Column 3b m.a.w * c.x + m.b.w * c.y + m.c.w * c.z + m.d.w * c.w,//Column 4b //Row 4a m.a.x * d.x + m.b.x * d.y + m.c.x * d.z + m.d.x * d.w,//Column 1b m.a.y * d.x + m.b.y * d.y + m.c.y * d.z + m.d.y * d.w,//Column 2b m.a.z * d.x + m.b.z * d.y + m.c.z * d.z + m.d.z * d.w,//Column 3b m.a.w * d.x + m.b.w * d.y + m.c.w * d.z + m.d.w * d.w,//Column 4b }; //Backwards /*return { //Row 1b a.x * m.a.x + b.x * m.a.y + c.x * m.a.z + d.x * m.a.w,//Column 1a a.y * m.a.x + b.y * m.a.y + c.y * m.a.z + d.y * m.a.w,//Column 2a a.z * m.a.x + b.z * m.a.y + c.z * m.a.z + d.z * m.a.w,//Column 3a a.w * m.a.x + b.w * m.a.y + c.w * m.a.z + d.w * m.a.w,//Column 4a //Row 2b a.x * m.b.x + b.x * m.b.y + c.x * m.b.z + d.x * m.b.w,//Column 1a a.y * m.b.x + b.y * m.b.y + c.y * m.b.z + d.y * m.b.w,//Column 2a a.z * m.b.x + b.z * m.b.y + c.z * m.b.z + d.z * m.b.w,//Column 3a a.w * m.b.x + b.w * m.b.y + c.w * m.b.z + d.w * m.b.w,//Column 4a //Row 3b a.x * m.c.x + b.x * m.c.y + c.x * m.c.z + d.x * m.c.w,//Column 1a a.y * m.c.x + b.y * m.c.y + c.y * m.c.z + d.y * m.c.w,//Column 2a a.z * m.c.x + b.z * m.c.y + c.z * m.c.z + d.z * m.c.w,//Column 3a a.w * m.c.x + b.w * m.c.y + c.w * m.c.z + d.w * m.c.w,//Column 4a //Row 4b a.x * m.d.x + b.x * m.d.y + c.x * m.d.z + d.x * m.d.w,//Column 1a a.y * m.d.x + b.y * m.d.y + c.y * m.d.z + d.y * m.d.w,//Column 2a a.z * m.d.x + b.z * m.d.y + c.z * m.d.z + d.z * m.d.w,//Column 3a a.w * m.d.x + b.w * m.d.y + c.w * m.d.z + d.w * m.d.w,//Column 4a };*/ } tmat4x4& operator*=(const tmat4x4& o) { *this = *this * o; } tmat4x4 operator*(const T& scalar) const { return { a * scalar, b * scalar, c * scalar, d * scalar } } tmat4x4& operator*=(const T& scalar) { a *= scalar; b *= scalar; c *= scalar; d *= scalar; return *this; } tmat4x4 operator/(const T& scalar) const { return { a / scalar, b / scalar, c / scalar, d / scalar } } tmat4x4& operator/=(const T& scalar) { a /= scalar; b /= scalar; c /= scalar; d /= scalar; return *this; } operator RECT() { return { a, b, c, d } } float* data() { return &(a.x); } }; typedef tmat4x4 mat4x4; typedef tmat4x4 dmat4x4; typedef tmat4x4 imat4x4; const mat4x4 IDENTITY_MATRIX( 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ); }