1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:37:34 +00:00

LibGfx+LibGL: Do not crash if matrix inverse does not exist

Allow `Matrix::inverse()` to return an error and deal with those in
LibGL. Also use this opportunity to more efficiently calculate the
transpose of the model view matrix for the normal transformation.
This commit is contained in:
Jelle Raaijmakers 2022-01-11 00:50:17 +01:00 committed by Linus Groh
parent 03c1f1780d
commit a4a666152b
2 changed files with 16 additions and 13 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Error.h>
#include <AK/Types.h>
#include <initializer_list>
@ -85,9 +86,8 @@ public:
{
Matrix division;
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < N; ++j) {
for (size_t j = 0; j < N; ++j)
division.m_elements[i][j] = m_elements[i][j] / divisor;
}
}
return division;
}
@ -159,10 +159,11 @@ public:
return result;
}
[[nodiscard]] constexpr Matrix inverse() const
[[nodiscard]] constexpr ErrorOr<Matrix> inverse() const
{
auto det = determinant();
VERIFY(det != 0);
if (det == 0)
return Error::from_string_literal("inverse of matrix does not exist"sv);
return adjugate() / det;
}
@ -170,9 +171,8 @@ public:
{
Matrix result;
for (size_t i = 0; i < N; ++i) {
for (size_t j = 0; j < N; ++j) {
for (size_t j = 0; j < N; ++j)
result.m_elements[i][j] = m_elements[j][i];
}
}
return result;
}