improved projection class

This commit is contained in:
2024-09-30 11:54:47 -07:00
parent 4fb06d70fc
commit ac79326da8
3 changed files with 14 additions and 8 deletions

View File

@@ -122,6 +122,12 @@ namespace omath
return *this; return *this;
} }
template <size_t OtherColumns>
constexpr Mat<Rows, OtherColumns> operator*=(const Mat<Columns, OtherColumns>& other)
{
return *this = *this * other;
}
constexpr Mat operator*(float f) const constexpr Mat operator*(float f) const
{ {
Mat result(*this); Mat result(*this);

View File

@@ -6,7 +6,7 @@
#include <expected> #include <expected>
#include <omath/Vector3.h> #include <omath/Vector3.h>
#include <omath/Matrix.h> #include <omath/Mat.h>
#include <string_view> #include <string_view>
#include "ErrorCodes.h" #include "ErrorCodes.h"
@@ -28,7 +28,7 @@ namespace omath::projection
Camera(const Vector3& position, const Vector3& viewAngles, const ViewPort& viewPort, float fov, float near, float far); Camera(const Vector3& position, const Vector3& viewAngles, const ViewPort& viewPort, float fov, float near, float far);
void SetViewAngles(const Vector3& viewAngles); void SetViewAngles(const Vector3& viewAngles);
[[nodiscard]] Matrix GetViewMatrix() const; [[nodiscard]] Mat<4, 4> GetViewMatrix() const;
[[nodiscard]] std::expected<Vector2, Error> WorldToScreen(const Vector3& worldPosition) const; [[nodiscard]] std::expected<Vector2, Error> WorldToScreen(const Vector3& worldPosition) const;

View File

@@ -21,24 +21,24 @@ namespace omath::projection
m_farPlaneDistance = far; m_farPlaneDistance = far;
} }
Matrix Camera::GetViewMatrix() const Mat<4, 4> Camera::GetViewMatrix() const
{ {
const auto forward = Vector3::ForwardVector(m_viewAngles.x, m_viewAngles.y); const auto forward = Vector3::ForwardVector(m_viewAngles.x, m_viewAngles.y);
const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z); const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
return Matrix::TranslationMatrix(-m_origin) * Matrix::OrientationMatrix(forward, right, up); return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up);
} }
std::expected<Vector2, Error> Camera::WorldToScreen(const Vector3 &worldPosition) const std::expected<Vector2, Error> Camera::WorldToScreen(const Vector3 &worldPosition) const
{ {
const auto posVecAsMatrix = Matrix({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}}); const auto posVecAsMatrix = Mat<1, 4>({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}});
const auto projectionMatrix = Matrix::ProjectionMatrix(m_fieldOfView, m_viewPort.AspectRatio(), const auto projectionMatrix = Mat<4, 4>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(),
m_nearPlaneDistance, m_farPlaneDistance); m_nearPlaneDistance, m_farPlaneDistance);
auto projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix); Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
if (projected.At(0, 3) <= 0.f) if (projected.At(0, 3) <= 0.f)
return std::unexpected(Error::WORLD_POSITION_IS_BEHIND_CAMERA); return std::unexpected(Error::WORLD_POSITION_IS_BEHIND_CAMERA);
@@ -49,7 +49,7 @@ namespace omath::projection
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(Error::WORLD_POSITION_IS_OUT_OF_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 *= Mat<4, 4>::ToScreenMat(m_viewPort.m_width, m_viewPort.m_height);
return Vector2{projected.At(0, 0), projected.At(0, 1)}; return Vector2{projected.At(0, 0), projected.At(0, 1)};
} }