Compare commits

..

7 Commits

Author SHA1 Message Date
27b24b5fe7 added gource script 2026-04-20 01:36:08 +03:00
4186ae8d76 added more tests 2026-04-20 01:17:11 +03:00
8e6e3211c2 Merge pull request #183 from orange-cpp/featue/aabb-improvement
Featue/aabb improvement
2026-04-19 23:49:25 +03:00
1c0619ff7b added new methods 2026-04-19 23:20:29 +03:00
dfd18e96fb added aabb improvemnt 2026-04-19 23:07:58 +03:00
20930c629a added method to get camera matrix 2026-04-18 15:40:38 +03:00
0845a2e863 clarified interfaces 2026-04-18 12:54:37 +03:00
9 changed files with 1155 additions and 69 deletions

View File

@@ -318,22 +318,22 @@ int main()
glfwPollEvents();
omath::Vector3<float> move_dir;
if (glfwGetKey(window, GLFW_KEY_W))
move_dir += camera.get_forward();
move_dir += camera.get_abs_forward();
if (glfwGetKey(window, GLFW_KEY_A))
move_dir -= camera.get_right();
move_dir -= camera.get_abs_right();
if (glfwGetKey(window, GLFW_KEY_S))
move_dir -= camera.get_forward();
move_dir -= camera.get_abs_forward();
if (glfwGetKey(window, GLFW_KEY_D))
move_dir += camera.get_right();
move_dir += camera.get_abs_right();
if (glfwGetKey(window, GLFW_KEY_SPACE))
move_dir += camera.get_up();
move_dir += camera.get_abs_up();
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL))
move_dir -= camera.get_up();
move_dir -= camera.get_abs_up();
auto delta = glfwGetTime() - old_mouse_time;

View File

@@ -7,6 +7,8 @@
namespace omath::primitives
{
enum class UpAxis { X, Y, Z };
template<class Type>
struct Aabb final
{
@@ -24,5 +26,42 @@ namespace omath::primitives
{
return (max - min) / static_cast<Type>(2);
}
template<UpAxis Up = UpAxis::Y>
[[nodiscard]]
constexpr Vector3<Type> top() const noexcept
{
const auto aabb_center = center();
if constexpr (Up == UpAxis::Z)
return {aabb_center.x, aabb_center.y, max.z};
else if constexpr (Up == UpAxis::X)
return {max.x, aabb_center.y, aabb_center.z};
else if constexpr (Up == UpAxis::Y)
return {aabb_center.x, max.y, aabb_center.z};
else
std::unreachable();
}
template<UpAxis Up = UpAxis::Y>
[[nodiscard]]
constexpr Vector3<Type> bottom() const noexcept
{
const auto aabb_center = center();
if constexpr (Up == UpAxis::Z)
return {aabb_center.x, aabb_center.y, min.z};
else if constexpr (Up == UpAxis::X)
return {min.x, aabb_center.y, aabb_center.z};
else if constexpr (Up == UpAxis::Y)
return {aabb_center.x, min.y, aabb_center.z};
else
std::unreachable();
}
[[nodiscard]]
constexpr bool is_collide(const Aabb& other) const noexcept
{
return min.x <= other.max.x && max.x >= other.min.x &&
min.y <= other.max.y && max.y >= other.min.y &&min.z <= other.max.z && max.z >= other.min.z;
}
};
} // namespace omath::primitives

View File

@@ -90,6 +90,25 @@ namespace omath::projection
{
}
struct ProjectionParams
{
FieldOfView fov;
float aspect_ratio;
};
// Recovers vertical FOV and aspect ratio from a perspective projection matrix
// built by any of the engine traits. Both variants (ZERO_TO_ONE and
// NEGATIVE_ONE_TO_ONE) share the same m[0,0]/m[1,1] layout, so this works
// regardless of the NDC depth range.
[[nodiscard]]
static ProjectionParams extract_projection_params(const Mat4X4Type& proj_matrix) noexcept
{
// m[1,1] == 1 / tan(fov/2) => fov = 2 * atan(1 / m[1,1])
const float f = proj_matrix.at(1, 1);
// m[0,0] == m[1,1] / aspect_ratio => aspect = m[1,1] / m[0,0]
return {FieldOfView::from_radians(2.f * std::atan(1.f / f)), f / proj_matrix.at(0, 0)};
}
[[nodiscard]]
static ViewAnglesType calc_view_angles_from_view_matrix(const Mat4X4Type& view_matrix) noexcept
{
@@ -127,9 +146,6 @@ namespace omath::projection
Vector3<float> get_forward() const noexcept
{
const auto& view_matrix = get_view_matrix();
if constexpr (axes.inverted_forward)
return -Vector3<float>{view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
return {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
}
@@ -137,8 +153,6 @@ namespace omath::projection
Vector3<float> get_right() const noexcept
{
const auto& view_matrix = get_view_matrix();
if constexpr (axes.inverted_right)
return -Vector3<float>{view_matrix[0, 0], view_matrix[0, 1], view_matrix[0, 2]};
return {view_matrix[0, 0], view_matrix[0, 1], view_matrix[0, 2]};
}
@@ -148,6 +162,27 @@ namespace omath::projection
const auto& view_matrix = get_view_matrix();
return {view_matrix[1, 0], view_matrix[1, 1], view_matrix[1, 2]};
}
[[nodiscard]]
Vector3<float> get_abs_forward() const noexcept
{
if constexpr (axes.inverted_forward)
return -get_forward();
return get_forward();
}
[[nodiscard]]
Vector3<float> get_abs_right() const noexcept
{
if constexpr (axes.inverted_right)
return -get_right();
return get_right();
}
[[nodiscard]]
Vector3<float> get_abs_up() const noexcept
{
return get_up();
}
[[nodiscard]] const Mat4X4Type& get_view_projection_matrix() const noexcept
{

61
scripts/gource-timelapse.sh Executable file
View File

@@ -0,0 +1,61 @@
#!/bin/bash
# =============================================================================
# Gource Timelapse — renders the repository history as a video
# Requires: gource, ffmpeg
# =============================================================================
set -euo pipefail
# --- Config (override via env vars) ---
OUTPUT="${OUTPUT:-gource-timelapse.mp4}"
SECONDS_PER_DAY="${SECONDS_PER_DAY:-0.1}"
RESOLUTION="${RESOLUTION:-1920x1080}"
FPS="${FPS:-60}"
TITLE="${TITLE:-omath}"
# --- Dependency checks ---
for cmd in gource ffmpeg; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: '$cmd' is not installed."
echo " macOS: brew install $cmd"
echo " Linux: sudo apt install $cmd"
exit 1
fi
done
echo "----------------------------------------------------"
echo "Rendering gource timelapse → $OUTPUT"
echo " Resolution : $RESOLUTION"
echo " FPS : $FPS"
echo " Speed : ${SECONDS_PER_DAY}s per day"
echo "----------------------------------------------------"
gource \
--title "$TITLE" \
--seconds-per-day "$SECONDS_PER_DAY" \
--auto-skip-seconds 0.1 \
--time-scale 3 \
--max-files 0 \
--hide filenames \
--date-format "%Y-%m-%d" \
--multi-sampling \
--bloom-multiplier 0.5 \
--elasticity 0.05 \
--${RESOLUTION%x*}x${RESOLUTION#*x} \
--output-framerate "$FPS" \
--output-ppm-stream - \
| ffmpeg -y \
-r "$FPS" \
-f image2pipe \
-vcodec ppm \
-i - \
-vcodec libx264 \
-preset fast \
-pix_fmt yuv420p \
-crf 18 \
"$OUTPUT"
echo "----------------------------------------------------"
echo "Done: $OUTPUT"
echo "----------------------------------------------------"

View File

@@ -453,3 +453,184 @@ TEST(unit_test_frostbite_engine, ViewAnglesAsVector3NormalizedYaw)
EXPECT_NEAR(vec.y, -90.f, 0.01f);
}
// ---------------------------------------------------------------------------
// extract_projection_params
// ---------------------------------------------------------------------------
// Tolerance: tan/atan round-trip in single precision introduces ~1e-5 rad
// error, which is ~5.7e-4 degrees.
static constexpr float k_fov_tolerance_deg = 0.001f;
static constexpr float k_aspect_tolerance = 1e-5f;
TEST(unit_test_frostbite_engine, ExtractProjectionParams_BasicRoundTrip)
{
// Build a matrix with known inputs and verify both outputs are recovered.
constexpr float fov_deg = 60.f;
constexpr float aspect = 16.f / 9.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.1f, 1000.f, omath::NDCDepthRange::ZERO_TO_ONE);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_NegOneToOneDepthRange)
{
// The FOV/aspect encoding in rows 0 and 1 is identical for both NDC
// depth ranges, so extraction must work the same way.
constexpr float fov_deg = 75.f;
constexpr float aspect = 4.f / 3.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.1f, 500.f, omath::NDCDepthRange::NEGATIVE_ONE_TO_ONE);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_Fov45)
{
constexpr float fov_deg = 45.f;
constexpr float aspect = 16.f / 9.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.01f, 1000.f);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_Fov90)
{
constexpr float fov_deg = 90.f;
constexpr float aspect = 16.f / 9.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.01f, 1000.f);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_Fov120)
{
constexpr float fov_deg = 120.f;
constexpr float aspect = 16.f / 9.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.01f, 1000.f);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_AspectRatio_4by3)
{
constexpr float fov_deg = 60.f;
constexpr float aspect = 4.f / 3.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.1f, 500.f);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_AspectRatio_Ultrawide)
{
constexpr float fov_deg = 90.f;
constexpr float aspect = 21.f / 9.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.1f, 500.f);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_AspectRatio_Square)
{
constexpr float fov_deg = 90.f;
constexpr float aspect = 1.f;
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.1f, 500.f);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_FovAndAspectAreIndependent)
{
// Changing only FOV must not affect recovered aspect ratio, and vice versa.
constexpr float aspect = 16.f / 9.f;
for (const float fov_deg : {45.f, 60.f, 90.f, 110.f})
{
const auto mat = omath::frostbite_engine::calc_perspective_projection_matrix(
fov_deg, aspect, 0.1f, 1000.f);
const auto [fov, ar] = omath::frostbite_engine::Camera::extract_projection_params(mat);
EXPECT_NEAR(fov.as_degrees(), fov_deg, k_fov_tolerance_deg);
EXPECT_NEAR(ar, aspect, k_aspect_tolerance);
}
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_ViaCamera_RoundTrip)
{
// End-to-end: construct a Camera, retrieve its projection matrix, then
// recover the FOV and aspect ratio and compare against the original inputs.
constexpr auto fov_in = omath::projection::FieldOfView::from_degrees(90.f);
constexpr float aspect = 1920.f / 1080.f;
const auto cam = omath::frostbite_engine::Camera(
{0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov_in, 0.01f, 1000.f);
const auto [fov_out, ar_out] =
omath::frostbite_engine::Camera::extract_projection_params(cam.get_projection_matrix());
EXPECT_NEAR(fov_out.as_degrees(), fov_in.as_degrees(), k_fov_tolerance_deg);
EXPECT_NEAR(ar_out, aspect, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_ViaCamera_AfterFovChange)
{
// Verify that the extracted FOV tracks the camera's FOV after set_field_of_view().
auto cam = omath::frostbite_engine::Camera(
{0.f, 0.f, 0.f}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(60.f), 0.01f, 1000.f);
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(110.f));
const auto [fov, ar] =
omath::frostbite_engine::Camera::extract_projection_params(cam.get_projection_matrix());
EXPECT_NEAR(fov.as_degrees(), 110.f, k_fov_tolerance_deg);
EXPECT_NEAR(ar, 1920.f / 1080.f, k_aspect_tolerance);
}
TEST(unit_test_frostbite_engine, ExtractProjectionParams_ViaCamera_AfterViewportChange)
{
// Verify that the extracted aspect ratio tracks the viewport after set_view_port().
auto cam = omath::frostbite_engine::Camera(
{0.f, 0.f, 0.f}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(90.f), 0.01f, 1000.f);
cam.set_view_port({1280.f, 720.f});
const auto [fov, ar] =
omath::frostbite_engine::Camera::extract_projection_params(cam.get_projection_matrix());
EXPECT_NEAR(fov.as_degrees(), 90.f, k_fov_tolerance_deg);
EXPECT_NEAR(ar, 1280.f / 720.f, k_aspect_tolerance);
}

View File

@@ -20,8 +20,8 @@
#include <vector>
#if defined(__linux__)
# include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#if defined(__ANDROID__)
#if __ANDROID_API__ >= 30
#include <sys/mman.h>
@@ -57,9 +57,11 @@ public:
MemFdFile(MemFdFile&& o) noexcept
: m_path(std::move(o.m_path))
#if defined(OMATH_TEST_USE_MEMFD)
, m_fd(o.m_fd)
,
m_fd(o.m_fd)
#else
, m_temp_path(std::move(o.m_temp_path))
,
m_temp_path(std::move(o.m_temp_path))
#endif
{
#if defined(OMATH_TEST_USE_MEMFD)
@@ -69,9 +71,15 @@ public:
#endif
}
[[nodiscard]] bool valid() const { return !m_path.empty(); }
[[nodiscard]] bool valid() const
{
return !m_path.empty();
}
[[nodiscard]] const std::filesystem::path& path() const { return m_path; }
[[nodiscard]] const std::filesystem::path& path() const
{
return m_path;
}
static MemFdFile create(const std::vector<std::uint8_t>& data)
{
@@ -163,25 +171,27 @@ inline std::vector<std::uint8_t> build_minimal_pe(const std::vector<std::uint8_t
std::vector<std::uint8_t> buf(data_off + section_bytes.size(), 0u);
buf[0] = 'M'; buf[1] = 'Z';
buf[0] = 'M';
buf[1] = 'Z';
std::memcpy(buf.data() + 0x3Cu, &e_lfanew, 4);
buf[nt_off] = 'P'; buf[nt_off + 1] = 'E';
buf[nt_off] = 'P';
buf[nt_off + 1] = 'E';
const std::uint16_t machine = 0x8664u, num_sections = 1u;
constexpr std::uint16_t machine = 0x8664u, num_sections = 1u;
std::memcpy(buf.data() + fh_off, &machine, 2);
std::memcpy(buf.data() + fh_off + 2, &num_sections, 2);
std::memcpy(buf.data() + fh_off + 16, &size_opt, 2);
const std::uint16_t magic = 0x20Bu;
constexpr std::uint16_t magic = 0x20Bu;
std::memcpy(buf.data() + oh_off, &magic, 2);
const char name[8] = {'.','t','e','x','t',0,0,0};
constexpr char name[8] = {'.', 't', 'e', 'x', 't', 0, 0, 0};
std::memcpy(buf.data() + sh_off, name, 8);
const auto vsize = static_cast<std::uint32_t>(section_bytes.size());
const std::uint32_t vaddr = 0x1000u;
const auto ptr_raw = static_cast<std::uint32_t>(data_off);
constexpr std::uint32_t vaddr = 0x1000u;
constexpr auto ptr_raw = static_cast<std::uint32_t>(data_off);
std::memcpy(buf.data() + sh_off + 8, &vsize, 4);
std::memcpy(buf.data() + sh_off + 12, &vaddr, 4);
std::memcpy(buf.data() + sh_off + 16, &vsize, 4);

View File

@@ -0,0 +1,240 @@
//
// Created by Vladislav on 19.04.2026.
//
#include <gtest/gtest.h>
#include "omath/3d_primitives/aabb.hpp"
using AABB = omath::primitives::Aabb<float>;
using Vec3 = omath::Vector3<float>;
// --- center() ---
TEST(AabbTests, CenterOfSymmetricBox)
{
constexpr AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
constexpr auto c = box.center();
EXPECT_FLOAT_EQ(c.x, 0.f);
EXPECT_FLOAT_EQ(c.y, 0.f);
EXPECT_FLOAT_EQ(c.z, 0.f);
}
TEST(AabbTests, CenterOfOffsetBox)
{
constexpr AABB box{{1.f, 2.f, 3.f}, {3.f, 6.f, 7.f}};
constexpr auto c = box.center();
EXPECT_FLOAT_EQ(c.x, 2.f);
EXPECT_FLOAT_EQ(c.y, 4.f);
EXPECT_FLOAT_EQ(c.z, 5.f);
}
TEST(AabbTests, CenterOfDegenerateBox)
{
constexpr AABB box{{5.f, 5.f, 5.f}, {5.f, 5.f, 5.f}};
constexpr auto c = box.center();
EXPECT_FLOAT_EQ(c.x, 5.f);
EXPECT_FLOAT_EQ(c.y, 5.f);
EXPECT_FLOAT_EQ(c.z, 5.f);
}
// --- extents() ---
TEST(AabbTests, ExtentsOfSymmetricBox)
{
constexpr AABB box{{-2.f, -3.f, -4.f}, {2.f, 3.f, 4.f}};
constexpr auto e = box.extents();
EXPECT_FLOAT_EQ(e.x, 2.f);
EXPECT_FLOAT_EQ(e.y, 3.f);
EXPECT_FLOAT_EQ(e.z, 4.f);
}
TEST(AabbTests, ExtentsOfUnitBox)
{
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 2.f, 2.f}};
constexpr auto e = box.extents();
EXPECT_FLOAT_EQ(e.x, 1.f);
EXPECT_FLOAT_EQ(e.y, 1.f);
EXPECT_FLOAT_EQ(e.z, 1.f);
}
TEST(AabbTests, ExtentsOfDegenerateBox)
{
constexpr AABB box{{3.f, 3.f, 3.f}, {3.f, 3.f, 3.f}};
constexpr auto e = box.extents();
EXPECT_FLOAT_EQ(e.x, 0.f);
EXPECT_FLOAT_EQ(e.y, 0.f);
EXPECT_FLOAT_EQ(e.z, 0.f);
}
using UpAxis = omath::primitives::UpAxis;
// --- top() ---
TEST(AabbTests, TopYUpSymmetricBox)
{
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto t = box.top<UpAxis::Y>();
EXPECT_FLOAT_EQ(t.x, 0.f);
EXPECT_FLOAT_EQ(t.y, 2.f);
EXPECT_FLOAT_EQ(t.z, 0.f);
}
TEST(AabbTests, TopYUpOffsetBox)
{
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto t = box.top<UpAxis::Y>();
EXPECT_FLOAT_EQ(t.x, 2.f);
EXPECT_FLOAT_EQ(t.y, 10.f);
EXPECT_FLOAT_EQ(t.z, 4.f);
}
TEST(AabbTests, TopZUpSymmetricBox)
{
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto t = box.top<UpAxis::Z>();
EXPECT_FLOAT_EQ(t.x, 0.f);
EXPECT_FLOAT_EQ(t.y, 0.f);
EXPECT_FLOAT_EQ(t.z, 3.f);
}
TEST(AabbTests, TopZUpOffsetBox)
{
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto t = box.top<UpAxis::Z>();
EXPECT_FLOAT_EQ(t.x, 2.f);
EXPECT_FLOAT_EQ(t.y, 7.f);
EXPECT_FLOAT_EQ(t.z, 6.f);
}
TEST(AabbTests, TopDefaultIsYUp)
{
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}};
EXPECT_EQ(box.top(), box.top<UpAxis::Y>());
}
// --- bottom() ---
TEST(AabbTests, BottomYUpSymmetricBox)
{
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto b = box.bottom<UpAxis::Y>();
EXPECT_FLOAT_EQ(b.x, 0.f);
EXPECT_FLOAT_EQ(b.y, -2.f);
EXPECT_FLOAT_EQ(b.z, 0.f);
}
TEST(AabbTests, BottomYUpOffsetBox)
{
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto b = box.bottom<UpAxis::Y>();
EXPECT_FLOAT_EQ(b.x, 2.f);
EXPECT_FLOAT_EQ(b.y, 4.f);
EXPECT_FLOAT_EQ(b.z, 4.f);
}
TEST(AabbTests, BottomZUpSymmetricBox)
{
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
constexpr auto b = box.bottom<UpAxis::Z>();
EXPECT_FLOAT_EQ(b.x, 0.f);
EXPECT_FLOAT_EQ(b.y, 0.f);
EXPECT_FLOAT_EQ(b.z, -3.f);
}
TEST(AabbTests, BottomZUpOffsetBox)
{
constexpr AABB box{{1.f, 4.f, 2.f}, {3.f, 10.f, 6.f}};
constexpr auto b = box.bottom<UpAxis::Z>();
EXPECT_FLOAT_EQ(b.x, 2.f);
EXPECT_FLOAT_EQ(b.y, 7.f);
EXPECT_FLOAT_EQ(b.z, 2.f);
}
TEST(AabbTests, BottomDefaultIsYUp)
{
constexpr AABB box{{0.f, 0.f, 0.f}, {2.f, 4.f, 6.f}};
EXPECT_EQ(box.bottom(), box.bottom<UpAxis::Y>());
}
TEST(AabbTests, TopAndBottomAreSymmetric)
{
constexpr AABB box{{-1.f, -2.f, -3.f}, {1.f, 2.f, 3.f}};
EXPECT_FLOAT_EQ(box.top<UpAxis::Y>().y, -box.bottom<UpAxis::Y>().y);
EXPECT_FLOAT_EQ(box.top<UpAxis::Z>().z, -box.bottom<UpAxis::Z>().z);
}
// --- is_collide() ---
TEST(AabbTests, OverlappingBoxesCollide)
{
constexpr AABB a{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
constexpr AABB b{{0.f, 0.f, 0.f}, {2.f, 2.f, 2.f}};
EXPECT_TRUE(a.is_collide(b));
EXPECT_TRUE(b.is_collide(a));
}
TEST(AabbTests, SeparatedBoxesDoNotCollide)
{
constexpr AABB a{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
constexpr AABB b{{2.f, 2.f, 2.f}, {4.f, 4.f, 4.f}};
EXPECT_FALSE(a.is_collide(b));
EXPECT_FALSE(b.is_collide(a));
}
TEST(AabbTests, TouchingFacesCollide)
{
constexpr AABB a{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
constexpr AABB b{{1.f, -1.f, -1.f}, {3.f, 1.f, 1.f}};
EXPECT_TRUE(a.is_collide(b));
EXPECT_TRUE(b.is_collide(a));
}
TEST(AabbTests, ContainedBoxCollides)
{
constexpr AABB outer{{-3.f, -3.f, -3.f}, {3.f, 3.f, 3.f}};
constexpr AABB inner{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
EXPECT_TRUE(outer.is_collide(inner));
EXPECT_TRUE(inner.is_collide(outer));
}
TEST(AabbTests, SeparatedOnXAxisDoNotCollide)
{
constexpr AABB a{{0.f, 0.f, 0.f}, {1.f, 1.f, 1.f}};
constexpr AABB b{{2.f, 0.f, 0.f}, {3.f, 1.f, 1.f}};
EXPECT_FALSE(a.is_collide(b));
}
TEST(AabbTests, SeparatedOnYAxisDoNotCollide)
{
constexpr AABB a{{0.f, 0.f, 0.f}, {1.f, 1.f, 1.f}};
constexpr AABB b{{0.f, 2.f, 0.f}, {1.f, 3.f, 1.f}};
EXPECT_FALSE(a.is_collide(b));
}
TEST(AabbTests, SeparatedOnZAxisDoNotCollide)
{
constexpr AABB a{{0.f, 0.f, 0.f}, {1.f, 1.f, 1.f}};
constexpr AABB b{{0.f, 0.f, 2.f}, {1.f, 1.f, 3.f}};
EXPECT_FALSE(a.is_collide(b));
}
TEST(AabbTests, IdenticalBoxesCollide)
{
constexpr AABB a{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
EXPECT_TRUE(a.is_collide(a));
}
TEST(AabbTests, DegeneratePointBoxCollidesWhenInsideOther)
{
constexpr AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
constexpr AABB point{{0.f, 0.f, 0.f}, {0.f, 0.f, 0.f}};
EXPECT_TRUE(box.is_collide(point));
EXPECT_TRUE(point.is_collide(box));
}
TEST(AabbTests, DegeneratePointBoxDoesNotCollideWhenOutside)
{
constexpr AABB box{{-1.f, -1.f, -1.f}, {1.f, 1.f, 1.f}};
constexpr AABB point{{5.f, 0.f, 0.f}, {5.f, 0.f, 0.f}};
EXPECT_FALSE(box.is_collide(point));
EXPECT_FALSE(point.is_collide(box));
}

View File

@@ -0,0 +1,275 @@
//
// Created by Vladislav on 20.04.2026.
//
#include <gtest/gtest.h>
#include <omath/engines/cry_engine/traits/pred_engine_trait.hpp>
#include <omath/projectile_prediction/projectile.hpp>
#include <omath/projectile_prediction/target.hpp>
using namespace omath;
using namespace omath::cry_engine;
// ---- predict_projectile_position ----
TEST(CryPredEngineTrait, PredictProjectilePositionAtTimeZero)
{
projectile_prediction::Projectile p;
p.m_origin = {1.f, 2.f, 3.f};
p.m_launch_offset = {4.f, 5.f, 6.f};
p.m_launch_speed = 100.f;
p.m_gravity_scale = 1.f;
const auto pos = PredEngineTrait::predict_projectile_position(p, 0.f, 0.f, 0.f, 9.81f);
// At t=0 no velocity is applied, just origin+offset
EXPECT_NEAR(pos.x, 5.f, 1e-4f);
EXPECT_NEAR(pos.y, 7.f, 1e-4f);
EXPECT_NEAR(pos.z, 9.f, 1e-4f);
}
TEST(CryPredEngineTrait, PredictProjectilePositionZeroAnglesForwardIsY)
{
// Cry engine forward = +Y. At pitch=0, yaw=0 the projectile travels along +Y.
projectile_prediction::Projectile p;
p.m_origin = {0.f, 0.f, 0.f};
p.m_launch_speed = 10.f;
p.m_gravity_scale = 0.f; // no gravity so we isolate direction
const auto pos = PredEngineTrait::predict_projectile_position(p, 0.f, 0.f, 1.f, 9.81f);
EXPECT_NEAR(pos.x, 0.f, 1e-4f);
EXPECT_NEAR(pos.y, 10.f, 1e-4f);
EXPECT_NEAR(pos.z, 0.f, 1e-4f);
}
TEST(CryPredEngineTrait, PredictProjectilePositionGravityDropsZ)
{
projectile_prediction::Projectile p;
p.m_origin = {0.f, 0.f, 0.f};
p.m_launch_speed = 10.f;
p.m_gravity_scale = 1.f;
const auto pos = PredEngineTrait::predict_projectile_position(p, 0.f, 0.f, 2.f, 9.81f);
// z = 0 - (9.81 * 1) * (4) * 0.5 = -19.62
EXPECT_NEAR(pos.z, -9.81f * 4.f * 0.5f, 1e-3f);
}
TEST(CryPredEngineTrait, PredictProjectilePositionGravityScaleZeroNoZDrop)
{
projectile_prediction::Projectile p;
p.m_origin = {0.f, 0.f, 0.f};
p.m_launch_speed = 10.f;
p.m_gravity_scale = 0.f;
const auto pos = PredEngineTrait::predict_projectile_position(p, 0.f, 0.f, 3.f, 9.81f);
EXPECT_NEAR(pos.z, 0.f, 1e-4f);
}
TEST(CryPredEngineTrait, PredictProjectilePositionWithLaunchOffset)
{
projectile_prediction::Projectile p;
p.m_origin = {5.f, 0.f, 0.f};
p.m_launch_offset = {0.f, 0.f, 2.f};
p.m_launch_speed = 10.f;
p.m_gravity_scale = 0.f;
const auto pos = PredEngineTrait::predict_projectile_position(p, 0.f, 0.f, 1.f, 0.f);
// launch position = {5, 0, 2}, travels along +Y by 10
EXPECT_NEAR(pos.x, 5.f, 1e-4f);
EXPECT_NEAR(pos.y, 10.f, 1e-4f);
EXPECT_NEAR(pos.z, 2.f, 1e-4f);
}
// ---- predict_target_position ----
TEST(CryPredEngineTrait, PredictTargetPositionGroundedStationary)
{
projectile_prediction::Target t;
t.m_origin = {10.f, 20.f, 5.f};
t.m_velocity = {0.f, 0.f, 0.f};
t.m_is_airborne = false;
const auto pred = PredEngineTrait::predict_target_position(t, 5.f, 9.81f);
EXPECT_NEAR(pred.x, 10.f, 1e-6f);
EXPECT_NEAR(pred.y, 20.f, 1e-6f);
EXPECT_NEAR(pred.z, 5.f, 1e-6f);
}
TEST(CryPredEngineTrait, PredictTargetPositionGroundedMoving)
{
projectile_prediction::Target t;
t.m_origin = {0.f, 0.f, 0.f};
t.m_velocity = {3.f, 4.f, 0.f};
t.m_is_airborne = false;
const auto pred = PredEngineTrait::predict_target_position(t, 2.f, 9.81f);
EXPECT_NEAR(pred.x, 6.f, 1e-6f);
EXPECT_NEAR(pred.y, 8.f, 1e-6f);
EXPECT_NEAR(pred.z, 0.f, 1e-6f); // grounded — no gravity
}
TEST(CryPredEngineTrait, PredictTargetPositionAirborneGravityDropsZ)
{
projectile_prediction::Target t;
t.m_origin = {0.f, 0.f, 20.f};
t.m_velocity = {0.f, 0.f, 0.f};
t.m_is_airborne = true;
const auto pred = PredEngineTrait::predict_target_position(t, 2.f, 9.81f);
// z = 20 - 9.81 * 4 * 0.5 = 20 - 19.62 = 0.38
EXPECT_NEAR(pred.z, 20.f - 9.81f * 4.f * 0.5f, 1e-4f);
}
TEST(CryPredEngineTrait, PredictTargetPositionAirborneMovingWithGravity)
{
projectile_prediction::Target t;
t.m_origin = {0.f, 0.f, 50.f};
t.m_velocity = {10.f, 5.f, 0.f};
t.m_is_airborne = true;
const auto pred = PredEngineTrait::predict_target_position(t, 3.f, 9.81f);
EXPECT_NEAR(pred.x, 30.f, 1e-4f);
EXPECT_NEAR(pred.y, 15.f, 1e-4f);
EXPECT_NEAR(pred.z, 50.f - 9.81f * 9.f * 0.5f, 1e-4f);
}
// ---- calc_vector_2d_distance ----
TEST(CryPredEngineTrait, CalcVector2dDistance_3_4_5)
{
EXPECT_NEAR(PredEngineTrait::calc_vector_2d_distance({3.f, 4.f, 999.f}), 5.f, 1e-5f);
}
TEST(CryPredEngineTrait, CalcVector2dDistance_ZeroVector)
{
EXPECT_NEAR(PredEngineTrait::calc_vector_2d_distance({0.f, 0.f, 0.f}), 0.f, 1e-6f);
}
TEST(CryPredEngineTrait, CalcVector2dDistance_ZIgnored)
{
// Z does not affect the 2D distance
EXPECT_NEAR(PredEngineTrait::calc_vector_2d_distance({0.f, 5.f, 100.f}),
PredEngineTrait::calc_vector_2d_distance({0.f, 5.f, 0.f}), 1e-6f);
}
// ---- get_vector_height_coordinate ----
TEST(CryPredEngineTrait, GetVectorHeightCoordinate_ReturnsZ)
{
// Cry engine up = +Z
EXPECT_FLOAT_EQ(PredEngineTrait::get_vector_height_coordinate({1.f, 2.f, 7.f}), 7.f);
}
// ---- calc_direct_pitch_angle ----
TEST(CryPredEngineTrait, CalcDirectPitchAngle_Flat)
{
// Target at same height → pitch = 0
EXPECT_NEAR(PredEngineTrait::calc_direct_pitch_angle({0.f, 0.f, 0.f}, {0.f, 100.f, 0.f}), 0.f, 1e-4f);
}
TEST(CryPredEngineTrait, CalcDirectPitchAngle_LookingUp)
{
// Target at 45° above (equal XY distance and Z height)
// direction to {0, 1, 1} normalized = {0, 0.707, 0.707}, asin(0.707) = 45°
EXPECT_NEAR(PredEngineTrait::calc_direct_pitch_angle({0.f, 0.f, 0.f}, {0.f, 1.f, 1.f}), 45.f, 1e-3f);
}
TEST(CryPredEngineTrait, CalcDirectPitchAngle_LookingDown)
{
// Target directly below
EXPECT_NEAR(PredEngineTrait::calc_direct_pitch_angle({0.f, 0.f, 10.f}, {0.f, 0.f, 0.f}), -90.f, 1e-3f);
}
TEST(CryPredEngineTrait, CalcDirectPitchAngle_LookingDirectlyUp)
{
EXPECT_NEAR(PredEngineTrait::calc_direct_pitch_angle({0.f, 0.f, 0.f}, {0.f, 0.f, 100.f}), 90.f, 1e-3f);
}
// ---- calc_direct_yaw_angle ----
TEST(CryPredEngineTrait, CalcDirectYawAngle_ForwardAlongY)
{
// Cry engine forward = +Y → yaw = 0
EXPECT_NEAR(PredEngineTrait::calc_direct_yaw_angle({0.f, 0.f, 0.f}, {0.f, 100.f, 0.f}), 0.f, 1e-4f);
}
TEST(CryPredEngineTrait, CalcDirectYawAngle_AlongPositiveX)
{
// direction = {1, 0, 0}, yaw = -atan2(1, 0) = -90°
EXPECT_NEAR(PredEngineTrait::calc_direct_yaw_angle({0.f, 0.f, 0.f}, {100.f, 0.f, 0.f}), -90.f, 1e-3f);
}
TEST(CryPredEngineTrait, CalcDirectYawAngle_AlongNegativeX)
{
// direction = {-1, 0, 0}, yaw = -atan2(-1, 0) = 90°
EXPECT_NEAR(PredEngineTrait::calc_direct_yaw_angle({0.f, 0.f, 0.f}, {-100.f, 0.f, 0.f}), 90.f, 1e-3f);
}
TEST(CryPredEngineTrait, CalcDirectYawAngle_BackwardAlongNegY)
{
// direction = {0, -1, 0}, yaw = -atan2(0, -1) = ±180°
const float yaw = PredEngineTrait::calc_direct_yaw_angle({0.f, 0.f, 0.f}, {0.f, -100.f, 0.f});
EXPECT_NEAR(std::abs(yaw), 180.f, 1e-3f);
}
TEST(CryPredEngineTrait, CalcDirectYawAngle_OffOriginCamera)
{
// Same relative direction regardless of camera position
const float yaw_a = PredEngineTrait::calc_direct_yaw_angle({0.f, 0.f, 0.f}, {0.f, 100.f, 0.f});
const float yaw_b = PredEngineTrait::calc_direct_yaw_angle({50.f, 50.f, 0.f}, {50.f, 150.f, 0.f});
EXPECT_NEAR(yaw_a, yaw_b, 1e-4f);
}
// ---- calc_viewpoint_from_angles ----
TEST(CryPredEngineTrait, CalcViewpointFromAngles_45Degrees)
{
projectile_prediction::Projectile p;
p.m_origin = {0.f, 0.f, 0.f};
p.m_launch_speed = 10.f;
// Target along +Y at distance 10; pitch=45° → height = 10 * tan(45°) = 10
const Vector3<float> target{0.f, 10.f, 0.f};
const auto vp = PredEngineTrait::calc_viewpoint_from_angles(p, target, 45.f);
EXPECT_NEAR(vp.x, 0.f, 1e-4f);
EXPECT_NEAR(vp.y, 10.f, 1e-4f);
EXPECT_NEAR(vp.z, 10.f, 1e-3f);
}
TEST(CryPredEngineTrait, CalcViewpointFromAngles_ZeroPitch)
{
projectile_prediction::Projectile p;
p.m_origin = {0.f, 0.f, 5.f};
p.m_launch_speed = 1.f;
const Vector3<float> target{3.f, 4.f, 0.f};
const auto vp = PredEngineTrait::calc_viewpoint_from_angles(p, target, 0.f);
// tan(0) = 0 → viewpoint Z = origin.z + 0 = 5
EXPECT_NEAR(vp.x, 3.f, 1e-4f);
EXPECT_NEAR(vp.y, 4.f, 1e-4f);
EXPECT_NEAR(vp.z, 5.f, 1e-4f);
}
TEST(CryPredEngineTrait, CalcViewpointXYMatchesPredictedTargetXY)
{
projectile_prediction::Projectile p;
p.m_origin = {1.f, 2.f, 3.f};
p.m_launch_speed = 50.f;
const Vector3<float> target{10.f, 20.f, 5.f};
const auto vp = PredEngineTrait::calc_viewpoint_from_angles(p, target, 30.f);
// X and Y always match the predicted target position
EXPECT_NEAR(vp.x, target.x, 1e-4f);
EXPECT_NEAR(vp.y, target.y, 1e-4f);
}

View File

@@ -11,6 +11,7 @@
#include <omath/engines/opengl_engine/camera.hpp>
#include <omath/engines/source_engine/camera.hpp>
#include <omath/engines/unreal_engine/camera.hpp>
#include <omath/linear_algebra/triangle.hpp>
#include <omath/projection/camera.hpp>
#include <print>
#include <random>
@@ -793,9 +794,9 @@ TEST(UnitTestProjection, SourceEngine_ZeroAngles_BasisVectors)
constexpr float k_eps = 1e-5f;
const auto cam = omath::source_engine::Camera({}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(90.f), 0.01f, 1000.f);
const auto fwd = cam.get_forward();
const auto right = cam.get_right();
const auto up = cam.get_up();
const auto fwd = cam.get_abs_forward();
const auto right = cam.get_abs_right();
const auto up = cam.get_abs_up();
EXPECT_NEAR(fwd.x, omath::source_engine::k_abs_forward.x, k_eps);
EXPECT_NEAR(fwd.y, omath::source_engine::k_abs_forward.y, k_eps);
@@ -815,9 +816,9 @@ TEST(UnitTestProjection, UnityEngine_ZeroAngles_BasisVectors)
constexpr float k_eps = 1e-5f;
const auto cam = omath::unity_engine::Camera({}, {}, {1280.f, 720.f},
omath::projection::FieldOfView::from_degrees(60.f), 0.03f, 1000.f);
const auto fwd = cam.get_forward();
const auto right = cam.get_right();
const auto up = cam.get_up();
const auto fwd = cam.get_abs_forward();
const auto right = cam.get_abs_right();
const auto up = cam.get_abs_up();
EXPECT_NEAR(fwd.x, omath::unity_engine::k_abs_forward.x, k_eps);
EXPECT_NEAR(fwd.y, omath::unity_engine::k_abs_forward.y, k_eps);
@@ -837,9 +838,9 @@ TEST(UnitTestProjection, OpenGLEngine_ZeroAngles_BasisVectors)
constexpr float k_eps = 1e-5f;
const auto cam = omath::opengl_engine::Camera({}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(90.f), 0.01f, 1000.f);
const auto fwd = cam.get_forward();
const auto right = cam.get_right();
const auto up = cam.get_up();
const auto fwd = cam.get_abs_forward();
const auto right = cam.get_abs_right();
const auto up = cam.get_abs_up();
EXPECT_NEAR(fwd.x, omath::opengl_engine::k_abs_forward.x, k_eps);
EXPECT_NEAR(fwd.y, omath::opengl_engine::k_abs_forward.y, k_eps);
@@ -859,9 +860,9 @@ TEST(UnitTestProjection, UnrealEngine_ZeroAngles_BasisVectors)
constexpr float k_eps = 1e-5f;
const auto cam = omath::unreal_engine::Camera({}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(90.f), 0.01f, 1000.f);
const auto fwd = cam.get_forward();
const auto right = cam.get_right();
const auto up = cam.get_up();
const auto fwd = cam.get_abs_forward();
const auto right = cam.get_abs_right();
const auto up = cam.get_abs_up();
EXPECT_NEAR(fwd.x, omath::unreal_engine::k_abs_forward.x, k_eps);
EXPECT_NEAR(fwd.y, omath::unreal_engine::k_abs_forward.y, k_eps);
@@ -881,9 +882,9 @@ TEST(UnitTestProjection, FrostbiteEngine_ZeroAngles_BasisVectors)
constexpr float k_eps = 1e-5f;
const auto cam = omath::frostbite_engine::Camera({}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(90.f), 0.01f, 1000.f);
const auto fwd = cam.get_forward();
const auto right = cam.get_right();
const auto up = cam.get_up();
const auto fwd = cam.get_abs_forward();
const auto right = cam.get_abs_right();
const auto up = cam.get_abs_up();
EXPECT_NEAR(fwd.x, omath::frostbite_engine::k_abs_forward.x, k_eps);
EXPECT_NEAR(fwd.y, omath::frostbite_engine::k_abs_forward.y, k_eps);
@@ -903,9 +904,9 @@ TEST(UnitTestProjection, CryEngine_ZeroAngles_BasisVectors)
constexpr float k_eps = 1e-5f;
const auto cam = omath::cry_engine::Camera({}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(90.f), 0.01f, 1000.f);
const auto fwd = cam.get_forward();
const auto right = cam.get_right();
const auto up = cam.get_up();
const auto fwd = cam.get_abs_forward();
const auto right = cam.get_abs_right();
const auto up = cam.get_abs_up();
EXPECT_NEAR(fwd.x, omath::cry_engine::k_abs_forward.x, k_eps);
EXPECT_NEAR(fwd.y, omath::cry_engine::k_abs_forward.y, k_eps);
@@ -925,9 +926,9 @@ TEST(UnitTestProjection, IWEngine_ZeroAngles_BasisVectors)
constexpr float k_eps = 1e-5f;
const auto cam = omath::iw_engine::Camera({}, {}, {1920.f, 1080.f},
omath::projection::FieldOfView::from_degrees(90.f), 0.01f, 1000.f);
const auto fwd = cam.get_forward();
const auto right = cam.get_right();
const auto up = cam.get_up();
const auto fwd = cam.get_abs_forward();
const auto right = cam.get_abs_right();
const auto up = cam.get_abs_up();
EXPECT_NEAR(fwd.x, omath::iw_engine::k_abs_forward.x, k_eps);
EXPECT_NEAR(fwd.y, omath::iw_engine::k_abs_forward.y, k_eps);
@@ -941,3 +942,247 @@ TEST(UnitTestProjection, IWEngine_ZeroAngles_BasisVectors)
EXPECT_NEAR(up.y, omath::iw_engine::k_abs_up.y, k_eps);
EXPECT_NEAR(up.z, omath::iw_engine::k_abs_up.z, k_eps);
}
// ---- extract_projection_params ----
TEST(UnitTestProjection, ExtractProjectionParams_FovRoundTrip)
{
// Source engine applies a 0.75 scale factor to its projection matrix, so
// extract_projection_params (standard formula) does not round-trip with it.
// Use Unity engine, which uses a standard projection matrix.
constexpr float k_eps = 1e-4f;
constexpr auto fov = omath::projection::FieldOfView::from_degrees(75.f);
const auto cam = omath::unity_engine::Camera({}, {}, {1280.f, 720.f}, fov, 0.03f, 1000.f);
const auto params = omath::unity_engine::Camera::extract_projection_params(cam.get_projection_matrix());
EXPECT_NEAR(params.fov.as_degrees(), 75.f, k_eps);
}
TEST(UnitTestProjection, ExtractProjectionParams_AspectRatioRoundTrip)
{
constexpr float k_eps = 1e-4f;
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
const auto params = omath::source_engine::Camera::extract_projection_params(cam.get_projection_matrix());
EXPECT_NEAR(params.aspect_ratio, 1920.f / 1080.f, k_eps);
}
TEST(UnitTestProjection, ExtractProjectionParams_UnityEngine)
{
constexpr float k_eps = 1e-4f;
constexpr auto fov = omath::projection::FieldOfView::from_degrees(60.f);
const auto cam = omath::unity_engine::Camera({}, {}, {1280.f, 720.f}, fov, 0.03f, 1000.f);
const auto params = omath::unity_engine::Camera::extract_projection_params(cam.get_projection_matrix());
EXPECT_NEAR(params.fov.as_degrees(), 60.f, k_eps);
EXPECT_NEAR(params.aspect_ratio, 1280.f / 720.f, k_eps);
}
// ---- Accessors ----
TEST(UnitTestProjection, Accessors_GetFovNearFarOrigin)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const omath::Vector3<float> origin{10.f, 20.f, 30.f};
const auto cam = omath::source_engine::Camera(origin, {}, {1920.f, 1080.f}, fov, 0.1f, 500.f);
EXPECT_NEAR(cam.get_field_of_view().as_degrees(), 90.f, 1e-4f);
EXPECT_FLOAT_EQ(cam.get_near_plane(), 0.1f);
EXPECT_FLOAT_EQ(cam.get_far_plane(), 500.f);
EXPECT_FLOAT_EQ(cam.get_origin().x, 10.f);
EXPECT_FLOAT_EQ(cam.get_origin().y, 20.f);
EXPECT_FLOAT_EQ(cam.get_origin().z, 30.f);
}
// ---- Setters + cache invalidation ----
TEST(UnitTestProjection, SetFieldOfView_InvalidatesProjection)
{
constexpr auto fov_a = omath::projection::FieldOfView::from_degrees(90.f);
constexpr auto fov_b = omath::projection::FieldOfView::from_degrees(45.f);
auto cam = omath::source_engine::Camera({}, {}, {1920.f, 1080.f}, fov_a, 0.01f, 1000.f);
const auto proj_before = cam.get_projection_matrix();
cam.set_field_of_view(fov_b);
const auto proj_after = cam.get_projection_matrix();
EXPECT_NE(proj_before.at(0, 0), proj_after.at(0, 0));
EXPECT_NEAR(cam.get_field_of_view().as_degrees(), 45.f, 1e-4f);
}
TEST(UnitTestProjection, SetNearPlane_InvalidatesProjection)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
auto cam = omath::source_engine::Camera({}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
const auto proj_before = cam.get_projection_matrix();
cam.set_near_plane(1.f);
const auto proj_after = cam.get_projection_matrix();
EXPECT_FLOAT_EQ(cam.get_near_plane(), 1.f);
EXPECT_NE(proj_before.at(2, 2), proj_after.at(2, 2));
}
TEST(UnitTestProjection, SetFarPlane_InvalidatesProjection)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
auto cam = omath::source_engine::Camera({}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
const auto proj_before = cam.get_projection_matrix();
cam.set_far_plane(100.f);
const auto proj_after = cam.get_projection_matrix();
EXPECT_FLOAT_EQ(cam.get_far_plane(), 100.f);
EXPECT_NE(proj_before.at(2, 2), proj_after.at(2, 2));
}
TEST(UnitTestProjection, SetOrigin_InvalidatesViewMatrix)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
// Target is off to the side — stays at the same world position while camera
// moves laterally, so the projected X must change.
const auto screen_before = cam.world_to_screen({500.f, 100.f, 0.f});
cam.set_origin({0.f, 100.f, 0.f}); // lateral shift
const auto screen_after = cam.world_to_screen({500.f, 100.f, 0.f});
ASSERT_TRUE(screen_before.has_value());
ASSERT_TRUE(screen_after.has_value());
EXPECT_NE(screen_before->x, screen_after->x);
EXPECT_FLOAT_EQ(cam.get_origin().y, 100.f);
}
TEST(UnitTestProjection, SetViewPort_InvalidatesProjection)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
auto cam = omath::source_engine::Camera({}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
const auto proj_before = cam.get_projection_matrix();
// 1280x800 is 8:5, different aspect ratio from 1920x1080 (16:9)
cam.set_view_port({1280.f, 800.f});
const auto proj_after = cam.get_projection_matrix();
EXPECT_NE(proj_before.at(0, 0), proj_after.at(0, 0));
}
TEST(UnitTestProjection, SetViewAngles_InvalidatesViewMatrix)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
auto cam = omath::source_engine::Camera({}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
const auto view_before = cam.get_view_matrix();
const omath::source_engine::ViewAngles rotated{
omath::source_engine::PitchAngle::from_degrees(30.f),
omath::source_engine::YawAngle::from_degrees(45.f),
omath::source_engine::RollAngle::from_degrees(0.f)
};
cam.set_view_angles(rotated);
const auto view_after = cam.get_view_matrix();
EXPECT_NE(view_before.at(0, 0), view_after.at(0, 0));
}
// ---- calc_look_at_angles / look_at ----
TEST(UnitTestProjection, CalcLookAtAngles_ForwardTarget)
{
// Source engine: +X is forward. Camera at origin, target on +X axis.
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
const auto angles = cam.calc_look_at_angles({100.f, 0.f, 0.f});
EXPECT_NEAR(angles.pitch.as_degrees(), 0.f, 1e-4f);
EXPECT_NEAR(angles.yaw.as_degrees(), 0.f, 1e-4f);
}
TEST(UnitTestProjection, LookAt_ForwardVectorPointsAtTarget)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
cam.look_at({200.f, 0.f, 0.f});
const auto fwd = cam.get_abs_forward();
// After pointing at +X target the forward vector should be ~(1,0,0)
EXPECT_NEAR(fwd.x, 1.f, 1e-4f);
EXPECT_NEAR(fwd.y, 0.f, 1e-4f);
EXPECT_NEAR(fwd.z, 0.f, 1e-4f);
}
// ---- is_culled_by_frustum (triangle) ----
TEST(UnitTestProjection, TriangleInsideFrustumNotCulled)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
// Small triangle directly in front (Source: +X forward)
const omath::Triangle<omath::Vector3<float>> tri{
{100.f, 0.f, 1.f},
{100.f, 1.f, -1.f},
{100.f, -1.f, -1.f}
};
EXPECT_FALSE(cam.is_culled_by_frustum(tri));
}
TEST(UnitTestProjection, TriangleBehindCameraCulled)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
// Triangle entirely behind the camera (-X)
const omath::Triangle<omath::Vector3<float>> tri{
{-100.f, 0.f, 1.f},
{-100.f, 1.f, -1.f},
{-100.f, -1.f, -1.f}
};
EXPECT_TRUE(cam.is_culled_by_frustum(tri));
}
TEST(UnitTestProjection, TriangleBeyondFarPlaneCulled)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
// Triangle beyond the 1000-unit far plane
const omath::Triangle<omath::Vector3<float>> tri{
{2000.f, 0.f, 1.f},
{2000.f, 1.f, -1.f},
{2000.f, -1.f, -1.f}
};
EXPECT_TRUE(cam.is_culled_by_frustum(tri));
}
TEST(UnitTestProjection, TriangleFarToSideCulled)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
// Triangle far outside the side frustum planes
const omath::Triangle<omath::Vector3<float>> tri{
{100.f, 5000.f, 0.f},
{100.f, 5001.f, 1.f},
{100.f, 5001.f, -1.f}
};
EXPECT_TRUE(cam.is_culled_by_frustum(tri));
}
TEST(UnitTestProjection, TriangleStraddlingFrustumNotCulled)
{
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
const auto cam = omath::source_engine::Camera({0.f, 0.f, 0.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
// Large triangle with vertices on both sides of the frustum — should not be culled
const omath::Triangle<omath::Vector3<float>> tri{
{ 100.f, 0.f, 0.f},
{ 100.f, 5000.f, 0.f},
{ 100.f, 0.f, 5000.f}
};
EXPECT_FALSE(cam.is_culled_by_frustum(tri));
}