LibGfx+Demos: Make Matrix4x4 a true alias for Matrix<4,T>

Matrix4x4 was defined as a derived class of Matrix<N,T> before.
Furthermore, some code was duplicated and it was overall just messy.
This commit turns Matrix4x4 into a simple alias for Matrix<4,T>.
This commit is contained in:
Stephan Unverwerth 2021-05-13 21:33:17 +02:00 committed by Andreas Kling
parent 0833db0874
commit c2d84efaae
5 changed files with 103 additions and 117 deletions

View file

@ -128,13 +128,13 @@ void Cube::timer_event(Core::TimerEvent&)
static float angle = 0; static float angle = 0;
angle += 0.02f; angle += 0.02f;
auto matrix = FloatMatrix4x4::translate(FloatVector3(0, 0, 1.5f)) auto matrix = Gfx::translation_matrix(FloatVector3(0, 0, 1.5f))
* FloatMatrix4x4::rotate(FloatVector3(1, 0, 0), angle * 1.17356641f) * Gfx::rotation_matrix(FloatVector3(1, 0, 0), angle * 1.17356641f)
* FloatMatrix4x4::rotate(FloatVector3(0, 1, 0), angle * 0.90533273f) * Gfx::rotation_matrix(FloatVector3(0, 1, 0), angle * 0.90533273f)
* FloatMatrix4x4::rotate(FloatVector3(0, 0, 1), angle); * Gfx::rotation_matrix(FloatVector3(0, 0, 1), angle);
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
transformed_vertices[i] = matrix.transform_point(vertices[i]); transformed_vertices[i] = transform_point(matrix, vertices[i]);
} }
GUI::Painter painter(*m_bitmap); GUI::Painter painter(*m_bitmap);

View file

@ -82,10 +82,10 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
glCallList(m_init_list); glCallList(m_init_list);
angle -= 0.01f; angle -= 0.01f;
auto matrix = FloatMatrix4x4::translate(FloatVector3(0, 0, -8.5)) auto matrix = Gfx::translation_matrix(FloatVector3(0, 0, -8.5))
* FloatMatrix4x4::rotate(FloatVector3(1, 0, 0), angle) * Gfx::rotation_matrix(FloatVector3(1, 0, 0), angle)
* FloatMatrix4x4::rotate(FloatVector3(0, 1, 0), 0.0f) * Gfx::rotation_matrix(FloatVector3(0, 1, 0), 0.0f)
* FloatMatrix4x4::rotate(FloatVector3(0, 0, 1), angle); * Gfx::rotation_matrix(FloatVector3(0, 0, 1), angle);
// We need to transpose here because OpenGL expects matrices in column major order // We need to transpose here because OpenGL expects matrices in column major order
// but our matrix class stores elements in row major order. // but our matrix class stores elements in row major order.

View file

@ -576,7 +576,7 @@ void SoftwareGLContext::gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdoub
FloatVector3 axis = { (float)x, (float)y, (float)z }; FloatVector3 axis = { (float)x, (float)y, (float)z };
axis.normalize(); axis.normalize();
auto rotation_mat = FloatMatrix4x4::rotate(axis, angle); auto rotation_mat = Gfx::rotation_matrix(axis, static_cast<float>(angle));
if (m_current_matrix_mode == GL_MODELVIEW) if (m_current_matrix_mode == GL_MODELVIEW)
m_model_view_matrix = m_model_view_matrix * rotation_mat; m_model_view_matrix = m_model_view_matrix * rotation_mat;
@ -596,9 +596,9 @@ void SoftwareGLContext::gl_scale(GLdouble x, GLdouble y, GLdouble z)
} }
if (m_current_matrix_mode == GL_MODELVIEW) { if (m_current_matrix_mode == GL_MODELVIEW) {
m_model_view_matrix = m_model_view_matrix * FloatMatrix4x4::scale({ static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) }); m_model_view_matrix = m_model_view_matrix * Gfx::scale_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
} else if (m_current_matrix_mode == GL_PROJECTION) { } else if (m_current_matrix_mode == GL_PROJECTION) {
m_projection_matrix = m_projection_matrix * FloatMatrix4x4::scale({ static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) }); m_projection_matrix = m_projection_matrix * Gfx::scale_matrix(FloatVector3 { static_cast<float>(x), static_cast<float>(y), static_cast<float>(z) });
} }
m_error = GL_NO_ERROR; m_error = GL_NO_ERROR;
@ -614,9 +614,9 @@ void SoftwareGLContext::gl_translate(GLdouble x, GLdouble y, GLdouble z)
} }
if (m_current_matrix_mode == GL_MODELVIEW) { if (m_current_matrix_mode == GL_MODELVIEW) {
m_model_view_matrix = m_model_view_matrix * FloatMatrix4x4::translate({ (float)x, (float)y, (float)z }); m_model_view_matrix = m_model_view_matrix * Gfx::translation_matrix(FloatVector3 { (float)x, (float)y, (float)z });
} else if (m_current_matrix_mode == GL_PROJECTION) { } else if (m_current_matrix_mode == GL_PROJECTION) {
m_projection_matrix = m_projection_matrix * FloatMatrix4x4::translate({ (float)x, (float)y, (float)z }); m_projection_matrix = m_projection_matrix * Gfx::translation_matrix(FloatVector3 { (float)x, (float)y, (float)z });
} }
m_error = GL_NO_ERROR; m_error = GL_NO_ERROR;

View file

@ -44,8 +44,8 @@ public:
constexpr Matrix operator*(const Matrix& other) const constexpr Matrix operator*(const Matrix& other) const
{ {
Matrix product; Matrix product;
for (int i = 0; i < N; ++i) { for (size_t i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) { for (size_t j = 0; j < N; ++j) {
auto& element = product.m_elements[i][j]; auto& element = product.m_elements[i][j];
if constexpr (N == 4) { if constexpr (N == 4) {
@ -75,6 +75,31 @@ public:
return product; return product;
} }
constexpr static Matrix identity()
{
Matrix result;
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < N; ++j) {
if (i == j)
result.m_elements[i][j] = 1;
else
result.m_elements[i][j] = 0;
}
}
return result;
}
constexpr Matrix transpose() const
{
Matrix result;
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < N; ++j) {
result.m_elements[i][j] = m_elements[j][i];
}
}
return result;
}
private: private:
T m_elements[N][N]; T m_elements[N][N];
}; };

View file

@ -14,117 +14,78 @@
namespace Gfx { namespace Gfx {
template<typename T> template<typename T>
class Matrix4x4 final { using Matrix4x4 = Matrix<4, T>;
public:
constexpr Matrix4x4() = default;
constexpr Matrix4x4(T _11, T _12, T _13, T _14,
T _21, T _22, T _23, T _24,
T _31, T _32, T _33, T _34,
T _41, T _42, T _43, T _44)
: m_elements {
_11, _12, _13, _14,
_21, _22, _23, _24,
_31, _32, _33, _34,
_41, _42, _43, _44
}
{
}
constexpr auto elements() const { return m_elements; } template<typename T>
constexpr auto elements() { return m_elements; } constexpr static Vector4<T> operator*(const Matrix4x4<T>& m, const Vector4<T>& v)
{
auto const& elements = m.elements();
return Vector4<T>(
v.x() * elements[0][0] + v.y() * elements[0][1] + v.z() * elements[0][2] + v.w() * elements[0][3],
v.x() * elements[1][0] + v.y() * elements[1][1] + v.z() * elements[1][2] + v.w() * elements[1][3],
v.x() * elements[2][0] + v.y() * elements[2][1] + v.z() * elements[2][2] + v.w() * elements[2][3],
v.x() * elements[3][0] + v.y() * elements[3][1] + v.z() * elements[3][2] + v.w() * elements[3][3]);
}
constexpr Matrix4x4 operator*(const Matrix4x4& other) const template<typename T>
{ constexpr static Vector3<T> transform_point(const Matrix4x4<T>& m, const Vector3<T>& p)
Matrix4x4 product; {
for (int i = 0; i < 4; ++i) { auto const& elements = m.elements();
for (int j = 0; j < 4; ++j) { return Vector3<T>(
product.m_elements[i][j] = m_elements[i][0] * other.m_elements[0][j] p.x() * elements[0][0] + p.y() * elements[0][1] + p.z() * elements[0][2] + elements[0][3],
+ m_elements[i][1] * other.m_elements[1][j] p.x() * elements[1][0] + p.y() * elements[1][1] + p.z() * elements[1][2] + elements[1][3],
+ m_elements[i][2] * other.m_elements[2][j] p.x() * elements[2][0] + p.y() * elements[2][1] + p.z() * elements[2][2] + elements[2][3]);
+ m_elements[i][3] * other.m_elements[3][j]; }
}
}
return product;
}
constexpr Vector4<T> operator*(const Vector4<T>& v) const template<typename T>
{ constexpr static Vector3<T> transform_direction(const Matrix4x4<T>& m, const Vector3<T>& d)
return Vector4<T>( {
v.x() * m_elements[0][0] + v.y() * m_elements[0][1] + v.z() * m_elements[0][2] + v.w() * m_elements[0][3], auto const& elements = m.elements();
v.x() * m_elements[1][0] + v.y() * m_elements[1][1] + v.z() * m_elements[1][2] + v.w() * m_elements[1][3], return Vector3<T>(
v.x() * m_elements[2][0] + v.y() * m_elements[2][1] + v.z() * m_elements[2][2] + v.w() * m_elements[2][3], d.x() * elements[0][0] + d.y() * elements[0][1] + d.z() * elements[0][2],
v.x() * m_elements[3][0] + v.y() * m_elements[3][1] + v.z() * m_elements[3][2] + v.w() * m_elements[3][3]); d.x() * elements[1][0] + d.y() * elements[1][1] + d.z() * elements[1][2],
} d.x() * elements[2][0] + d.y() * elements[2][1] + d.z() * elements[2][2]);
}
constexpr Vector3<T> transform_point(const Vector3<T>& p) const template<typename T>
{ constexpr static Matrix4x4<T> translation_matrix(const Vector3<T>& p)
return Vector3<T>( {
p.x() * m_elements[0][0] + p.y() * m_elements[0][1] + p.z() * m_elements[0][2] + m_elements[0][3], return Matrix4x4<T>(
p.x() * m_elements[1][0] + p.y() * m_elements[1][1] + p.z() * m_elements[1][2] + m_elements[1][3], 1, 0, 0, p.x(),
p.x() * m_elements[2][0] + p.y() * m_elements[2][1] + p.z() * m_elements[2][2] + m_elements[2][3]); 0, 1, 0, p.y(),
} 0, 0, 1, p.z(),
0, 0, 0, 1);
}
constexpr static Matrix4x4 identity() template<typename T>
{ constexpr static Matrix4x4<T> scale_matrix(const Vector3<T>& s)
return Matrix4x4( {
1, 0, 0, 0, return Matrix4x4<T>(
0, 1, 0, 0, s.x(), 0, 0, 0,
0, 0, 1, 0, 0, s.y(), 0, 0,
0, 0, 0, 1); 0, 0, s.z(), 0,
} 0, 0, 0, 1);
}
constexpr static Matrix4x4 translate(const Vector3<T>& p) template<typename T>
{ constexpr static Matrix4x4<T> rotation_matrix(const Vector3<T>& axis, T angle)
return Matrix4x4( {
1, 0, 0, p.x(), T c = cos(angle);
0, 1, 0, p.y(), T s = sin(angle);
0, 0, 1, p.z(), T t = 1 - c;
0, 0, 0, 1); T x = axis.x();
} T y = axis.y();
T z = axis.z();
constexpr static Matrix4x4 scale(const Vector3<T>& s) return Matrix4x4<T>(
{ t * x * x + c, t * x * y - z * s, t * x * z + y * s, 0,
return Matrix4x4( t * x * y + z * s, t * y * y + c, t * y * z - x * s, 0,
s.x(), 0, 0, 0, t * x * z - y * s, t * y * z + x * s, t * z * z + c, 0,
0, s.y(), 0, 0, 0, 0, 0, 1);
0, 0, s.z(), 0, }
0, 0, 0, 1);
}
constexpr static Matrix4x4 rotate(const Vector3<T>& axis, T angle)
{
T c = cos(angle);
T s = sin(angle);
T t = 1 - c;
T x = axis.x();
T y = axis.y();
T z = axis.z();
return Matrix4x4(
t * x * x + c, t * x * y - z * s, t * x * z + y * s, 0,
t * x * y + z * s, t * y * y + c, t * y * z - x * s, 0,
t * x * z - y * s, t * y * z + x * s, t * z * z + c, 0,
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];
};
typedef Matrix4x4<float> FloatMatrix4x4; typedef Matrix4x4<float> FloatMatrix4x4;
typedef Matrix4x4<double> DoubleMatrix4x4; typedef Matrix4x4<double> DoubleMatrix4x4;
} }
using Gfx::DoubleMatrix4x4; using Gfx::DoubleMatrix4x4;