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

@@ -21,24 +21,24 @@ namespace omath::projection
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 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);
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
{
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);
auto projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
if (projected.At(0, 3) <= 0.f)
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)
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)};
}