mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 00:57:44 +00:00
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:
parent
0833db0874
commit
c2d84efaae
5 changed files with 103 additions and 117 deletions
|
@ -14,117 +14,78 @@
|
|||
namespace Gfx {
|
||||
|
||||
template<typename T>
|
||||
class Matrix4x4 final {
|
||||
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
|
||||
}
|
||||
{
|
||||
}
|
||||
using Matrix4x4 = Matrix<4, T>;
|
||||
|
||||
constexpr auto elements() const { return m_elements; }
|
||||
constexpr auto elements() { return m_elements; }
|
||||
template<typename T>
|
||||
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
|
||||
{
|
||||
Matrix4x4 product;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
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;
|
||||
}
|
||||
template<typename T>
|
||||
constexpr static Vector3<T> transform_point(const Matrix4x4<T>& m, const Vector3<T>& p)
|
||||
{
|
||||
auto const& elements = m.elements();
|
||||
return Vector3<T>(
|
||||
p.x() * elements[0][0] + p.y() * elements[0][1] + p.z() * elements[0][2] + elements[0][3],
|
||||
p.x() * elements[1][0] + p.y() * elements[1][1] + p.z() * elements[1][2] + elements[1][3],
|
||||
p.x() * elements[2][0] + p.y() * elements[2][1] + p.z() * elements[2][2] + elements[2][3]);
|
||||
}
|
||||
|
||||
constexpr Vector4<T> operator*(const Vector4<T>& v) const
|
||||
{
|
||||
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],
|
||||
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]);
|
||||
}
|
||||
template<typename T>
|
||||
constexpr static Vector3<T> transform_direction(const Matrix4x4<T>& m, const Vector3<T>& d)
|
||||
{
|
||||
auto const& elements = m.elements();
|
||||
return Vector3<T>(
|
||||
d.x() * elements[0][0] + d.y() * elements[0][1] + d.z() * elements[0][2],
|
||||
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
|
||||
{
|
||||
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],
|
||||
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]);
|
||||
}
|
||||
template<typename T>
|
||||
constexpr static Matrix4x4<T> translation_matrix(const Vector3<T>& p)
|
||||
{
|
||||
return Matrix4x4<T>(
|
||||
1, 0, 0, p.x(),
|
||||
0, 1, 0, p.y(),
|
||||
0, 0, 1, p.z(),
|
||||
0, 0, 0, 1);
|
||||
}
|
||||
|
||||
constexpr static Matrix4x4 identity()
|
||||
{
|
||||
return Matrix4x4(
|
||||
1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
0, 0, 0, 1);
|
||||
}
|
||||
template<typename T>
|
||||
constexpr static Matrix4x4<T> scale_matrix(const Vector3<T>& s)
|
||||
{
|
||||
return Matrix4x4<T>(
|
||||
s.x(), 0, 0, 0,
|
||||
0, s.y(), 0, 0,
|
||||
0, 0, s.z(), 0,
|
||||
0, 0, 0, 1);
|
||||
}
|
||||
|
||||
constexpr static Matrix4x4 translate(const Vector3<T>& p)
|
||||
{
|
||||
return Matrix4x4(
|
||||
1, 0, 0, p.x(),
|
||||
0, 1, 0, p.y(),
|
||||
0, 0, 1, p.z(),
|
||||
0, 0, 0, 1);
|
||||
}
|
||||
template<typename T>
|
||||
constexpr static Matrix4x4<T> rotation_matrix(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();
|
||||
|
||||
constexpr static Matrix4x4 scale(const Vector3<T>& s)
|
||||
{
|
||||
return Matrix4x4(
|
||||
s.x(), 0, 0, 0,
|
||||
0, s.y(), 0, 0,
|
||||
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];
|
||||
};
|
||||
return Matrix4x4<T>(
|
||||
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);
|
||||
}
|
||||
|
||||
typedef Matrix4x4<float> FloatMatrix4x4;
|
||||
typedef Matrix4x4<double> DoubleMatrix4x4;
|
||||
|
||||
}
|
||||
|
||||
using Gfx::DoubleMatrix4x4;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue