diff --git a/es-app/src/animations/LaunchAnimation.h b/es-app/src/animations/LaunchAnimation.h index e6df933fe..c9120f238 100644 --- a/es-app/src/animations/LaunchAnimation.h +++ b/es-app/src/animations/LaunchAnimation.h @@ -4,7 +4,6 @@ #include "animations/Animation.h" #include "Log.h" -#include "math/Scale3x3f.h" // let's look at the game launch effect: // -move camera to center on point P (interpolation method: linear) @@ -44,7 +43,7 @@ public: cameraOut = Transform4x4f::Identity(); float zoom = lerp(1.0, 4.25f, t*t); - cameraOut *= Scale3x3f(Vector3f(zoom, zoom, 1)); + cameraOut.scale(zoom); const float sw = (float)Renderer::getScreenWidth() / zoom; const float sh = (float)Renderer::getScreenHeight() / zoom; diff --git a/es-app/src/views/ViewController.cpp b/es-app/src/views/ViewController.cpp index f9a3f8e8c..ee41d1d6c 100644 --- a/es-app/src/views/ViewController.cpp +++ b/es-app/src/views/ViewController.cpp @@ -372,7 +372,8 @@ void ViewController::update(int deltaTime) void ViewController::render(const Transform4x4f& parentTrans) { Transform4x4f trans = mCamera * parentTrans; - Transform4x4f transInverse = trans.inverse(); + Transform4x4f transInverse; + transInverse.invert(trans); // camera position, position + size Vector3f viewStart = transInverse.translation(); diff --git a/es-app/src/views/gamelist/IGameListView.cpp b/es-app/src/views/gamelist/IGameListView.cpp index 7a70359f2..93586893b 100644 --- a/es-app/src/views/gamelist/IGameListView.cpp +++ b/es-app/src/views/gamelist/IGameListView.cpp @@ -46,8 +46,8 @@ void IGameListView::render(const Transform4x4f& parentTrans) { Transform4x4f trans = parentTrans * getTransform(); - float scaleX = trans[0]; - float scaleY = trans[5]; + float scaleX = trans.r0().x(); + float scaleY = trans.r1().y(); Vector2i pos(trans.translation()[0], trans.translation()[1]); Vector2i size(mSize.x() * scaleX, mSize.y() * scaleY); diff --git a/es-core/src/GuiComponent.cpp b/es-core/src/GuiComponent.cpp index ec380374c..8d0b633c5 100644 --- a/es-core/src/GuiComponent.cpp +++ b/es-core/src/GuiComponent.cpp @@ -4,8 +4,6 @@ #include "Renderer.h" #include "animations/AnimationController.h" #include "ThemeData.h" -#include "math/Rotation3x3f.h" -#include "math/Scale3x3f.h" GuiComponent::GuiComponent(Window* window) : mWindow(window), mParent(NULL), mOpacity(255), mPosition(Vector3f::Zero()), mOrigin(Vector2f::Zero()), mRotationOrigin(0.5, 0.5), @@ -247,7 +245,7 @@ const Transform4x4f& GuiComponent::getTransform() mTransform.translate(mPosition); if (mScale != 1.0) { - mTransform *= Scale3x3f(mScale); + mTransform.scale(mScale); } if (mRotation != 0.0) { @@ -259,8 +257,8 @@ const Transform4x4f& GuiComponent::getTransform() if (xOff != 0.0 || yOff != 0.0) mTransform.translate(Vector3f(xOff * -1, yOff * -1, 0.0f)); - // apply rotation transorm - mTransform *= Rotation3x3f(mRotation, Vector3f::UnitZ()); + // apply rotation transform + mTransform.rotateZ(mRotation); // Tranform back to original point if (xOff != 0.0 || yOff != 0.0) diff --git a/es-core/src/math/Matrix3x3f.h b/es-core/src/math/Matrix3x3f.h deleted file mode 100644 index d349f9b6b..000000000 --- a/es-core/src/math/Matrix3x3f.h +++ /dev/null @@ -1,78 +0,0 @@ -#pragma once -#ifndef ES_CORE_MATH_MATRIX3X3F_H -#define ES_CORE_MATH_MATRIX3X3F_H - -#include -#include - -class Matrix3x3f -{ -public: - - Matrix3x3f() { } - Matrix3x3f(const Vector3f& r0, const Vector3f& r1, const Vector3f& r2) : mR0(r0), mR1(r1), mR2(r2) { } - - const bool operator==(const Matrix3x3f& other) const { return ((mR0 == other.mR0) && (mR1 == other.mR1) && (mR2 == other.mR2)); } - const bool operator!=(const Matrix3x3f& other) const { return ((mR0 != other.mR0) || (mR1 != other.mR1) || (mR2 != other.mR2)); } - - const Matrix3x3f operator*(const Matrix3x3f& other) const - { - const float* tm = (float*)this; - const float* om = (float*)&other; - - return - { - { - tm[0] * om[0] + tm[1] * om[3] + tm[2] * om[6], - tm[0] * om[1] + tm[1] * om[4] + tm[2] * om[7], - tm[0] * om[2] + tm[1] * om[5] + tm[2] * om[8] - }, - { - tm[3] * om[0] + tm[4] * om[3] + tm[5] * om[6], - tm[3] * om[1] + tm[4] * om[4] + tm[5] * om[7], - tm[3] * om[2] + tm[4] * om[5] + tm[5] * om[8] - }, - { - tm[6] * om[0] + tm[7] * om[3] + tm[8] * om[6], - tm[6] * om[1] + tm[7] * om[4] + tm[8] * om[7], - tm[6] * om[2] + tm[7] * om[5] + tm[8] * om[8] - } - }; - } - - const Vector3f operator*(const Vector3f& other) const - { - const float* tm = (float*)this; - const float* ov = (float*)&other; - - return - { - tm[0] * ov[0] + tm[3] * ov[1] + tm[6] * ov[2], - tm[1] * ov[0] + tm[4] * ov[1] + tm[7] * ov[2], - tm[2] * ov[0] + tm[5] * ov[1] + tm[8] * ov[2] - }; - } - - Matrix3x3f& operator*=(const Matrix3x3f& other) { *this = *this * other; return *this; } - - float& operator[](const int index) { assert(index < 9 && "index out of range"); return ((float*)&mR0)[index]; } - const float& operator[](const int index) const { assert(index < 9 && "index out of range"); return ((float*)&mR0)[index]; } - - Vector3f& r0() { return mR0; } - Vector3f& r1() { return mR1; } - Vector3f& r2() { return mR2; } - const Vector3f& r0() const { return mR0; } - const Vector3f& r1() const { return mR1; } - const Vector3f& r2() const { return mR2; } - - static const Matrix3x3f Identity() { return { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; } - -protected: - - Vector3f mR0; - Vector3f mR1; - Vector3f mR2; - -}; - -#endif // ES_CORE_MATH_MATRIX3X3F_H diff --git a/es-core/src/math/Matrix4x4f.h b/es-core/src/math/Matrix4x4f.h deleted file mode 100644 index fe5e62c68..000000000 --- a/es-core/src/math/Matrix4x4f.h +++ /dev/null @@ -1,180 +0,0 @@ -#pragma once -#ifndef ES_CORE_MATH_MATRIX4X4F_H -#define ES_CORE_MATH_MATRIX4X4F_H - -#include -#include -#include - -class Matrix4x4f -{ -public: - - friend class Transform4x4f; - - Matrix4x4f() { } - Matrix4x4f(const Vector4f& r0, const Vector4f& r1, const Vector4f& r2, const Vector4f& r3) : mR0(r0), mR1(r1), mR2(r2), mR3(r3) { } - - const bool operator==(const Matrix4x4f& other) const { return ((mR0 == other.mR0) && (mR1 == other.mR1) && (mR2 == other.mR2) && (mR3 == other.mR3)); } - const bool operator!=(const Matrix4x4f& other) const { return ((mR0 != other.mR0) || (mR1 != other.mR1) || (mR2 != other.mR2) || (mR3 != other.mR3)); } - - const Matrix4x4f operator*(const Matrix4x4f& other) const - { - const float* tm = (float*)this; - const float* om = (float*)&other; - - return - { - { - tm[ 0] * om[ 0] + tm[ 1] * om[ 4] + tm[ 2] * om[ 8] + tm[ 3] * om[12], - tm[ 0] * om[ 1] + tm[ 1] * om[ 5] + tm[ 2] * om[ 9] + tm[ 3] * om[13], - tm[ 0] * om[ 2] + tm[ 1] * om[ 6] + tm[ 2] * om[10] + tm[ 3] * om[14], - tm[ 0] * om[ 3] + tm[ 1] * om[ 7] + tm[ 2] * om[11] + tm[ 3] * om[15] - }, - { - tm[ 4] * om[ 0] + tm[ 5] * om[ 4] + tm[ 6] * om[ 8] + tm[ 7] * om[12], - tm[ 4] * om[ 1] + tm[ 5] * om[ 5] + tm[ 6] * om[ 9] + tm[ 7] * om[13], - tm[ 4] * om[ 2] + tm[ 5] * om[ 6] + tm[ 6] * om[10] + tm[ 7] * om[14], - tm[ 4] * om[ 3] + tm[ 5] * om[ 7] + tm[ 6] * om[11] + tm[ 7] * om[15] - }, - { - tm[ 8] * om[ 0] + tm[ 9] * om[ 4] + tm[10] * om[ 8] + tm[11] * om[12], - tm[ 8] * om[ 1] + tm[ 9] * om[ 5] + tm[10] * om[ 9] + tm[11] * om[13], - tm[ 8] * om[ 2] + tm[ 9] * om[ 6] + tm[10] * om[10] + tm[11] * om[14], - tm[ 8] * om[ 3] + tm[ 9] * om[ 7] + tm[10] * om[11] + tm[11] * om[15] - }, - { - tm[12] * om[ 0] + tm[13] * om[ 4] + tm[14] * om[ 8] + tm[15] * om[12], - tm[12] * om[ 1] + tm[13] * om[ 5] + tm[14] * om[ 9] + tm[15] * om[13], - tm[12] * om[ 2] + tm[13] * om[ 6] + tm[14] * om[10] + tm[15] * om[14], - tm[12] * om[ 3] + tm[13] * om[ 7] + tm[14] * om[11] + tm[15] * om[15] - } - }; - } - - const Matrix4x4f operator*(const Matrix3x3f& other) const - { - const float* tm = (float*)this; - const float* om = (float*)&other; - - return - { - { - tm[ 0] * om[0] + tm[ 1] * om[3] + tm[ 2] * om[6], - tm[ 0] * om[1] + tm[ 1] * om[4] + tm[ 2] * om[7], - tm[ 0] * om[2] + tm[ 1] * om[5] + tm[ 2] * om[8], - tm[ 3] - }, - { - tm[ 4] * om[0] + tm[ 5] * om[3] + tm[ 6] * om[6], - tm[ 4] * om[1] + tm[ 5] * om[4] + tm[ 6] * om[7], - tm[ 4] * om[2] + tm[ 5] * om[5] + tm[ 6] * om[8], - tm[ 7] - }, - { - tm[ 8] * om[0] + tm[ 9] * om[3] + tm[10] * om[6], - tm[ 8] * om[1] + tm[ 9] * om[4] + tm[10] * om[7], - tm[ 8] * om[2] + tm[ 9] * om[5] + tm[10] * om[8], - tm[11] - }, - { - tm[12], - tm[13], - tm[14], - tm[15] - } - }; - } - - Matrix4x4f& operator*=(const Matrix4x4f& other) { *this = *this * other; return *this; } - Matrix4x4f& operator*=(const Matrix3x3f& other) { *this = *this * other; return *this; } - - float& operator[](const int index) { assert(index < 16 && "index out of range"); return ((float*)&mR0)[index]; } - const float& operator[](const int index) const { assert(index < 16 && "index out of range"); return ((float*)&mR0)[index]; } - - Vector4f& r0() { return mR0; } - Vector4f& r1() { return mR1; } - Vector4f& r2() { return mR2; } - Vector4f& r3() { return mR3; } - const Vector4f& r0() const { return mR0; } - const Vector4f& r1() const { return mR1; } - const Vector4f& r2() const { return mR2; } - const Vector4f& r3() const { return mR3; } - - Matrix4x4f& invert(const Matrix4x4f& other) - { - auto Determinant2x2 = [=](const float x1, const float x2, - const float y1, const float y2) - { - return x1 * y2 - x2 * y1; - }; - - auto Determinant3x3 = [=](const float x1, const float x2, const float x3, - const float y1, const float y2, const float y3, - const float z1, const float z2, const float z3) - { - return x1 * Determinant2x2(y2, y3, z2, z3) - - y1 * Determinant2x2(x2, x3, z2, z3) + - z1 * Determinant2x2(x2, x3, y2, y3); - }; - - float* tm = (float*)this; - const float* om = (float*)&other; - - tm[ 0] = Determinant3x3(om[ 5], om[ 6], om[ 7], om[ 9], om[10], om[11], om[13], om[14], om[15]); - tm[ 1] = -Determinant3x3(om[ 1], om[ 2], om[ 3], om[ 9], om[10], om[11], om[13], om[14], om[15]); - tm[ 2] = Determinant3x3(om[ 1], om[ 2], om[ 3], om[ 5], om[ 6], om[ 7], om[13], om[14], om[15]); - tm[ 3] = -Determinant3x3(om[ 1], om[ 2], om[ 3], om[ 5], om[ 6], om[ 7], om[ 9], om[10], om[11]); - tm[ 4] = -Determinant3x3(om[ 4], om[ 6], om[ 7], om[ 8], om[10], om[11], om[12], om[14], om[15]); - tm[ 5] = Determinant3x3(om[ 0], om[ 2], om[ 3], om[ 8], om[10], om[11], om[12], om[14], om[15]); - tm[ 6] = -Determinant3x3(om[ 0], om[ 2], om[ 3], om[ 4], om[ 6], om[ 7], om[12], om[14], om[15]); - tm[ 7] = Determinant3x3(om[ 0], om[ 2], om[ 3], om[ 4], om[ 6], om[ 7], om[ 8], om[10], om[11]); - tm[ 8] = Determinant3x3(om[ 4], om[ 5], om[ 7], om[ 8], om[ 9], om[11], om[12], om[13], om[15]); - tm[ 9] = -Determinant3x3(om[ 0], om[ 1], om[ 3], om[ 8], om[ 9], om[11], om[12], om[13], om[15]); - tm[10] = Determinant3x3(om[ 0], om[ 1], om[ 3], om[ 4], om[ 5], om[ 7], om[12], om[13], om[15]); - tm[11] = -Determinant3x3(om[ 0], om[ 1], om[ 3], om[ 4], om[ 5], om[ 7], om[ 8], om[ 9], om[11]); - tm[12] = -Determinant3x3(om[ 4], om[ 5], om[ 6], om[ 8], om[ 9], om[10], om[12], om[13], om[14]); - tm[13] = Determinant3x3(om[ 0], om[ 1], om[ 2], om[ 8], om[ 9], om[10], om[12], om[13], om[14]); - tm[14] = -Determinant3x3(om[ 0], om[ 1], om[ 2], om[ 4], om[ 5], om[ 6], om[12], om[13], om[14]); - tm[15] = Determinant3x3(om[ 0], om[ 1], om[ 2], om[ 4], om[ 5], om[ 6], om[ 8], om[ 9], om[10]); - - float Determinant = om[ 0] * tm[ 0] + - om[ 4] * tm[ 1] + - om[ 8] * tm[ 2] + - om[12] * tm[ 3]; - - if(Determinant != 0) - Determinant = 1 / Determinant; - - mR0 *= Determinant; - mR1 *= Determinant; - mR2 *= Determinant; - mR3 *= Determinant; - - return *this; - } - - Matrix4x4f& invert() - { - return invert(Matrix4x4f(*this)); - } - - Matrix4x4f inverse() - { - Matrix4x4f m; - m.invert(*this); - return m; - } - - static const Matrix4x4f Identity() { return { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; } - -protected: - - Vector4f mR0; - Vector4f mR1; - Vector4f mR2; - Vector4f mR3; - -}; - -#endif // ES_CORE_MATH_MATRIX4X4F_H diff --git a/es-core/src/math/Rotation3x3f.h b/es-core/src/math/Rotation3x3f.h deleted file mode 100644 index c7aeb09c3..000000000 --- a/es-core/src/math/Rotation3x3f.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#ifndef ES_CORE_MATH_ROTATION3X3F_H -#define ES_CORE_MATH_ROTATION3X3F_H - -class Rotation3x3f : public Matrix3x3f -{ -public: - - Rotation3x3f(const float angle, const Vector3f& axis) - { - float* tm = (float*)this; - const float* av = (float*)&axis; - - const float s = sin(-angle); - const float c = cos(-angle); - const float t = 1 - c; - const float x = av[0]; - const float y = av[1]; - const float z = av[2]; - const float tx = t * x; - const float ty = t * y; - const float tz = t * z; - const float sx = s * x; - const float sy = s * y; - const float sz = s * z; - - tm[0] = tx * x + c; - tm[1] = tx * y - sz; - tm[2] = tx * z + sy; - tm[3] = ty * x + sz; - tm[4] = ty * y + c; - tm[5] = ty * z - sy; - tm[6] = tz * x - sy; - tm[7] = tz * y + sx; - tm[8] = tz * z + c; - }; - -}; - -#endif // ES_CORE_MATH_ROTATION3X3F_H diff --git a/es-core/src/math/Scale3x3f.h b/es-core/src/math/Scale3x3f.h deleted file mode 100644 index 36b56ab07..000000000 --- a/es-core/src/math/Scale3x3f.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#ifndef ES_CORE_MATH_SCALE3X3F_H -#define ES_CORE_MATH_SCALE3X3F_H - -class Scale3x3f : public Matrix3x3f -{ -public: - - Scale3x3f(const Vector3f& scale) - { - float* tm = (float*)this; - const float* sv = (float*)&scale; - - tm[0] = sv[0]; - tm[1] = 0; - tm[2] = 0; - tm[3] = 0; - tm[4] = sv[1]; - tm[5] = 0; - tm[6] = 0; - tm[7] = 0; - tm[8] = sv[2]; - }; - -}; - -#endif // ES_CORE_MATH_SCALE3X3F_H diff --git a/es-core/src/math/Transform4x4f.h b/es-core/src/math/Transform4x4f.h index 9df4bf8b4..43c4c9e49 100644 --- a/es-core/src/math/Transform4x4f.h +++ b/es-core/src/math/Transform4x4f.h @@ -2,18 +2,18 @@ #ifndef ES_CORE_MATH_TRANSFORM4X4F_H #define ES_CORE_MATH_TRANSFORM4X4F_H -#include -#include +#include "math/Vector4f.h" +#include "math/Vector3f.h" +#include -class Transform4x4f : public Matrix4x4f +class Transform4x4f { public: - Transform4x4f() { } - Transform4x4f(const Matrix4x4f& m) : Matrix4x4f(m) { } - Transform4x4f(const Vector4f& r0, const Vector4f& r1, const Vector4f& r2, const Vector4f& r3) : Matrix4x4f(r0, r1, r2, r3) { } + Transform4x4f() { } + Transform4x4f(const Vector4f& r0, const Vector4f& r1, const Vector4f& r2, const Vector4f& r3) : mR0(r0), mR1(r1), mR2(r2), mR3(r3) { } - const Transform4x4f operator*(const Matrix4x4f& other) const + const Transform4x4f operator*(const Transform4x4f& other) const { const float* tm = (float*)this; const float* om = (float*)&other; @@ -47,40 +47,6 @@ public: }; } - const Transform4x4f operator*(const Matrix3x3f& other) const - { - const float* tm = (float*)this; - const float* om = (float*)&other; - - return - { - { - tm[ 0] * om[0] + tm[ 1] * om[3] + tm[ 2] * om[6], - tm[ 0] * om[1] + tm[ 1] * om[4] + tm[ 2] * om[7], - tm[ 0] * om[2] + tm[ 1] * om[5] + tm[ 2] * om[8], - 0 - }, - { - tm[ 4] * om[0] + tm[ 5] * om[3] + tm[ 6] * om[6], - tm[ 4] * om[1] + tm[ 5] * om[4] + tm[ 6] * om[7], - tm[ 4] * om[2] + tm[ 5] * om[5] + tm[ 6] * om[8], - 0 - }, - { - tm[ 8] * om[0] + tm[ 9] * om[3] + tm[10] * om[6], - tm[ 8] * om[1] + tm[ 9] * om[4] + tm[10] * om[7], - tm[ 8] * om[2] + tm[ 9] * om[5] + tm[10] * om[8], - 0 - }, - { - tm[12], - tm[13], - tm[14], - 1 - } - }; - } - const Vector3f operator*(const Vector3f& other) const { const float* tm = (float*)this; @@ -94,11 +60,213 @@ public: }; } - Transform4x4f& operator*=(const Matrix4x4f& other) { *this = *this * other; return *this; } - Transform4x4f& operator*=(const Matrix3x3f& other) { *this = *this * other; return *this; } + Transform4x4f& operator*=(const Transform4x4f& other) { *this = *this * other; return *this; } - inline Vector3f& translation() { return mR3.v3(); } - inline const Vector3f& translation() const { return mR3.v3(); } + inline Vector4f& r0() { return mR0; } + inline Vector4f& r1() { return mR1; } + inline Vector4f& r2() { return mR2; } + inline Vector4f& r3() { return mR3; } + inline const Vector4f& r0() const { return mR0; } + inline const Vector4f& r1() const { return mR1; } + inline const Vector4f& r2() const { return mR2; } + inline const Vector4f& r3() const { return mR3; } + + inline Transform4x4f& invert(const Transform4x4f& other) + { + float* tm = (float*)this; + const float* om = (float*)&other; + + // Full invert + // tm[ 0] = ((om[ 5] * (om[10] * om[15] - om[11] * om[14])) - (om[ 9] * (om[ 6] * om[15] - om[ 7] * om[14])) + (om[13] * (om[ 6] * om[11] - om[ 7] * om[10]))); + // tm[ 1] = -((om[ 1] * (om[10] * om[15] - om[11] * om[14])) - (om[ 9] * (om[ 2] * om[15] - om[ 3] * om[14])) + (om[13] * (om[ 2] * om[11] - om[ 3] * om[10]))); + // tm[ 2] = ((om[ 1] * (om[ 6] * om[15] - om[ 7] * om[14])) - (om[ 5] * (om[ 2] * om[15] - om[ 3] * om[14])) + (om[13] * (om[ 2] * om[ 7] - om[ 3] * om[ 6]))); + // tm[ 3] = -((om[ 1] * (om[ 6] * om[11] - om[ 7] * om[10])) - (om[ 5] * (om[ 2] * om[11] - om[ 3] * om[10])) + (om[ 9] * (om[ 2] * om[ 7] - om[ 3] * om[ 6]))); + // tm[ 4] = -((om[ 4] * (om[10] * om[15] - om[11] * om[14])) - (om[ 8] * (om[ 6] * om[15] - om[ 7] * om[14])) + (om[12] * (om[ 6] * om[11] - om[ 7] * om[10]))); + // tm[ 5] = ((om[ 0] * (om[10] * om[15] - om[11] * om[14])) - (om[ 8] * (om[ 2] * om[15] - om[ 3] * om[14])) + (om[12] * (om[ 2] * om[11] - om[ 3] * om[10]))); + // tm[ 6] = -((om[ 0] * (om[ 6] * om[15] - om[ 7] * om[14])) - (om[ 4] * (om[ 2] * om[15] - om[ 3] * om[14])) + (om[12] * (om[ 2] * om[ 7] - om[ 3] * om[ 6]))); + // tm[ 7] = ((om[ 0] * (om[ 6] * om[11] - om[ 7] * om[10])) - (om[ 4] * (om[ 2] * om[11] - om[ 3] * om[10])) + (om[ 8] * (om[ 2] * om[ 7] - om[ 3] * om[ 6]))); + // tm[ 8] = ((om[ 4] * (om[ 9] * om[15] - om[11] * om[13])) - (om[ 8] * (om[ 5] * om[15] - om[ 7] * om[13])) + (om[12] * (om[ 5] * om[11] - om[ 7] * om[ 9]))); + // tm[ 9] = -((om[ 0] * (om[ 9] * om[15] - om[11] * om[13])) - (om[ 8] * (om[ 1] * om[15] - om[ 3] * om[13])) + (om[12] * (om[ 1] * om[11] - om[ 3] * om[ 9]))); + // tm[10] = ((om[ 0] * (om[ 5] * om[15] - om[ 7] * om[13])) - (om[ 4] * (om[ 1] * om[15] - om[ 3] * om[13])) + (om[12] * (om[ 1] * om[ 7] - om[ 3] * om[ 5]))); + // tm[11] = -((om[ 0] * (om[ 5] * om[11] - om[ 7] * om[ 9])) - (om[ 4] * (om[ 1] * om[11] - om[ 3] * om[ 9])) + (om[ 8] * (om[ 1] * om[ 7] - om[ 3] * om[ 5]))); + // tm[12] = -((om[ 4] * (om[ 9] * om[14] - om[10] * om[13])) - (om[ 8] * (om[ 5] * om[14] - om[ 6] * om[13])) + (om[12] * (om[ 5] * om[10] - om[ 6] * om[ 9]))); + // tm[13] = ((om[ 0] * (om[ 9] * om[14] - om[10] * om[13])) - (om[ 8] * (om[ 1] * om[14] - om[ 2] * om[13])) + (om[12] * (om[ 1] * om[10] - om[ 2] * om[ 9]))); + // tm[14] = -((om[ 0] * (om[ 5] * om[14] - om[ 6] * om[13])) - (om[ 4] * (om[ 1] * om[14] - om[ 2] * om[13])) + (om[12] * (om[ 1] * om[ 6] - om[ 2] * om[ 5]))); + // tm[15] = ((om[ 0] * (om[ 5] * om[10] - om[ 6] * om[ 9])) - (om[ 4] * (om[ 1] * om[10] - om[ 2] * om[ 9])) + (om[ 8] * (om[ 1] * om[ 6] - om[ 2] * om[ 5]))); + + // Optimized invert ( om[3, 7 and 11] is always 0, and om[15] is always 1 ) + tm[ 0] = ((om[ 5] * om[10]) - (om[ 9] * om[ 6])); + tm[ 1] = -((om[ 1] * om[10]) - (om[ 9] * om[ 2])); + tm[ 2] = ((om[ 1] * om[ 6]) - (om[ 5] * om[ 2])); + tm[ 3] = 0; + tm[ 4] = -((om[ 4] * om[10]) - (om[ 8] * om[ 6])); + tm[ 5] = ((om[ 0] * om[10]) - (om[ 8] * om[ 2])); + tm[ 6] = -((om[ 0] * om[ 6]) - (om[ 4] * om[ 2])); + tm[ 7] = 0; + tm[ 8] = ((om[ 4] * om[ 9]) - (om[ 8] * om[ 5])); + tm[ 9] = -((om[ 0] * om[ 9]) - (om[ 8] * om[ 1])); + tm[10] = ((om[ 0] * om[ 5]) - (om[ 4] * om[ 1])); + tm[11] = 0; + tm[12] = -((om[ 4] * (om[ 9] * om[14] - om[10] * om[13])) - (om[ 8] * (om[ 5] * om[14] - om[ 6] * om[13])) + (om[12] * (om[ 5] * om[10] - om[ 6] * om[ 9]))); + tm[13] = ((om[ 0] * (om[ 9] * om[14] - om[10] * om[13])) - (om[ 8] * (om[ 1] * om[14] - om[ 2] * om[13])) + (om[12] * (om[ 1] * om[10] - om[ 2] * om[ 9]))); + tm[14] = -((om[ 0] * (om[ 5] * om[14] - om[ 6] * om[13])) - (om[ 4] * (om[ 1] * om[14] - om[ 2] * om[13])) + (om[12] * (om[ 1] * om[ 6] - om[ 2] * om[ 5]))); + tm[15] = 1; + + float Determinant = om[ 0] * tm[ 0] + + om[ 4] * tm[ 1] + + om[ 8] * tm[ 2] + + om[12] * tm[ 3]; + + if(Determinant != 0) + Determinant = 1 / Determinant; + + tm[ 0] *= Determinant; + tm[ 1] *= Determinant; + tm[ 2] *= Determinant; + tm[ 4] *= Determinant; + tm[ 5] *= Determinant; + tm[ 6] *= Determinant; + tm[ 8] *= Determinant; + tm[ 9] *= Determinant; + tm[10] *= Determinant; + tm[12] *= Determinant; + tm[13] *= Determinant; + tm[14] *= Determinant; + + return *this; + } + + inline Transform4x4f& scale(const Vector3f& scale) + { + float* tm = (float*)this; + const float* sv = (float*)&scale; + + tm[ 0] *= sv[0]; + tm[ 1] *= sv[1]; + tm[ 2] *= sv[2]; + tm[ 4] *= sv[0]; + tm[ 5] *= sv[1]; + tm[ 6] *= sv[2]; + tm[ 8] *= sv[0]; + tm[ 9] *= sv[1]; + tm[10] *= sv[2]; + + return *this; + } + + inline Transform4x4f& rotate(const float angle, const Vector3f& axis) + { + float* tm = (float*)this; + const float* av = (float*)&axis; + const float s = sin(-angle); + const float c = cos(-angle); + const float t = 1 - c; + const float x = av[0]; + const float y = av[1]; + const float z = av[2]; + const float tx = t * x; + const float ty = t * y; + const float tz = t * z; + const float sx = s * x; + const float sy = s * y; + const float sz = s * z; + const float r[9] = { tx * x + c, + tx * y - sz, + tx * z + sy, + ty * x + sz, + ty * y + c, + ty * z - sx, + tz * x - sy, + tz * y + sx, + tz * z + c }; + const float temp[9] = { tm[ 0] * r[0] + tm[ 1] * r[3] + tm[ 2] * r[6], + tm[ 0] * r[1] + tm[ 1] * r[4] + tm[ 2] * r[7], + tm[ 0] * r[2] + tm[ 1] * r[5] + tm[ 2] * r[8], + tm[ 4] * r[0] + tm[ 5] * r[3] + tm[ 6] * r[6], + tm[ 4] * r[1] + tm[ 5] * r[4] + tm[ 6] * r[7], + tm[ 4] * r[2] + tm[ 5] * r[5] + tm[ 6] * r[8], + tm[ 8] * r[0] + tm[ 9] * r[3] + tm[10] * r[6], + tm[ 8] * r[1] + tm[ 9] * r[4] + tm[10] * r[7], + tm[ 8] * r[2] + tm[ 9] * r[5] + tm[10] * r[8] }; + + tm[ 0] = temp[0]; + tm[ 1] = temp[1]; + tm[ 2] = temp[2]; + tm[ 4] = temp[3]; + tm[ 5] = temp[4]; + tm[ 6] = temp[5]; + tm[ 8] = temp[6]; + tm[ 9] = temp[7]; + tm[10] = temp[8]; + + return *this; + }; + + inline Transform4x4f& rotateX(const float angle) + { + float* tm = (float*)this; + const float s = sin(-angle); + const float c = cos(-angle); + const float temp[6] = { tm[ 1] * c + tm[ 2] * s, + tm[ 1] * -s + tm[ 2] * c, + tm[ 5] * c + tm[ 6] * s, + tm[ 5] * -s + tm[ 6] * c, + tm[ 9] * c + tm[10] * s, + tm[ 9] * -s + tm[10] * c }; + + tm[ 1] = temp[0]; + tm[ 2] = temp[1]; + tm[ 5] = temp[2]; + tm[ 6] = temp[3]; + tm[ 9] = temp[4]; + tm[10] = temp[5]; + + return *this; + }; + + inline Transform4x4f& rotateY(const float angle) + { + float* tm = (float*)this; + const float s = sin(-angle); + const float c = cos(-angle); + const float temp[6] = { tm[ 0] * c + tm[ 2] * -s, + tm[ 0] * s + tm[ 2] * c, + tm[ 4] * c + tm[ 6] * -s, + tm[ 4] * s + tm[ 6] * c, + tm[ 8] * c + tm[10] * -s, + tm[ 8] * s + tm[10] * c }; + + tm[ 0] = temp[0]; + tm[ 2] = temp[1]; + tm[ 4] = temp[2]; + tm[ 6] = temp[3]; + tm[ 8] = temp[4]; + tm[10] = temp[5]; + + return *this; + }; + + inline Transform4x4f& rotateZ(const float angle) + { + float* tm = (float*)this; + const float s = sin(-angle); + const float c = cos(-angle); + const float temp[6] = { tm[ 0] * c + tm[ 1] * s, + tm[ 0] * -s + tm[ 1] * c, + tm[ 4] * c + tm[ 5] * s, + tm[ 4] * -s + tm[ 5] * c, + tm[ 8] * c + tm[ 9] * s, + tm[ 8] * -s + tm[ 9] * c }; + + tm[ 0] = temp[0]; + tm[ 1] = temp[1]; + tm[ 4] = temp[2]; + tm[ 5] = temp[3]; + tm[ 8] = temp[4]; + tm[ 9] = temp[5]; + + return *this; + }; inline Transform4x4f& translate(const Vector3f& translation) { @@ -118,10 +286,23 @@ public: tm[12] = (int)(tm[12] + 0.5f); tm[13] = (int)(tm[13] + 0.5f); + tm[14] = (int)(tm[14] + 0.5f); return *this; } + inline Vector3f& translation() { return mR3.v3(); } + inline const Vector3f& translation() const { return mR3.v3(); } + + static const Transform4x4f Identity() { return { { 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }; } + +protected: + + Vector4f mR0; + Vector4f mR1; + Vector4f mR2; + Vector4f mR3; + }; #endif // ES_CORE_MATH_TRANSFORM4X4F_H