From 9810d57a394b2bb56fbcfddb05d34dcd504a7b35 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 11 Mar 2014 18:32:33 +0200 Subject: Unified Matrix4 code --- src/Matrix4.h | 228 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 src/Matrix4.h (limited to 'src/Matrix4.h') diff --git a/src/Matrix4.h b/src/Matrix4.h new file mode 100644 index 000000000..14ccb94fd --- /dev/null +++ b/src/Matrix4.h @@ -0,0 +1,228 @@ + +#pragma once + + + + +#include + + + + + +template +// tolua_begin +class Matrix4 +{ + + TOLUA_TEMPLATE_BIND((T, float, double)) + + // tolua_end + +public: + + T cell[16]; + + enum + { + TX=3, TY=7, TZ=11, + D0=0, D1=5, D2=10, D3=15, + SX=D0, SY=D1, SZ=D2, + W=D3 + }; + + // tolua_begin + + inline Matrix4(void) + { + Identity(); + } + + inline Matrix4(const Matrix4 & a_Rhs) + { + *this = a_Rhs; + } + + inline Matrix4 & operator = (const Matrix4 & a_Rhs) + { + for (unsigned int i = 0; i < 16; ++i) + { + cell[i] = a_Rhs.cell[i]; + } + return *this; + } + + inline T & operator [] (int a_N) + { + ASSERT(a_N < 16); + return cell[a_N]; + } + + inline void Identity() + { + cell[1] = cell[2] = cell[TX] = cell[4] = cell[6] = cell[TY] = + cell[8] = cell[9] = cell[TZ] = cell[12] = cell[13] = cell[14] = 0; + cell[D0] = cell[D1] = cell[D2] = cell[W] = 1; + } + + inline void Init(const Vector3 & a_Pos, T a_RX, T a_RY, T a_RZ) + { + Matrix4 t; + t.RotateX(a_RZ); + RotateY(a_RY); + Concatenate(t); + t.RotateZ(a_RX); + Concatenate(t); + Translate(a_Pos); + } + + inline void RotateX(T a_RX) + { + T sx = (T) sin(a_RX * M_PI / 180); + T cx = (T) cos(a_RX * M_PI / 180); + + Identity(); + + cell[5] = cx, cell[6] = sx, cell[9] = -sx, cell[10] = cx; + } + + inline void RotateY(T a_RY) + { + T sy = (T) sin(a_RY * M_PI / 180); + T cy = (T) cos(a_RY * M_PI / 180); + + Identity(); + + cell[0] = cy, cell[2] = -sy, cell[8] = sy, cell[10] = cy; + } + + inline void RotateZ(T a_RZ) + { + T sz = (T) sin(a_RZ * M_PI / 180); + T cz = (T) cos(a_RZ * M_PI / 180); + + Identity(); + + cell[0] = cz; cell[1] = sz; + cell[4] = -sz; cell[5] = cz; + } + + inline void Translate(const Vector3 & a_Pos) + { + cell[TX] += a_Pos.x; + cell[TY] += a_Pos.y; + cell[TZ] += a_Pos.z; + } + + inline void SetTranslation(const Vector3 & a_Pos) + { + cell[TX] = a_Pos.x; + cell[TY] = a_Pos.y; + cell[TZ] = a_Pos.z; + } + + inline void Concatenate(const Matrix4 & m2) + { + Matrix4 res; + + for (unsigned int c = 0; c < 4; ++c) + { + for (unsigned int r = 0; r < 4; ++r) + { + res.cell[r * 4 + c] = ( + cell[r * 4] * m2.cell[c] + + cell[r * 4 + 1] * m2.cell[c + 4] + + cell[r * 4 + 2] * m2.cell[c + 8] + + cell[r * 4 + 3] * m2.cell[c + 12] + ); + } + } + + *this = res; + } + + inline Vector3 Transform(const Vector3 & v) const + { + T x = cell[0] * v.x + cell[1] * v.y + cell[2] * v.z + cell[3]; + T y = cell[4] * v.x + cell[5] * v.y + cell[6] * v.z + cell[7]; + T z = cell[8] * v.x + cell[9] * v.y + cell[10] * v.z + cell[11]; + + return Vector3(x, y, z); + } + + inline void Invert(void) + { + Matrix4 t; + + T tx = -cell[3]; + T ty = -cell[7]; + T tz = -cell[11]; + + for (unsigned int h = 0; h < 3; ++h) + { + for (unsigned int v = 0; v < 3; ++v) + { + t.cell[h + v * 4] = cell[v + h * 4]; + } + } + + for (unsigned int i = 0; i < 11; ++i) + { + cell[i] = t.cell[i]; + } + + cell[3] = tx * cell[0] + ty * cell[1] + tz * cell[2]; + cell[7] = tx * cell[4] + ty * cell[5] + tz * cell[6]; + cell[11] = tx * cell[8] + ty * cell[9] + tz * cell[10]; + } + + inline Vector3 GetXColumn(void) const + { + return Vector3(cell[0], cell[1], cell[2]); + } + + inline Vector3 GetYColumn(void) const + { + return Vector3(cell[4], cell[5], cell[6]); + } + + inline Vector3 GetZColumn(void) const + { + return Vector3(cell[8], cell[9], cell[10]); + } + + inline void SetXColumn(const Vector3 & a_X) + { + cell[0] = a_X.x; + cell[1] = a_X.y; + cell[2] = a_X.z; + } + + inline void SetYColumn(const Vector3 & a_Y) + { + cell[4] = a_Y.x; + cell[5] = a_Y.y; + cell[6] = a_Y.z; + } + + inline void SetZColumn(const Vector3 & a_Z) + { + cell[8] = a_Z.x; + cell[9] = a_Z.y; + cell[10] = a_Z.z; + } +}; +// tolua_end + + + + +// tolua_begin +typedef Matrix4 Matrix4d; +typedef Matrix4 Matrix4f; +// tolua_end + + + + + -- cgit v1.2.3 From abf4effaaf0eff1a98e005512f805211c2fad9a7 Mon Sep 17 00:00:00 2001 From: andrew Date: Tue, 11 Mar 2014 21:58:50 +0200 Subject: Matrix4: Removed enum --- src/Matrix4.h | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) (limited to 'src/Matrix4.h') diff --git a/src/Matrix4.h b/src/Matrix4.h index 14ccb94fd..a20c019e9 100644 --- a/src/Matrix4.h +++ b/src/Matrix4.h @@ -23,14 +23,6 @@ public: T cell[16]; - enum - { - TX=3, TY=7, TZ=11, - D0=0, D1=5, D2=10, D3=15, - SX=D0, SY=D1, SZ=D2, - W=D3 - }; - // tolua_begin inline Matrix4(void) @@ -60,9 +52,11 @@ public: inline void Identity() { - cell[1] = cell[2] = cell[TX] = cell[4] = cell[6] = cell[TY] = - cell[8] = cell[9] = cell[TZ] = cell[12] = cell[13] = cell[14] = 0; - cell[D0] = cell[D1] = cell[D2] = cell[W] = 1; + cell[1] = cell[2] = cell[3] = cell[4] = 0; + cell[6] = cell[7] = cell[8] = cell[9] = 0; + cell[11] = cell[12] = cell[13] = cell[14] = 0; + + cell[0] = cell[5] = cell[10] = cell[15] = 1; } inline void Init(const Vector3 & a_Pos, T a_RX, T a_RY, T a_RZ) @@ -83,7 +77,8 @@ public: Identity(); - cell[5] = cx, cell[6] = sx, cell[9] = -sx, cell[10] = cx; + cell[5] = cx; cell[6] = sx; + cell[9] = -sx; cell[10] = cx; } inline void RotateY(T a_RY) @@ -93,7 +88,8 @@ public: Identity(); - cell[0] = cy, cell[2] = -sy, cell[8] = sy, cell[10] = cy; + cell[0] = cy; cell[2] = -sy; + cell[8] = sy; cell[10] = cy; } inline void RotateZ(T a_RZ) @@ -109,16 +105,16 @@ public: inline void Translate(const Vector3 & a_Pos) { - cell[TX] += a_Pos.x; - cell[TY] += a_Pos.y; - cell[TZ] += a_Pos.z; + cell[3] += a_Pos.x; + cell[7] += a_Pos.y; + cell[11] += a_Pos.z; } inline void SetTranslation(const Vector3 & a_Pos) { - cell[TX] = a_Pos.x; - cell[TY] = a_Pos.y; - cell[TZ] = a_Pos.z; + cell[3] = a_Pos.x; + cell[7] = a_Pos.y; + cell[11] = a_Pos.z; } inline void Concatenate(const Matrix4 & m2) @@ -130,7 +126,7 @@ public: for (unsigned int r = 0; r < 4; ++r) { res.cell[r * 4 + c] = ( - cell[r * 4] * m2.cell[c] + + cell[r * 4 + 0] * m2.cell[c + 0] + cell[r * 4 + 1] * m2.cell[c + 4] + cell[r * 4 + 2] * m2.cell[c + 8] + cell[r * 4 + 3] * m2.cell[c + 12] @@ -207,8 +203,8 @@ public: inline void SetZColumn(const Vector3 & a_Z) { - cell[8] = a_Z.x; - cell[9] = a_Z.y; + cell[8] = a_Z.x; + cell[9] = a_Z.y; cell[10] = a_Z.z; } }; -- cgit v1.2.3 From 6f2bb0ad44592ac42334dc627afd3ad734b7a4ab Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 12 Mar 2014 16:13:03 +0200 Subject: M_PI MSVC Fix --- src/Matrix4.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Matrix4.h') diff --git a/src/Matrix4.h b/src/Matrix4.h index a20c019e9..456677f0f 100644 --- a/src/Matrix4.h +++ b/src/Matrix4.h @@ -3,7 +3,7 @@ - +#define _USE_MATH_DEFINES // Enable non-standard math defines (MSVC) #include -- cgit v1.2.3