mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 17:03:27 +00:00
updated z range
This commit is contained in:
@@ -21,6 +21,9 @@
|
||||
#include <omath/engines/unreal_engine/traits/camera_trait.hpp>
|
||||
|
||||
#include <omath/engines/source_engine/traits/pred_engine_trait.hpp>
|
||||
#include <omath/engines/source_engine/traits/camera_trait.hpp>
|
||||
|
||||
#include <omath/engines/cry_engine/traits/camera_trait.hpp>
|
||||
|
||||
#include <omath/projectile_prediction/projectile.hpp>
|
||||
#include <omath/projectile_prediction/target.hpp>
|
||||
@@ -218,9 +221,14 @@ TEST(TraitTests, Frostbite_Pred_And_Mesh_And_Camera)
|
||||
// CameraTrait look at should be callable
|
||||
const auto angles = e::CameraTrait::calc_look_at_angle({0, 0, 0}, {0, 1, 1});
|
||||
(void)angles;
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f);
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
|
||||
expect_matrix_near(proj, expected);
|
||||
|
||||
const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
expect_matrix_near(proj_zo, expected_zo);
|
||||
EXPECT_NE(proj, proj_zo);
|
||||
}
|
||||
|
||||
TEST(TraitTests, IW_Pred_And_Mesh_And_Camera)
|
||||
@@ -264,10 +272,15 @@ TEST(TraitTests, IW_Pred_And_Mesh_And_Camera)
|
||||
e::ViewAngles va;
|
||||
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va));
|
||||
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(45.f), {1920.f, 1080.f}, 0.1f, 1000.f);
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(45.f), {1920.f, 1080.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
const auto expected = e::calc_perspective_projection_matrix(45.f, 1920.f / 1080.f, 0.1f, 1000.f);
|
||||
expect_matrix_near(proj, expected);
|
||||
|
||||
const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(45.f), {1920.f, 1080.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
const auto expected_zo = e::calc_perspective_projection_matrix(45.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
expect_matrix_near(proj_zo, expected_zo);
|
||||
EXPECT_NE(proj, proj_zo);
|
||||
|
||||
// non-airborne
|
||||
t.m_is_airborne = false;
|
||||
const auto pred_ground_iw = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f);
|
||||
@@ -314,10 +327,15 @@ TEST(TraitTests, OpenGL_Pred_And_Mesh_And_Camera)
|
||||
e::ViewAngles va;
|
||||
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va));
|
||||
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f);
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
|
||||
expect_matrix_near(proj, expected);
|
||||
|
||||
const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
expect_matrix_near(proj_zo, expected_zo);
|
||||
EXPECT_NE(proj, proj_zo);
|
||||
|
||||
// non-airborne
|
||||
t.m_is_airborne = false;
|
||||
const auto pred_ground_gl = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f);
|
||||
@@ -364,10 +382,15 @@ TEST(TraitTests, Unity_Pred_And_Mesh_And_Camera)
|
||||
e::ViewAngles va;
|
||||
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va));
|
||||
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f);
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
|
||||
expect_matrix_near(proj, expected);
|
||||
|
||||
const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
expect_matrix_near(proj_zo, expected_zo);
|
||||
EXPECT_NE(proj, proj_zo);
|
||||
|
||||
// non-airborne
|
||||
t.m_is_airborne = false;
|
||||
const auto pred_ground_unity = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f);
|
||||
@@ -414,12 +437,237 @@ TEST(TraitTests, Unreal_Pred_And_Mesh_And_Camera)
|
||||
e::ViewAngles va;
|
||||
expect_matrix_near(e::MeshTrait::rotation_matrix(va), e::rotation_matrix(va));
|
||||
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f);
|
||||
const auto proj = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
const auto expected = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f);
|
||||
expect_matrix_near(proj, expected);
|
||||
|
||||
const auto proj_zo = e::CameraTrait::calc_projection_matrix(projection::FieldOfView::from_degrees(60.f), {1280.f, 720.f}, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
const auto expected_zo = e::calc_perspective_projection_matrix(60.f, 1280.f / 720.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
expect_matrix_near(proj_zo, expected_zo);
|
||||
EXPECT_NE(proj, proj_zo);
|
||||
|
||||
// non-airborne
|
||||
t.m_is_airborne = false;
|
||||
const auto pred_ground_unreal = e::PredEngineTrait::predict_target_position(t, 2.f, 9.81f);
|
||||
EXPECT_NEAR(pred_ground_unreal.x, 4.f, 1e-6f);
|
||||
}
|
||||
|
||||
// ── NDC Depth Range tests for Source and CryEngine camera traits ────────────
|
||||
|
||||
TEST(NDCDepthRangeTests, Source_BothDepthRanges)
|
||||
{
|
||||
namespace e = omath::source_engine;
|
||||
|
||||
const auto proj_no = e::CameraTrait::calc_projection_matrix(
|
||||
projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f,
|
||||
NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
const auto expected_no = e::calc_perspective_projection_matrix(
|
||||
90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
expect_matrix_near(proj_no, expected_no);
|
||||
|
||||
const auto proj_zo = e::CameraTrait::calc_projection_matrix(
|
||||
projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f,
|
||||
NDCDepthRange::ZERO_TO_ONE);
|
||||
const auto expected_zo = e::calc_perspective_projection_matrix(
|
||||
90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
expect_matrix_near(proj_zo, expected_zo);
|
||||
|
||||
EXPECT_NE(proj_no, proj_zo);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, CryEngine_BothDepthRanges)
|
||||
{
|
||||
namespace e = omath::cry_engine;
|
||||
|
||||
const auto proj_no = e::CameraTrait::calc_projection_matrix(
|
||||
projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f,
|
||||
NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
const auto expected_no = e::calc_perspective_projection_matrix(
|
||||
90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
expect_matrix_near(proj_no, expected_no);
|
||||
|
||||
const auto proj_zo = e::CameraTrait::calc_projection_matrix(
|
||||
projection::FieldOfView::from_degrees(90.f), {1920.f, 1080.f}, 0.1f, 1000.f,
|
||||
NDCDepthRange::ZERO_TO_ONE);
|
||||
const auto expected_zo = e::calc_perspective_projection_matrix(
|
||||
90.f, 1920.f / 1080.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
expect_matrix_near(proj_zo, expected_zo);
|
||||
|
||||
EXPECT_NE(proj_no, proj_zo);
|
||||
}
|
||||
|
||||
// ── Verify Z mapping for ZERO_TO_ONE across all engines ─────────────────────
|
||||
|
||||
// Helper: projects a point at given z through a left-handed projection matrix and returns NDC z
|
||||
static float project_z_lh(const Mat<4, 4>& proj, float z)
|
||||
{
|
||||
auto clip = proj * mat_column_from_vector<float>({0, 0, z});
|
||||
return clip.at(2, 0) / clip.at(3, 0);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, Source_ZeroToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::source_engine;
|
||||
// Source is left-handed
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f);
|
||||
EXPECT_GT(project_z_lh(proj, 500.f), 0.0f);
|
||||
EXPECT_LT(project_z_lh(proj, 500.f), 1.0f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, IW_ZeroToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::iw_engine;
|
||||
// IW is left-handed
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f);
|
||||
EXPECT_GT(project_z_lh(proj, 500.f), 0.0f);
|
||||
EXPECT_LT(project_z_lh(proj, 500.f), 1.0f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, OpenGL_ZeroToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::opengl_engine;
|
||||
// OpenGL is right-handed (negative z forward), column-major
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
|
||||
// OpenGL engine uses column-major matrices, project manually
|
||||
auto proj_z = [&](float z) {
|
||||
auto clip = proj * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>({0, 0, z});
|
||||
return clip.at(2, 0) / clip.at(3, 0);
|
||||
};
|
||||
|
||||
EXPECT_NEAR(proj_z(-0.1f), 0.0f, 1e-4f);
|
||||
EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-4f);
|
||||
EXPECT_GT(proj_z(-500.f), 0.0f);
|
||||
EXPECT_LT(proj_z(-500.f), 1.0f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, Frostbite_ZeroToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::frostbite_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f);
|
||||
EXPECT_GT(project_z_lh(proj, 500.f), 0.0f);
|
||||
EXPECT_LT(project_z_lh(proj, 500.f), 1.0f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, Unity_ZeroToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::unity_engine;
|
||||
// Unity is right-handed, row-major
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
|
||||
auto proj_z = [&](float z) {
|
||||
auto clip = proj * mat_column_from_vector<float>({0, 0, z});
|
||||
return clip.at(2, 0) / clip.at(3, 0);
|
||||
};
|
||||
|
||||
EXPECT_NEAR(proj_z(-0.1f), 0.0f, 1e-4f);
|
||||
EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-4f);
|
||||
EXPECT_GT(proj_z(-500.f), 0.0f);
|
||||
EXPECT_LT(proj_z(-500.f), 1.0f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, Unreal_ZeroToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::unreal_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f);
|
||||
EXPECT_GT(project_z_lh(proj, 500.f), 0.0f);
|
||||
EXPECT_LT(project_z_lh(proj, 500.f), 1.0f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, CryEngine_ZeroToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::cry_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::ZERO_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), 0.0f, 1e-4f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-4f);
|
||||
EXPECT_GT(project_z_lh(proj, 500.f), 0.0f);
|
||||
EXPECT_LT(project_z_lh(proj, 500.f), 1.0f);
|
||||
}
|
||||
|
||||
// ── Verify Z mapping for NEGATIVE_ONE_TO_ONE across all engines ─────────────
|
||||
|
||||
TEST(NDCDepthRangeTests, Source_NegativeOneToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::source_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, IW_NegativeOneToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::iw_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, Frostbite_NegativeOneToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::frostbite_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, Unreal_NegativeOneToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::unreal_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, CryEngine_NegativeOneToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::cry_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
|
||||
EXPECT_NEAR(project_z_lh(proj, 0.1f), -1.0f, 1e-3f);
|
||||
EXPECT_NEAR(project_z_lh(proj, 1000.f), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, OpenGL_NegativeOneToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::opengl_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
|
||||
auto proj_z = [&](float z) {
|
||||
auto clip = proj * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>({0, 0, z});
|
||||
return clip.at(2, 0) / clip.at(3, 0);
|
||||
};
|
||||
|
||||
EXPECT_NEAR(proj_z(-0.1f), -1.0f, 1e-3f);
|
||||
EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(NDCDepthRangeTests, Unity_NegativeOneToOne_ZRange)
|
||||
{
|
||||
namespace e = omath::unity_engine;
|
||||
const auto proj = e::calc_perspective_projection_matrix(90.f, 16.f / 9.f, 0.1f, 1000.f, NDCDepthRange::NEGATIVE_ONE_TO_ONE);
|
||||
|
||||
auto proj_z = [&](float z) {
|
||||
auto clip = proj * mat_column_from_vector<float>({0, 0, z});
|
||||
return clip.at(2, 0) / clip.at(3, 0);
|
||||
};
|
||||
|
||||
EXPECT_NEAR(proj_z(-0.1f), -1.0f, 1e-3f);
|
||||
EXPECT_NEAR(proj_z(-1000.f), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
@@ -240,4 +240,126 @@ TEST(UnitTestMatStandalone, MatPerspectiveLeftHanded)
|
||||
projected /= projected.at(3, 0);
|
||||
|
||||
EXPECT_TRUE(projected.at(2, 0) > -1.0f && projected.at(2, 0) < 0.f);
|
||||
}
|
||||
|
||||
TEST(UnitTestMatStandalone, MatPerspectiveLeftHandedZeroToOne)
|
||||
{
|
||||
const auto proj = mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
|
||||
90.f, 16.f / 9.f, 0.1f, 1000.f);
|
||||
|
||||
// Near plane point should map to z ~ 0
|
||||
auto near_pt = proj * mat_column_from_vector<float>({0, 0, 0.1f});
|
||||
near_pt /= near_pt.at(3, 0);
|
||||
EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f);
|
||||
|
||||
// Far plane point should map to z ~ 1
|
||||
auto far_pt = proj * mat_column_from_vector<float>({0, 0, 1000.f});
|
||||
far_pt /= far_pt.at(3, 0);
|
||||
EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f);
|
||||
|
||||
// Mid-range point should be in [0, 1]
|
||||
auto mid_pt = proj * mat_column_from_vector<float>({0, 0, 500.f});
|
||||
mid_pt /= mid_pt.at(3, 0);
|
||||
EXPECT_GT(mid_pt.at(2, 0), 0.0f);
|
||||
EXPECT_LT(mid_pt.at(2, 0), 1.0f);
|
||||
}
|
||||
|
||||
TEST(UnitTestMatStandalone, MatPerspectiveRightHandedZeroToOne)
|
||||
{
|
||||
const auto proj = mat_perspective_right_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
|
||||
90.f, 16.f / 9.f, 0.1f, 1000.f);
|
||||
|
||||
// Near plane point (negative z for right-handed) should map to z ~ 0
|
||||
auto near_pt = proj * mat_column_from_vector<float>({0, 0, -0.1f});
|
||||
near_pt /= near_pt.at(3, 0);
|
||||
EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f);
|
||||
|
||||
// Far plane point should map to z ~ 1
|
||||
auto far_pt = proj * mat_column_from_vector<float>({0, 0, -1000.f});
|
||||
far_pt /= far_pt.at(3, 0);
|
||||
EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f);
|
||||
|
||||
// Mid-range point should be in [0, 1]
|
||||
auto mid_pt = proj * mat_column_from_vector<float>({0, 0, -500.f});
|
||||
mid_pt /= mid_pt.at(3, 0);
|
||||
EXPECT_GT(mid_pt.at(2, 0), 0.0f);
|
||||
EXPECT_LT(mid_pt.at(2, 0), 1.0f);
|
||||
}
|
||||
|
||||
TEST(UnitTestMatStandalone, MatPerspectiveNegativeOneToOneRange)
|
||||
{
|
||||
// Verify existing [-1, 1] behavior with explicit template arg matches default
|
||||
const auto proj_default = mat_perspective_left_handed(90.f, 16.f / 9.f, 0.1f, 1000.f);
|
||||
const auto proj_explicit = mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR,
|
||||
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(90.f, 16.f / 9.f, 0.1f, 1000.f);
|
||||
|
||||
EXPECT_EQ(proj_default, proj_explicit);
|
||||
|
||||
// Near plane should map to z ~ -1
|
||||
auto near_pt = proj_default * mat_column_from_vector<float>({0, 0, 0.1f});
|
||||
near_pt /= near_pt.at(3, 0);
|
||||
EXPECT_NEAR(near_pt.at(2, 0), -1.0f, 1e-3f);
|
||||
|
||||
// Far plane should map to z ~ 1
|
||||
auto far_pt = proj_default * mat_column_from_vector<float>({0, 0, 1000.f});
|
||||
far_pt /= far_pt.at(3, 0);
|
||||
EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-3f);
|
||||
}
|
||||
|
||||
TEST(UnitTestMatStandalone, MatPerspectiveZeroToOneEquanity)
|
||||
{
|
||||
// LH and RH should produce same NDC for mirrored z
|
||||
constexpr omath::Vector3<float> left_handed = {0, 2, 10};
|
||||
constexpr omath::Vector3<float> right_handed = {0, 2, -10};
|
||||
|
||||
const auto proj_lh = mat_perspective_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
|
||||
90.f, 16.f / 9.f, 0.1f, 1000.f);
|
||||
const auto proj_rh = mat_perspective_right_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
|
||||
90.f, 16.f / 9.f, 0.1f, 1000.f);
|
||||
|
||||
auto ndc_lh = proj_lh * mat_column_from_vector(left_handed);
|
||||
auto ndc_rh = proj_rh * mat_column_from_vector(right_handed);
|
||||
|
||||
ndc_lh /= ndc_lh.at(3, 0);
|
||||
ndc_rh /= ndc_rh.at(3, 0);
|
||||
|
||||
EXPECT_EQ(ndc_lh, ndc_rh);
|
||||
}
|
||||
|
||||
TEST(UnitTestMatStandalone, MatOrthoLeftHandedZeroToOne)
|
||||
{
|
||||
const auto ortho = mat_ortho_left_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
|
||||
-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f);
|
||||
|
||||
// Near plane should map to z ~ 0
|
||||
auto near_pt = ortho * mat_column_from_vector<float>({0, 0, 0.1f});
|
||||
EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f);
|
||||
|
||||
// Far plane should map to z ~ 1
|
||||
auto far_pt = ortho * mat_column_from_vector<float>({0, 0, 100.f});
|
||||
EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f);
|
||||
}
|
||||
|
||||
TEST(UnitTestMatStandalone, MatOrthoRightHandedZeroToOne)
|
||||
{
|
||||
const auto ortho = mat_ortho_right_handed<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
|
||||
-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f);
|
||||
|
||||
// Near plane (negative z for RH) should map to z ~ 0
|
||||
auto near_pt = ortho * mat_column_from_vector<float>({0, 0, -0.1f});
|
||||
EXPECT_NEAR(near_pt.at(2, 0), 0.0f, 1e-4f);
|
||||
|
||||
// Far plane should map to z ~ 1
|
||||
auto far_pt = ortho * mat_column_from_vector<float>({0, 0, -100.f});
|
||||
EXPECT_NEAR(far_pt.at(2, 0), 1.0f, 1e-4f);
|
||||
}
|
||||
|
||||
TEST(UnitTestMatStandalone, MatOrthoNegativeOneToOneDefault)
|
||||
{
|
||||
// Verify explicit [-1, 1] matches default
|
||||
const auto ortho_default = mat_ortho_left_handed(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f);
|
||||
const auto ortho_explicit = mat_ortho_left_handed<float, MatStoreType::ROW_MAJOR,
|
||||
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(-1.f, 1.f, -1.f, 1.f, 0.1f, 100.f);
|
||||
|
||||
EXPECT_EQ(ortho_default, ortho_explicit);
|
||||
}
|
||||
Reference in New Issue
Block a user