added check, removed deprecated code

This commit is contained in:
2024-12-02 10:57:47 +03:00
parent 04a5535ade
commit 9c0285e353
4 changed files with 27 additions and 49 deletions

View File

@@ -7,7 +7,6 @@
#include <sstream>
#include <stdexcept>
#include <utility>
#include "Angles.hpp"
#include "Vector3.hpp"
@@ -317,42 +316,6 @@ namespace omath
};
}
[[nodiscard]]
constexpr static Mat<4, 4> TranslationMat(const Vector3& diff) noexcept
{
return {
{1, 0, 0, diff.x},
{0, 1, 0, diff.y},
{0, 0, 1, diff.z},
{0, 0, 0, 1},
};
}
[[nodiscard]]
constexpr static Mat<4, 4> OrientationMat(const Vector3& forward, const Vector3& right,
const Vector3& up) noexcept
{
return {
{right.x, up.x, forward.x, 0},
{right.y, up.y, forward.y, 0},
{right.z, up.z, forward.z, 0},
{0, 0, 0, 1},
};
}
[[nodiscard]]
constexpr static Mat<4, 4> ProjectionMat(const Type& fieldOfView, const Type& aspectRatio, const Type& near,
const Type& far, const Type& lensZoom) noexcept
{
const Type& fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2);
const Type& frustumHeight = far - near;
return {{-1 / (aspectRatio * fovHalfTan) * lensZoom, 0, 0, 0},
{0, -1 / fovHalfTan * lensZoom, 0, 0},
{0, 0, -far / frustumHeight, -1},
{0, 0, near * far / frustumHeight, 0}};
}
private:
std::array<Type, Rows * Columns> m_data;
};
@@ -368,19 +331,19 @@ namespace omath
[[nodiscard]]
constexpr static Mat<4, 1, T, St> MatColumnFromVector(const Vector3& vector) noexcept
{
return {
{vector.x}, {vector.y}, {vector.z}, {1}};
return {{vector.x}, {vector.y}, {vector.z}, {1}};
}
template<class T = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
constexpr Mat<4, 4, T, St> MatTranslation(const Vector3& diff) noexcept
{
return Mat<4, 4, T, St>{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{diff.x, diff.y, diff.z, 1},
}.Transposed();
return
{
{1, 0, 0, diff.x},
{0, 1, 0, diff.y},
{0, 0, 1, diff.z},
{0, 0, 0, 1},
};
}
} // namespace omath

View File

@@ -4,7 +4,7 @@
#pragma once
#include "omath/Vector3.hpp"
#include "omath/Mat.hpp"
#include "omath/Angle.hpp"
namespace omath::opengl
{

View File

@@ -55,7 +55,7 @@ namespace omath::source
{
constexpr auto kMultiplyFactor = Type(0.75);
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2) * kMultiplyFactor;
const Type fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2) * kMultiplyFactor;
return {
{-static_cast<Type>(1) / (aspectRatio * fovHalfTan), 0, 0, 0},

View File

@@ -31,6 +31,7 @@ namespace omath::projection
std::invoke_result_t<ProjectionFunc, const float&, const float&, const float&, const float&>>
class Camera
{
public:
Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far,
@@ -56,15 +57,20 @@ namespace omath::projection
{
return GetProjectionMatrix() * GetViewMatrix();
}
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen([[maybe_unused]] const Vector3& worldPosition) const
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition) const
{
using mat = std::invoke_result_t<ViewMatFunc, const ViewAnglesType&, const Vector3&>;
Mat<4, 1> projected = GetViewProjectionMatrix() * MatColumnFromVector<float, mat::GetStoreOrdering()>(worldPosition);
auto projected = GetViewProjectionMatrix() * MatColumnFromVector<float, mat::GetStoreOrdering()>(worldPosition);
if (projected.At(3, 0) == 0.0f)
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
projected /= projected.At(3, 0);
if (IsNdcOutOfBounds(projected))
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
return Vector3{++projected.At(0,0) / 2 * m_viewPort.m_width , ++projected.At(1,0) / 2 * m_viewPort.m_height, projected.At(2,0)};
}
@@ -75,10 +81,19 @@ namespace omath::projection
float m_nearPlaneDistance;
private:
ViewAnglesType m_viewAngles;
Vector3 m_origin;
std::function<ViewMatFunc> CreateViewMatrix;
std::function<ProjectionFunc> CreateProjectionMatrix;
template<class Type>
[[nodiscard]]
constexpr static bool IsNdcOutOfBounds(const Type& ndc)
{
return std::ranges::any_of( ndc.RawArray(), [](const auto& val) { return val < -1 || val > 1; });
}
};
} // namespace omath::projection