From abfa3e8123d77349c8cc941eda61203cfffddd6a Mon Sep 17 00:00:00 2001 From: Orange Date: Tue, 3 Sep 2024 21:41:22 +0300 Subject: [PATCH] added error codes --- include/omath/projection/Camera.h | 5 +++-- include/omath/projection/ErrorCodes.h | 16 ++++++++++++++++ source/projection/Camera.cpp | 6 +++--- 3 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 include/omath/projection/ErrorCodes.h diff --git a/include/omath/projection/Camera.h b/include/omath/projection/Camera.h index 799c37d..a7ab73b 100644 --- a/include/omath/projection/Camera.h +++ b/include/omath/projection/Camera.h @@ -8,6 +8,7 @@ #include #include #include +#include "ErrorCodes.h" namespace omath::projection @@ -18,7 +19,7 @@ namespace omath::projection float m_width; float m_height; - [[nodiscard]] float AspectRatio() const {return m_width / m_height;} + [[nodiscard]] constexpr float AspectRatio() const {return m_width / m_height;} }; class Camera @@ -29,7 +30,7 @@ namespace omath::projection [[nodiscard]] Matrix GetViewMatrix() const; - [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const; + [[nodiscard]] std::expected WorldToScreen(const Vector3& worldPosition) const; ViewPort m_viewPort{}; float m_fieldOfView; diff --git a/include/omath/projection/ErrorCodes.h b/include/omath/projection/ErrorCodes.h new file mode 100644 index 0000000..968a15e --- /dev/null +++ b/include/omath/projection/ErrorCodes.h @@ -0,0 +1,16 @@ +// +// Created by Vlad on 03.09.2024. +// + +#pragma once +#include + + +namespace omath::projection +{ + enum class Error : uint16_t + { + WORLD_POSITION_IS_BEHIND_CAMERA = 0, + WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS, + }; +} \ No newline at end of file diff --git a/source/projection/Camera.cpp b/source/projection/Camera.cpp index 99e2c11..f0f7baf 100644 --- a/source/projection/Camera.cpp +++ b/source/projection/Camera.cpp @@ -30,7 +30,7 @@ namespace omath::projection return Matrix::TranslationMatrix(-m_origin) * Matrix::OrientationMatrix(forward, right, up); } - std::expected Camera::WorldToScreen(const Vector3 &worldPosition) const + std::expected Camera::WorldToScreen(const Vector3 &worldPosition) const { const auto posVecAsMatrix = Matrix({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); @@ -41,13 +41,13 @@ namespace omath::projection auto projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); if (projected.At(0, 3) <= 0.f) - return std::unexpected("Projection point is out of camera field of view"); + return std::unexpected(Error::WORLD_POSITION_IS_BEHIND_CAMERA); projected /= projected.At(0, 3); if (projected.At(0, 0) < -1.f || projected.At(0, 0) > 1.f || projected.At(0, 1) < -1.f || projected.At(0, 1) > 1.f) - return std::unexpected("Projection point is out screen bounds"); + return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS); projected *= Matrix::ToScreenMatrix(m_viewPort.m_width, m_viewPort.m_height);