mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
added camera
This commit is contained in:
@@ -51,6 +51,8 @@ namespace omath
|
|||||||
|
|
||||||
Matrix operator*(const Matrix &other) const;
|
Matrix operator*(const Matrix &other) const;
|
||||||
|
|
||||||
|
Matrix& operator*=(const Matrix &other);
|
||||||
|
|
||||||
Matrix operator*(float f) const;
|
Matrix operator*(float f) const;
|
||||||
|
|
||||||
Matrix operator*(const Vector3 &vec3) const;
|
Matrix operator*(const Vector3 &vec3) const;
|
||||||
|
|||||||
38
include/omath/projection/Camera.h
Normal file
38
include/omath/projection/Camera.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 27.08.2024.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <expected>
|
||||||
|
#include <omath/Vector3.h>
|
||||||
|
#include <omath/Matrix.h>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::projection
|
||||||
|
{
|
||||||
|
class Camera
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void SetViewAngles(const Vector3& viewAngles);
|
||||||
|
[[nodiscard]] const Vector3& GetViewAngles() const;
|
||||||
|
|
||||||
|
[[nodiscard]] Matrix GetViewMatrix() const;
|
||||||
|
[[nodiscard]] Matrix GetProjectionMatrix() const;
|
||||||
|
[[nodiscard]] Matrix GetTranslationMatrix() const;
|
||||||
|
[[nodiscard]] Matrix GetOrientationMatrix() const;
|
||||||
|
|
||||||
|
[[nodiscard]] std::expected<Vector3, std::string_view> WorldToScreen(const Vector3& worldPosition) const;
|
||||||
|
|
||||||
|
Vector3 m_viewPort;
|
||||||
|
float m_fieldOfView;
|
||||||
|
|
||||||
|
float m_farPlaneDistance;
|
||||||
|
float m_nearPlaneDistance;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector3 m_viewAngles;
|
||||||
|
Vector3 m_origin;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -6,3 +6,4 @@ target_sources(omath PRIVATE
|
|||||||
|
|
||||||
add_subdirectory(prediction)
|
add_subdirectory(prediction)
|
||||||
add_subdirectory(pathfinding)
|
add_subdirectory(pathfinding)
|
||||||
|
add_subdirectory(projection)
|
||||||
@@ -219,7 +219,12 @@ namespace omath
|
|||||||
const auto sinYaw = std::sin(angles::DegreesToRadians(yaw));
|
const auto sinYaw = std::sin(angles::DegreesToRadians(yaw));
|
||||||
|
|
||||||
|
|
||||||
return {cosPitch*cosYaw, cosPitch*sinYaw, sinPitch};
|
return
|
||||||
|
{
|
||||||
|
cosPitch*cosYaw,
|
||||||
|
cosPitch*sinYaw,
|
||||||
|
sinPitch
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::RightVector(const float pitch, const float yaw, const float roll)
|
Vector3 Vector3::RightVector(const float pitch, const float yaw, const float roll)
|
||||||
@@ -230,13 +235,33 @@ namespace omath
|
|||||||
const auto cosYaw = std::cos(angles::DegreesToRadians(yaw));
|
const auto cosYaw = std::cos(angles::DegreesToRadians(yaw));
|
||||||
const auto sinYaw = std::sin(angles::DegreesToRadians(yaw));
|
const auto sinYaw = std::sin(angles::DegreesToRadians(yaw));
|
||||||
|
|
||||||
const auto cosRoll = std::cos(angles::DegreesToRadians(yaw));
|
const auto cosRoll = std::cos(angles::DegreesToRadians(roll));
|
||||||
const auto sinRoll = std::sin(angles::DegreesToRadians(yaw));
|
const auto sinRoll = std::sin(angles::DegreesToRadians(roll));
|
||||||
|
|
||||||
|
|
||||||
return {-sinRoll*sinPitch*cosYaw + -cosRoll*-sinYaw,
|
return {-sinRoll*sinPitch*cosYaw + -cosRoll*-sinYaw,
|
||||||
-sinRoll*sinPitch*sinYaw + -cosRoll*cosYaw,
|
-sinRoll*sinPitch*sinYaw + -cosRoll*cosYaw,
|
||||||
sinRoll*cosPitch};
|
-sinRoll*cosPitch};
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3 Vector3::UpVector(float pitch, float yaw, float roll)
|
||||||
|
{
|
||||||
|
const auto cosPitch = std::cos(angles::DegreesToRadians(pitch));
|
||||||
|
const auto sinPitch = std::sin(angles::DegreesToRadians(pitch));
|
||||||
|
|
||||||
|
const auto cosYaw = std::cos(angles::DegreesToRadians(yaw));
|
||||||
|
const auto sinYaw = std::sin(angles::DegreesToRadians(yaw));
|
||||||
|
|
||||||
|
const auto cosRoll = std::cos(angles::DegreesToRadians(roll));
|
||||||
|
const auto sinRoll = std::sin(angles::DegreesToRadians(roll));
|
||||||
|
|
||||||
|
|
||||||
|
return
|
||||||
|
{
|
||||||
|
cosRoll*sinPitch*cosYaw+-sinRoll*-sinYaw,
|
||||||
|
cosRoll*sinPitch*cosYaw+-sinRoll*-sinYaw,
|
||||||
|
cosRoll*cosPitch,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3 Vector3::Cross(const Vector3 &v) const
|
Vector3 Vector3::Cross(const Vector3 &v) const
|
||||||
|
|||||||
@@ -120,6 +120,12 @@ namespace omath
|
|||||||
return outMat;
|
return outMat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Matrix & Matrix::operator*=(const Matrix &other)
|
||||||
|
{
|
||||||
|
*this = *this * other;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Matrix Matrix::operator*(const float f) const
|
Matrix Matrix::operator*(const float f) const
|
||||||
{
|
{
|
||||||
auto out = *this;
|
auto out = *this;
|
||||||
|
|||||||
1
source/projection/CMakeLists.txt
Normal file
1
source/projection/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
target_sources(omath PRIVATE Camera.cpp)
|
||||||
74
source/projection/Camera.cpp
Normal file
74
source/projection/Camera.cpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
//
|
||||||
|
// Created by Vlad on 27.08.2024.
|
||||||
|
//
|
||||||
|
#include "omath/projection/Camera.h"
|
||||||
|
#include "omath/angles.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace omath::projection
|
||||||
|
{
|
||||||
|
Matrix Camera::GetViewMatrix() const
|
||||||
|
{
|
||||||
|
return GetOrientationMatrix() * GetTranslationMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix Camera::GetProjectionMatrix() const
|
||||||
|
{
|
||||||
|
const auto aspectRatio = m_viewPort.x / m_viewPort.y;
|
||||||
|
const auto verticalFov = std::tan(angles::DegreesToRadians(m_fieldOfView) / 2.f);
|
||||||
|
|
||||||
|
|
||||||
|
return Matrix(
|
||||||
|
{
|
||||||
|
{1.f / (aspectRatio * verticalFov), 0.f, 0.f, 0.f},
|
||||||
|
{0.f, 1.f / verticalFov, 0.f, 0.f},
|
||||||
|
{0.f, 0.f, m_farPlaneDistance / (m_farPlaneDistance-m_nearPlaneDistance), -m_farPlaneDistance*m_nearPlaneDistance / (m_farPlaneDistance-m_nearPlaneDistance)},
|
||||||
|
{0.f, 0.f, 1.f, 0.f}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix Camera::GetTranslationMatrix() const
|
||||||
|
{
|
||||||
|
return Matrix(
|
||||||
|
{
|
||||||
|
{1.f, 0.f, 0.f, m_origin.x},
|
||||||
|
{0.f, 1.f, 0.f, m_origin.y},
|
||||||
|
{0.f, 0.f, 1.f, m_origin.z},
|
||||||
|
{0.f, 0.f, 0.f, 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.y, right.z, 0.f},
|
||||||
|
{up.x, up.y, up.y, 0.f},
|
||||||
|
{forward.x, forward.y, forward.z, 0.f},
|
||||||
|
{0.f, 0.f, 0.f, 1.f},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
return Vector3{projected.At(0, 0), projected.At(0, 1), projected.At(0, 2)};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,5 +13,5 @@ TEST(UnitTestAstar, FindingRightPath)
|
|||||||
mesh.m_verTextMap[{0.f, 1.f, 0.f}] = {{0.f, 2.f, 0.f}};
|
mesh.m_verTextMap[{0.f, 1.f, 0.f}] = {{0.f, 2.f, 0.f}};
|
||||||
mesh.m_verTextMap[{0.f, 2.f, 0.f}] = {{0.f, 3.f, 0.f}};
|
mesh.m_verTextMap[{0.f, 2.f, 0.f}] = {{0.f, 3.f, 0.f}};
|
||||||
mesh.m_verTextMap[{0.f, 3.f, 0.f}] = {};
|
mesh.m_verTextMap[{0.f, 3.f, 0.f}] = {};
|
||||||
omath::pathfinding::Astar::FindPath({}, {0.f, 3.f, 0.f}, mesh);
|
std::ignore = omath::pathfinding::Astar::FindPath({}, {0.f, 3.f, 0.f}, mesh);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user