mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:57:44 +00:00
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].
This commit is contained in:
parent
a5194274af
commit
0833db0874
4 changed files with 49 additions and 31 deletions
|
@ -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];
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue