mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
extracted some methods
This commit is contained in:
@@ -10,8 +10,8 @@
|
||||
|
||||
namespace omath::projection
|
||||
{
|
||||
Camera::Camera(const Vector3 &position, const Vector3 &viewAngles, const Vector3 &viewPort, const float fov, const float near,
|
||||
const float far)
|
||||
Camera::Camera(const Vector3 &position, const Vector3 &viewAngles, const ViewPort &viewPort,
|
||||
const float fov, const float near, const float far)
|
||||
{
|
||||
m_origin = position;
|
||||
m_viewAngles = viewAngles;
|
||||
@@ -21,79 +21,41 @@ namespace omath::projection
|
||||
m_farPlaneDistance = far;
|
||||
}
|
||||
|
||||
float & Camera::GetFloat1() {
|
||||
static float m_float1 = 1.52550f;
|
||||
float& Camera::GetFloat1() {
|
||||
static float m_float1 = 0.36689f;
|
||||
return m_float1;
|
||||
}
|
||||
|
||||
float & Camera::GetFloat2() {
|
||||
float& Camera::GetFloat2() {
|
||||
static float m_float2 = 1.14500f;
|
||||
return m_float2;
|
||||
}
|
||||
|
||||
Matrix Camera::GetViewMatrix() const
|
||||
{
|
||||
return GetTranslationMatrix() * GetOrientationMatrix();
|
||||
}
|
||||
|
||||
Matrix Camera::GetProjectionMatrix() const
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(m_fieldOfView) / 2.f);
|
||||
const auto aspectRatio = m_viewPort.x / m_viewPort.y;
|
||||
|
||||
const auto far = m_farPlaneDistance;
|
||||
const auto near = m_nearPlaneDistance;
|
||||
|
||||
return Matrix(
|
||||
{
|
||||
{1.f / (aspectRatio*fovHalfTan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
|
||||
{0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)},
|
||||
{0.f, 0.f, -1.f, 0.f},
|
||||
});
|
||||
}
|
||||
|
||||
Matrix Camera::GetTranslationMatrix() const
|
||||
{
|
||||
return Matrix(
|
||||
{
|
||||
{1.f, 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f, 0.f, 0.f},
|
||||
{0.f, 0.f, 1.f, 0.f},
|
||||
{-m_origin.x, -m_origin.y, -m_origin.z, 1.f},
|
||||
});
|
||||
}
|
||||
|
||||
Matrix Camera::GetOrientationMatrix() 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(
|
||||
{
|
||||
{right.x, up.x, forward.x, 0.f},
|
||||
{right.y, up.y, forward.y, 0.f},
|
||||
{right.z, up.z, forward.z, 0.f},
|
||||
{0.f, 0.f, 0.f, 1.f},
|
||||
});
|
||||
return Matrix::TranslationMatrix(-m_origin) * Matrix::OrientationMatrix(forward, right, up);
|
||||
}
|
||||
|
||||
std::expected<Vector3, std::string_view> Camera::WorldToScreen(const Vector3 &worldPosition) const
|
||||
{
|
||||
const auto posVecAsMatrix = Matrix({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}});
|
||||
|
||||
const auto viewProjectionMatrix = GetViewMatrix() * GetProjectionMatrix();
|
||||
|
||||
auto projected = posVecAsMatrix * viewProjectionMatrix;
|
||||
const auto projectionMatrix = Matrix::ProjectionMatrix(m_fieldOfView, m_viewPort.AspectRatio(),
|
||||
m_nearPlaneDistance, m_farPlaneDistance);
|
||||
|
||||
auto projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
|
||||
|
||||
if (projected.At(0, 3) <= 0.f)
|
||||
return std::unexpected("Projection point is out of camera field of view");
|
||||
|
||||
projected /= projected.At(0, 3);
|
||||
|
||||
projected *= Matrix::ToScreenMatrix(m_viewPort.x, m_viewPort.y);
|
||||
projected *= Matrix::ToScreenMatrix(m_viewPort.m_width, m_viewPort.m_height);
|
||||
|
||||
return Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user