From 504eaeed7fded670a22165f454b9fa2bfb694826 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Sat, 17 Feb 2024 21:10:22 -0500 Subject: [PATCH] More work on camera tests --- src/engine/Camera.cpp | 3 +- src/engine/Camera.hpp | 4 +- tests/src/CameraTesting.cpp | 79 ++++++++++++++++++++++++++++++++++++ tests/src/FrustumTesting.cpp | 38 ++++++++++++----- tests/src/TransformTests.cpp | 5 --- 5 files changed, 111 insertions(+), 18 deletions(-) diff --git a/src/engine/Camera.cpp b/src/engine/Camera.cpp index 20cf410..10118ce 100644 --- a/src/engine/Camera.cpp +++ b/src/engine/Camera.cpp @@ -244,7 +244,8 @@ namespace fgl::engine ZoneScoped; //Flip Z due to the fact we use Z+ outside of this function. It must be Z- inside - position.z = -position.z; + //position.z = -position.z; + // Maybe unneeded? switch ( mode ) { diff --git a/src/engine/Camera.hpp b/src/engine/Camera.hpp index 514a135..b8eddeb 100644 --- a/src/engine/Camera.hpp +++ b/src/engine/Camera.hpp @@ -79,13 +79,13 @@ namespace fgl::engine const Vector getUp() const { return Vector( - glm::normalize( glm::vec3( view_matrix[ 0 ][ 1 ], view_matrix[ 1 ][ 1 ], view_matrix[ 2 ][ 1 ] ) ) ); + glm::normalize( -glm::vec3( view_matrix[ 0 ][ 1 ], view_matrix[ 1 ][ 1 ], view_matrix[ 2 ][ 1 ] ) ) ); } const Vector getRight() const { return Vector( - glm::normalize( glm::vec3( -view_matrix[ 0 ][ 0 ], view_matrix[ 1 ][ 0 ], view_matrix[ 2 ][ 0 ] ) ) ); + glm::normalize( glm::vec3( view_matrix[ 0 ][ 0 ], view_matrix[ 1 ][ 0 ], view_matrix[ 2 ][ 0 ] ) ) ); } const Vector getForward() const diff --git a/tests/src/CameraTesting.cpp b/tests/src/CameraTesting.cpp index ef877a1..4f769d7 100644 --- a/tests/src/CameraTesting.cpp +++ b/tests/src/CameraTesting.cpp @@ -24,6 +24,8 @@ TEST_CASE( "Camera", "[camera]" ) camera.setOrthographicProjection( 1.0f, 1.0f, 1.0f, 1.0f, constants::NEAR_PLANE, constants::FAR_PLANE ); } + camera.setPerspectiveProjection( 90.0f, 1.0f, constants::NEAR_PLANE, constants::FAR_PLANE ); + SECTION( "Default orientation" ) { const auto camera_up { camera.getUp() }; @@ -61,4 +63,81 @@ TEST_CASE( "Camera", "[camera]" ) REQUIRE( camera_forward.z <= std::numeric_limits< float >::epsilon() ); } } + + SECTION( "Camera projection test - Perspective" ) + { + WHEN( "Camera is translated right by WORLD_RIGHT" ) + { + camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_RIGHT, Rotation( 0.0f ) ); + + THEN( "camera.getPosition() should be WORLD_RIGHT" ) + { + const auto position { camera.getPosition() }; + REQUIRE( position == constants::WORLD_RIGHT ); + } + + THEN( "A point at the origin should be translated to the left" ) + { + const auto matrix { camera.getProjectionViewMatrix() }; + const auto point { matrix * glm::vec4( constants::WORLD_CENTER, 1.0f ) }; + + REQUIRE( point.x < 0.0f ); + REQUIRE( point.y == 0.0f ); + } + } + + WHEN( "Camera is translated up by WORLD_UP" ) + { + camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_UP, Rotation( 0.0f ) ); + + THEN( "camera.getPosition() should be WORLD_UP" ) + { + const auto position { camera.getPosition() }; + REQUIRE( position == constants::WORLD_UP ); + } + + THEN( "A point at the origin should be translated down" ) + { + const auto matrix { camera.getProjectionViewMatrix() }; + const auto point { matrix * glm::vec4( constants::WORLD_CENTER, 1.0f ) }; + + REQUIRE( point.x == 0.0f ); + REQUIRE( point.y < 0.0f ); + } + } + + WHEN( "Camera is translated forward by WORLD_FORWARD" ) + { + camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_FORWARD, Rotation( 0.0f ) ); + + THEN( "camera.getPosition() should be WORLD_FORWARD" ) + { + const auto position { camera.getPosition() }; + REQUIRE( position == constants::WORLD_FORWARD ); + } + + THEN( "A point at the origin should be translated back" ) + { + const auto matrix { camera.getProjectionViewMatrix() }; + const auto point { matrix * glm::vec4( constants::WORLD_CENTER, 1.0f ) }; + + REQUIRE( point.x <= 0.0f ); + REQUIRE( point.y <= 0.0f ); + } + } + } + + WHEN( "Two points are translated by the camera when directly in front" ) + { + const auto point_near { constants::WORLD_FORWARD }; + const auto point_far { constants::WORLD_FORWARD * 2.0f }; + + const auto projected_near { camera.getProjectionViewMatrix() * glm::vec4( point_near, 1.0f ) }; + const auto projected_far { camera.getProjectionViewMatrix() * glm::vec4( point_far, 1.0f ) }; + + THEN( "The near point should be closer than the far point" ) + { + REQUIRE( projected_near.z < projected_far.z ); + } + } } \ No newline at end of file diff --git a/tests/src/FrustumTesting.cpp b/tests/src/FrustumTesting.cpp index ff19111..9d66250 100644 --- a/tests/src/FrustumTesting.cpp +++ b/tests/src/FrustumTesting.cpp @@ -66,30 +66,48 @@ TEST_CASE( "Frustum", "[frustum][rotation][translation]" ) } } - SECTION( "Forward" ) + WHEN( "Translated Forward" ) { camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_FORWARD, Rotation( 0.0f, 0.0f, 0.0f ) ); //Translate forward by 1 world unit const auto translated_forward { camera.getFrustumBounds() }; - //Verify that during a translation the direction isn't changed - REQUIRE( translated_forward.near.direction() == base_frustum.near.direction() ); + THEN( "Direction is the same" ) + { + //Verify that during a translation the direction isn't changed + REQUIRE( translated_forward.near.direction() == base_frustum.near.direction() ); + } - REQUIRE( translated_forward.near.direction() == constants::WORLD_FORWARD ); - REQUIRE( translated_forward.near.distance() == constants::NEAR_PLANE + 1.0f ); + THEN( "The near plane should be translated backwards" ) + { + REQUIRE( translated_forward.near.direction() == constants::WORLD_FORWARD ); + REQUIRE( translated_forward.near.distance() == constants::NEAR_PLANE + 1.0f ); + REQUIRE( + translated_forward.near.getPosition() + == ( constants::WORLD_FORWARD * ( constants::NEAR_PLANE + 1.0f ) ) ); + } - REQUIRE( translated_forward.far.direction() == constants::WORLD_BACKWARD ); - REQUIRE( translated_forward.far.distance() == -( constants::FAR_PLANE + 1.0f ) ); + THEN( "The far plane should be translated backwards" ) + { + REQUIRE( translated_forward.far.direction() == constants::WORLD_BACKWARD ); + REQUIRE( translated_forward.far.distance() == -( constants::FAR_PLANE + 1.0f ) ); + REQUIRE( + translated_forward.far.getPosition() + == ( constants::WORLD_FORWARD * ( constants::FAR_PLANE + 1.0f ) ) ); + } } - SECTION( "Up" ) + WHEN( "Translated Up" ) { camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_UP, Rotation( 0.0f, 0.0f, 0.0f ) ); //Translate up by 1 world unit const auto translated_up { camera.getFrustumBounds() }; - //Verify that during a translation the direction isn't changed - REQUIRE( translated_up.near.direction() == base_frustum.near.direction() ); + THEN( "Direction is the same" ) + { + //Verify that during a translation the direction isn't changed + REQUIRE( translated_up.near.direction() == base_frustum.near.direction() ); + } REQUIRE( translated_up.near.direction() == constants::WORLD_FORWARD ); REQUIRE( translated_up.near.distance() == constants::NEAR_PLANE ); diff --git a/tests/src/TransformTests.cpp b/tests/src/TransformTests.cpp index 6bfa607..d02549a 100644 --- a/tests/src/TransformTests.cpp +++ b/tests/src/TransformTests.cpp @@ -9,11 +9,6 @@ using namespace fgl::engine; -bool epsilonEqual( const float a, const float b ) -{ - return std::abs( a - b ) <= std::numeric_limits< float >::epsilon(); -} - TEST_CASE( "Transform", "[transform][rotation][translation]" ) { TransformComponent component;