diff --git a/include/omath/engines/unity_engine/camera.hpp b/include/omath/engines/unity_engine/camera.hpp new file mode 100644 index 0000000..e3a7f4e --- /dev/null +++ b/include/omath/engines/unity_engine/camera.hpp @@ -0,0 +1,21 @@ +// +// Created by Vlad on 3/22/2025. +// + +#pragma once +#include "omath/engines/unity_engine/constants.hpp" +#include "omath/projection/camera.hpp" + +namespace omath::unity_engine +{ + class Camera final : public projection::Camera + { + public: + Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, + const Angle& fov, float near, float far); + void LookAt(const Vector3& target) override; + protected: + [[nodiscard]] Mat4x4 CalcViewMatrix() const override; + [[nodiscard]] Mat4x4 CalcProjectionMatrix() const override; + }; +} \ No newline at end of file diff --git a/include/omath/engines/unity_engine/constants.hpp b/include/omath/engines/unity_engine/constants.hpp new file mode 100644 index 0000000..dae1775 --- /dev/null +++ b/include/omath/engines/unity_engine/constants.hpp @@ -0,0 +1,26 @@ +// +// Created by Vlad on 3/22/2025. +// + +#pragma once + +#include +#include +#include +#include + +namespace omath::unity_engine +{ + constexpr Vector3 kAbsUp = {0, 1, 0}; + constexpr Vector3 kAbsRight = {1, 0, 0}; + constexpr Vector3 kAbsForward = {0, 0, 1}; + + using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>; + using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>; + using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>; + using PitchAngle = Angle; + using YawAngle = Angle; + using RollAngle = Angle; + + using ViewAngles = omath::ViewAngles; +} // namespace omath::source diff --git a/include/omath/engines/unity_engine/formulas.hpp b/include/omath/engines/unity_engine/formulas.hpp new file mode 100644 index 0000000..6352cd1 --- /dev/null +++ b/include/omath/engines/unity_engine/formulas.hpp @@ -0,0 +1,24 @@ +// +// Created by Vlad on 3/22/2025. +// + +#pragma once +#include "omath/engines/unity_engine/constants.hpp" + +namespace omath::unity_engine +{ + [[nodiscard]] + Vector3 ForwardVector(const ViewAngles& angles); + + [[nodiscard]] + Vector3 RightVector(const ViewAngles& angles); + + [[nodiscard]] + Vector3 UpVector(const ViewAngles& angles); + + [[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin); + + + [[nodiscard]] + Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far); +} // namespace omath::source diff --git a/source/engines/CMakeLists.txt b/source/engines/CMakeLists.txt index eb223b0..aea648a 100644 --- a/source/engines/CMakeLists.txt +++ b/source/engines/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(source_engine) add_subdirectory(opengl_engine) add_subdirectory(iw_engine) +add_subdirectory(unity_engine) \ No newline at end of file diff --git a/source/engines/unity_engine/CMakeLists.txt b/source/engines/unity_engine/CMakeLists.txt new file mode 100644 index 0000000..5da7def --- /dev/null +++ b/source/engines/unity_engine/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(omath PRIVATE formulas.cpp camera.cpp) \ No newline at end of file diff --git a/source/engines/unity_engine/camera.cpp b/source/engines/unity_engine/camera.cpp new file mode 100644 index 0000000..e1b256c --- /dev/null +++ b/source/engines/unity_engine/camera.cpp @@ -0,0 +1,27 @@ +// +// Created by Vlad on 3/22/2025. +// +#include + + +namespace omath::unity_engine +{ + Camera::Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort, + const projection::FieldOfView& fov, const float near, const float far) : + projection::Camera(position, viewAngles, viewPort, fov, near, far) + { + } + void Camera::LookAt([[maybe_unused]] const Vector3& target) + { + throw std::runtime_error("Not implemented"); + } + Mat4x4 Camera::CalcViewMatrix() const + { + return unity_engine::CalcViewMatrix(m_viewAngles, m_origin); + } + Mat4x4 Camera::CalcProjectionMatrix() const + { + return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance, + m_farPlaneDistance); + } +} // namespace omath::unity_engine diff --git a/source/engines/unity_engine/formulas.cpp b/source/engines/unity_engine/formulas.cpp new file mode 100644 index 0000000..7a273cf --- /dev/null +++ b/source/engines/unity_engine/formulas.cpp @@ -0,0 +1,45 @@ +// +// Created by Vlad on 3/22/2025. +// +#include "omath/engines/unity_engine/formulas.hpp" + + +namespace omath::unity_engine +{ + Vector3 unity_engine::ForwardVector(const ViewAngles& angles) + { + const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward); + + return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; + } + Vector3 RightVector(const ViewAngles& angles) + { + const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight); + + return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; + } + Vector3 UpVector(const ViewAngles& angles) + { + const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp); + + return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)}; + } + Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin) + { + return MatCameraView(ForwardVector(angles), RightVector(angles), + UpVector(angles), cam_origin); + } + Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, + const float far) + { + const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f); + + return { + {1.f / (aspectRatio * fovHalfTan), 0, 0, 0}, + {0, 1.f / (fovHalfTan), 0, 0}, + {0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)}, + {0, 0, 1, 0}, + + }; + } +} // namespace omath::unity_engine