improved mat & camera interface

This commit is contained in:
2025-03-15 18:53:18 +03:00
parent 58e2c3b5b7
commit 4200ef63a6
2 changed files with 21 additions and 13 deletions

View File

@@ -34,11 +34,13 @@ namespace omath
class Mat final
{
public:
constexpr Mat()
constexpr Mat() noexcept
{
Clear();
}
constexpr static MatStoreType GetStoreOrdering()
[[nodiscard]]
constexpr static MatStoreType GetStoreOrdering() noexcept
{
return StoreType;
}
@@ -72,6 +74,7 @@ namespace omath
m_data = other.m_data;
}
[[nodiscard]]
constexpr Type& operator[](const size_t row, const size_t col)
{
return At(row, col);
@@ -100,7 +103,8 @@ namespace omath
return {Rows, Columns};
}
[[nodiscard]] constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const
[[nodiscard]]
constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const
{
if (rowIndex >= Rows || columnIndex >= Columns)
throw std::out_of_range("Index out of range");
@@ -177,6 +181,7 @@ namespace omath
return *this = *this * other;
}
[[nodiscard]]
constexpr Mat operator*(const Type& f) const noexcept
{
Mat result(*this);
@@ -192,6 +197,7 @@ namespace omath
return *this;
}
[[nodiscard]]
constexpr Mat operator/(const Type& f) const noexcept
{
Mat result(*this);

View File

@@ -38,7 +38,7 @@ namespace omath::projection
{
}
protected:
virtual void LookAt(const Vector3<float>& target) = 0;
[[nodiscard]] virtual Mat4x4Type CalcViewMatrix() const = 0;
@@ -49,41 +49,44 @@ namespace omath::projection
{
return CalcProjectionMatrix() * CalcViewMatrix();
}
public:
[[nodiscard]] const Mat4x4Type& GetViewProjectionMatrix() const
{
if (!m_viewProjectionMatrix.has_value())
m_viewProjectionMatrix = CalcViewProjectionMatrix();
return m_viewProjectionMatrix.value();
}
void SetFieldOfView(const FieldOfView& fov)
{
m_fieldOfView = fov;
m_viewProjectionMatrix = CalcViewProjectionMatrix();
}
void SetNearPlane(const float near)
{
m_nearPlaneDistance = near;
m_viewProjectionMatrix = CalcViewProjectionMatrix();
}
void SetFarPlane(const float far)
{
m_farPlaneDistance = far;
m_viewProjectionMatrix = CalcViewProjectionMatrix();
}
void SetViewAngles(const ViewAnglesType& viewAngles)
{
m_viewAngles = viewAngles;
m_viewProjectionMatrix = CalcViewProjectionMatrix();
}
void SetOrigin(const Vector3<float>& origin)
{
m_origin = origin;
m_viewProjectionMatrix = CalcViewProjectionMatrix();
}
void SetViewPort(const ViewPort& viewPort)
{
m_viewPort = viewPort;
m_viewProjectionMatrix = CalcViewProjectionMatrix();
}
[[nodiscard]] const FieldOfView& GetFieldOfView() const
@@ -113,10 +116,9 @@ namespace omath::projection
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToScreen(const Vector3<float>& worldPosition) const
{
if (!m_viewProjectionMatrix.has_value())
m_viewProjectionMatrix = CalcViewProjectionMatrix();
const auto& viewProjMatrix = GetViewProjectionMatrix();
auto projected = m_viewProjectionMatrix.value() * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
auto projected = viewProjMatrix * MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
if (projected.At(3, 0) == 0.0f)
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);