Skip to content

Commit 8ccc945

Browse files
committed
added additional method
1 parent 2ee4852 commit 8ccc945

3 files changed

Lines changed: 60 additions & 1 deletion

File tree

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.3.0
1+
5.4.0

include/omath/projection/camera.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,23 @@ namespace omath::projection
466466
return false;
467467
}
468468

469+
[[nodiscard("You must view camera space coordinates")]]
470+
constexpr Vector3<NumericType> world_to_view_coordinates(const Vector3<NumericType>& world_coordinates) const noexcept
471+
{
472+
if consteval
473+
{
474+
const auto view_coordinates =
475+
calc_view_matrix()
476+
* mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(world_coordinates);
477+
478+
return {view_coordinates.at(0, 0), view_coordinates.at(1, 0), view_coordinates.at(2, 0)};
479+
}
480+
const auto view_coordinates =
481+
get_view_matrix()
482+
* mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(world_coordinates);
483+
484+
return {view_coordinates.at(0, 0), view_coordinates.at(1, 0), view_coordinates.at(2, 0)};
485+
}
469486
[[nodiscard("You must use view port position")]] constexpr std::expected<Vector3<NumericType>, Error>
470487
world_to_view_port(const Vector3<NumericType>& world_position,
471488
const ViewPortClipping& clipping = ViewPortClipping::AUTO) const noexcept

tests/general/unit_test_projection.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,48 @@ TEST(UnitTestProjection, AabbUnityEngineStraddlesNearNotCulled)
580580
EXPECT_FALSE(cam.is_aabb_culled_by_frustum(aabb));
581581
}
582582

583+
TEST(UnitTestProjection, WorldToViewCoordinates_TranslatedSourceCamera)
584+
{
585+
constexpr float k_eps = 1e-4f;
586+
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
587+
auto cam = omath::source_engine::Camera({10.f, 20.f, 30.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
588+
589+
const auto view_coordinates = cam.world_to_view_coordinates({15.f, 12.f, 37.f});
590+
591+
EXPECT_NEAR(view_coordinates.x, 8.f, k_eps);
592+
EXPECT_NEAR(view_coordinates.y, 7.f, k_eps);
593+
EXPECT_NEAR(view_coordinates.z, 5.f, k_eps);
594+
}
595+
596+
TEST(UnitTestProjection, WorldToViewCoordinates_RotatedSourceCamera)
597+
{
598+
constexpr float k_eps = 1e-4f;
599+
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
600+
const omath::source_engine::ViewAngles angles{omath::source_engine::PitchAngle::from_degrees(0.f),
601+
omath::source_engine::YawAngle::from_degrees(90.f),
602+
omath::source_engine::RollAngle::from_degrees(0.f)};
603+
auto cam = omath::source_engine::Camera({10.f, 20.f, 30.f}, angles, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
604+
605+
const auto view_coordinates = cam.world_to_view_coordinates({14.f, 26.f, 38.f});
606+
607+
EXPECT_NEAR(view_coordinates.x, 4.f, k_eps);
608+
EXPECT_NEAR(view_coordinates.y, 8.f, k_eps);
609+
EXPECT_NEAR(view_coordinates.z, 6.f, k_eps);
610+
}
611+
612+
TEST(UnitTestProjection, WorldToViewCoordinates_ColumnMajorOpenGlCamera)
613+
{
614+
constexpr float k_eps = 1e-4f;
615+
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
616+
auto cam = omath::opengl_engine::Camera({10.f, 20.f, 30.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
617+
618+
const auto view_coordinates = cam.world_to_view_coordinates({14.f, 26.f, 22.f});
619+
620+
EXPECT_NEAR(view_coordinates.x, 4.f, k_eps);
621+
EXPECT_NEAR(view_coordinates.y, 6.f, k_eps);
622+
EXPECT_NEAR(view_coordinates.z, -8.f, k_eps);
623+
}
624+
583625
TEST(UnitTestProjection, CalcViewAnglesFromViewMatrix_LookingForward)
584626
{
585627
constexpr float k_eps = 1e-4f;

0 commit comments

Comments
 (0)