diff options
author | Stephan Unverwerth <s.unverwerth@gmx.de> | 2021-05-13 19:59:57 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-13 22:24:42 +0200 |
commit | 0833db08745f45e89d9ac14901336884b8662255 (patch) | |
tree | 09a72e4883d71805c2c79ee142d7e88771a18471 /Userland | |
parent | a5194274afcd626557f9e0658da2ddc57b22d837 (diff) | |
download | serenity-0833db08745f45e89d9ac14901336884b8662255.zip |
LibGfx: Make Matrix class consistently row-major
Matrix elements were interpreted in different ways.
This makes it definitely row-major, allowing initialization via
initializer list in a standard scientific order. Also matrix
multiplication now happens in the correct order and accessing
elements happens as m_elements[row][column].
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Demos/GLTeapot/main.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibGL/GLMat.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Matrix.h | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Matrix4x4.h | 43 |
4 files changed, 49 insertions, 31 deletions
diff --git a/Userland/Demos/GLTeapot/main.cpp b/Userland/Demos/GLTeapot/main.cpp index de79da0753..9e99b7d6d0 100644 --- a/Userland/Demos/GLTeapot/main.cpp +++ b/Userland/Demos/GLTeapot/main.cpp @@ -87,6 +87,10 @@ void GLContextWidget::timer_event(Core::TimerEvent&) * FloatMatrix4x4::rotate(FloatVector3(0, 1, 0), 0.0f) * FloatMatrix4x4::rotate(FloatVector3(0, 0, 1), angle); + // We need to transpose here because OpenGL expects matrices in column major order + // but our matrix class stores elements in row major order. + matrix = matrix.transpose(); + glMatrixMode(GL_MODELVIEW); glLoadMatrixf((float*)matrix.elements()); diff --git a/Userland/Libraries/LibGL/GLMat.cpp b/Userland/Libraries/LibGL/GLMat.cpp index a80a54990b..1371f7c49e 100644 --- a/Userland/Libraries/LibGL/GLMat.cpp +++ b/Userland/Libraries/LibGL/GLMat.cpp @@ -37,11 +37,14 @@ void glPopMatrix() void glLoadMatrixf(const GLfloat* matrix) { + // Transpose the matrix here because glLoadMatrix expects elements + // in column major order but out Matrix class stores elements in + // row major order. FloatMatrix4x4 mat( - matrix[0], matrix[1], matrix[2], matrix[3], - matrix[4], matrix[5], matrix[6], matrix[7], - matrix[8], matrix[9], matrix[10], matrix[11], - matrix[12], matrix[13], matrix[14], matrix[15]); + matrix[0], matrix[4], matrix[8], matrix[12], + matrix[1], matrix[5], matrix[9], matrix[13], + matrix[2], matrix[6], matrix[10], matrix[14], + matrix[3], matrix[7], matrix[11], matrix[15]); g_gl_context->gl_load_matrix(mat); } diff --git a/Userland/Libraries/LibGfx/Matrix.h b/Userland/Libraries/LibGfx/Matrix.h index 5a85eebca7..b348c36e43 100644 --- a/Userland/Libraries/LibGfx/Matrix.h +++ b/Userland/Libraries/LibGfx/Matrix.h @@ -49,23 +49,23 @@ public: auto& element = product.m_elements[i][j]; if constexpr (N == 4) { - element = m_elements[0][j] * other.m_elements[i][0] - + m_elements[1][j] * other.m_elements[i][1] - + m_elements[2][j] * other.m_elements[i][2] - + m_elements[3][j] * other.m_elements[i][3]; + element = m_elements[i][0] * other.m_elements[0][j] + + m_elements[i][1] * other.m_elements[1][j] + + m_elements[i][2] * other.m_elements[2][j] + + m_elements[i][3] * other.m_elements[3][j]; } else if constexpr (N == 3) { - element = m_elements[0][j] * other.m_elements[i][0] - + m_elements[1][j] * other.m_elements[i][1] - + m_elements[2][j] * other.m_elements[i][2]; + element = m_elements[i][0] * other.m_elements[0][j] + + m_elements[i][1] * other.m_elements[1][j] + + m_elements[i][2] * other.m_elements[2][j]; } else if constexpr (N == 2) { - element = m_elements[0][j] * other.m_elements[i][0] - + m_elements[1][j] * other.m_elements[i][1]; + element = m_elements[i][0] * other.m_elements[0][j] + + m_elements[i][1] * other.m_elements[1][j]; } else if constexpr (N == 1) { - element = m_elements[0][j] * other.m_elements[i][0]; + element = m_elements[i][0] * other.m_elements[0][j]; } else { T value {}; for (size_t k = 0; k < N; ++k) - value += m_elements[k][j] * other.m_elements[i][k]; + value += m_elements[i][k] * other.m_elements[k][j]; element = value; } diff --git a/Userland/Libraries/LibGfx/Matrix4x4.h b/Userland/Libraries/LibGfx/Matrix4x4.h index 010cfffbbe..780c411513 100644 --- a/Userland/Libraries/LibGfx/Matrix4x4.h +++ b/Userland/Libraries/LibGfx/Matrix4x4.h @@ -14,7 +14,7 @@ namespace Gfx { template<typename T> -class Matrix4x4 final : public Matrix<4, T> { +class Matrix4x4 final { public: constexpr Matrix4x4() = default; constexpr Matrix4x4(T _11, T _12, T _13, T _14, @@ -38,10 +38,10 @@ public: Matrix4x4 product; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { - product.m_elements[i][j] = m_elements[0][j] * other.m_elements[i][0] - + m_elements[1][j] * other.m_elements[i][1] - + m_elements[2][j] * other.m_elements[i][2] - + m_elements[3][j] * other.m_elements[i][3]; + product.m_elements[i][j] = m_elements[i][0] * other.m_elements[0][j] + + m_elements[i][1] * other.m_elements[1][j] + + m_elements[i][2] * other.m_elements[2][j] + + m_elements[i][3] * other.m_elements[3][j]; } } return product; @@ -50,18 +50,18 @@ public: constexpr Vector4<T> operator*(const Vector4<T>& v) const { return Vector4<T>( - v.x() * m_elements[0][0] + v.y() * m_elements[1][0] + v.z() * m_elements[2][0] + v.w() * m_elements[3][0], - v.x() * m_elements[0][1] + v.y() * m_elements[1][1] + v.z() * m_elements[2][1] + v.w() * m_elements[3][1], - v.x() * m_elements[0][2] + v.y() * m_elements[1][2] + v.z() * m_elements[2][2] + v.w() * m_elements[3][2], - v.x() * m_elements[0][3] + v.y() * m_elements[1][3] + v.z() * m_elements[2][3] + v.w() * m_elements[3][3]); + v.x() * m_elements[0][0] + v.y() * m_elements[0][1] + v.z() * m_elements[0][2] + v.w() * m_elements[0][3], + v.x() * m_elements[1][0] + v.y() * m_elements[1][1] + v.z() * m_elements[1][2] + v.w() * m_elements[1][3], + v.x() * m_elements[2][0] + v.y() * m_elements[2][1] + v.z() * m_elements[2][2] + v.w() * m_elements[2][3], + v.x() * m_elements[3][0] + v.y() * m_elements[3][1] + v.z() * m_elements[3][2] + v.w() * m_elements[3][3]); } constexpr Vector3<T> transform_point(const Vector3<T>& p) const { return Vector3<T>( - p.x() * m_elements[0][0] + p.y() * m_elements[1][0] + p.z() * m_elements[2][0] + m_elements[3][0], - p.x() * m_elements[0][1] + p.y() * m_elements[1][1] + p.z() * m_elements[2][1] + m_elements[3][1], - p.x() * m_elements[0][2] + p.y() * m_elements[1][2] + p.z() * m_elements[2][2] + m_elements[3][2]); + p.x() * m_elements[0][0] + p.y() * m_elements[0][1] + p.z() * m_elements[0][2] + m_elements[0][3], + p.x() * m_elements[1][0] + p.y() * m_elements[1][1] + p.z() * m_elements[1][2] + m_elements[1][3], + p.x() * m_elements[2][0] + p.y() * m_elements[2][1] + p.z() * m_elements[2][2] + m_elements[2][3]); } constexpr static Matrix4x4 identity() @@ -76,10 +76,10 @@ public: constexpr static Matrix4x4 translate(const Vector3<T>& p) { return Matrix4x4( - 1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - p.x(), p.y(), p.z(), 1); + 1, 0, 0, p.x(), + 0, 1, 0, p.y(), + 0, 0, 1, p.z(), + 0, 0, 0, 1); } constexpr static Matrix4x4 scale(const Vector3<T>& s) @@ -107,6 +107,17 @@ public: 0, 0, 0, 1); } + constexpr Matrix4x4 transpose() const + { + Matrix4x4 result; + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + result.m_elements[i][j] = m_elements[j][i]; + } + } + return result; + } + private: T m_elements[4][4]; }; |