added error codes

This commit is contained in:
2024-09-03 21:41:22 +03:00
parent 2947bbd95a
commit abfa3e8123
3 changed files with 22 additions and 5 deletions

View File

@@ -8,6 +8,7 @@
#include <omath/Vector3.h> #include <omath/Vector3.h>
#include <omath/Matrix.h> #include <omath/Matrix.h>
#include <string_view> #include <string_view>
#include "ErrorCodes.h"
namespace omath::projection namespace omath::projection
@@ -18,7 +19,7 @@ namespace omath::projection
float m_width; float m_width;
float m_height; float m_height;
[[nodiscard]] float AspectRatio() const {return m_width / m_height;} [[nodiscard]] constexpr float AspectRatio() const {return m_width / m_height;}
}; };
class Camera class Camera
@@ -29,7 +30,7 @@ namespace omath::projection
[[nodiscard]] Matrix GetViewMatrix() const; [[nodiscard]] Matrix GetViewMatrix() const;
[[nodiscard]] std::expected<Vector3, std::string_view> WorldToScreen(const Vector3& worldPosition) const; [[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition) const;
ViewPort m_viewPort{}; ViewPort m_viewPort{};
float m_fieldOfView; float m_fieldOfView;

View File

@@ -0,0 +1,16 @@
//
// Created by Vlad on 03.09.2024.
//
#pragma once
#include <cstdint>
namespace omath::projection
{
enum class Error : uint16_t
{
WORLD_POSITION_IS_BEHIND_CAMERA = 0,
WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS,
};
}

View File

@@ -30,7 +30,7 @@ namespace omath::projection
return Matrix::TranslationMatrix(-m_origin) * Matrix::OrientationMatrix(forward, right, up); return Matrix::TranslationMatrix(-m_origin) * Matrix::OrientationMatrix(forward, right, up);
} }
std::expected<Vector3, std::string_view> Camera::WorldToScreen(const Vector3 &worldPosition) const std::expected<Vector3, Error> Camera::WorldToScreen(const Vector3 &worldPosition) const
{ {
const auto posVecAsMatrix = Matrix({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); const auto posVecAsMatrix = Matrix({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}});
@@ -41,13 +41,13 @@ namespace omath::projection
auto projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); auto projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
if (projected.At(0, 3) <= 0.f) 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); projected /= projected.At(0, 3);
if (projected.At(0, 0) < -1.f || projected.At(0, 0) > 1.f || 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) 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); projected *= Matrix::ToScreenMatrix(m_viewPort.m_width, m_viewPort.m_height);