This commit is contained in:
2024-12-01 04:10:25 +03:00
parent 46b4eb9151
commit 339dbade7e
2 changed files with 12 additions and 7 deletions

View File

@@ -29,10 +29,10 @@ namespace omath::source
template<class Type = float>
requires std::is_floating_point_v<Type> || std::is_integral_v<Type>
[[nodiscard]] constexpr Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> ViewMatrixFromVecs(const Vector3& forward, const Vector3& right,
[[nodiscard]] constexpr Mat<4, 4, Type, MatStoreType::ROW_MAJOR> ViewMatrixFromVecs(const Vector3& forward, const Vector3& right,
const Vector3& up, const Vector3& camera_pos)
{
return MatTranslation<float, MatStoreType::COLUMN_MAJOR>(-camera_pos) * Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR>{
return MatTranslation<float, MatStoreType::ROW_MAJOR>(-camera_pos) * Mat<4, 4, Type, MatStoreType::ROW_MAJOR>{
{right.x, up.x, forward.x, 0},
{right.y, up.y, forward.y, 0},
{right.z, up.z, forward.z, 0},
@@ -43,14 +43,14 @@ namespace omath::source
template<class Type = float>
requires std::is_floating_point_v<Type> || std::is_integral_v<Type>
[[nodiscard]] Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR> ViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
[[nodiscard]] Mat<4, 4, Type, MatStoreType::ROW_MAJOR> ViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
{
return ViewMatrixFromVecs(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
}
template<class Type>
requires std::is_floating_point_v<Type> || std::is_integral_v<Type>
[[nodiscard]] Mat<4, 4, Type, MatStoreType::COLUMN_MAJOR>
[[nodiscard]] Mat<4, 4, Type, MatStoreType::ROW_MAJOR>
PerspectiveProjectionMatrix(const Type& fieldOfView, const Type& aspectRatio, const Type& near, const Type& far)
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2);

View File

@@ -52,13 +52,18 @@ namespace omath::projection
return CreateProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, m_farPlaneDistance);
}
[[nodiscard]] auto GetViewProjectionMatrix() const
{
return GetProjectionMatrix() * GetViewMatrix().Transposed();
}
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen([[maybe_unused]] const Vector3& worldPosition) const
{
using mat = std::invoke_result_t<ViewMatFunc, const ViewAnglesType&, const Vector3&>;
const auto vecAsMatrix = MatColumnFromVector<float, mat::GetStoreOrdering()>(worldPosition);
const auto projected = GetViewMatrix().Transposed() * vecAsMatrix;
Mat<4, 1> 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);
return Vector3{projected.At(0,0), projected.At(1,0), projected.At(2,0)};
}