mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 20:43:27 +00:00
Compare commits
2 Commits
f3f454b02e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 20930c629a | |||
| 0845a2e863 |
@@ -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;
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -793,9 +793,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 +815,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 +837,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 +859,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 +881,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 +903,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 +925,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);
|
||||
|
||||
Reference in New Issue
Block a user