From fab4c492335e4f0234458b60548d51e4a46bdff4 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Mon, 19 Feb 2024 18:35:33 -0500 Subject: [PATCH] So many steps backwards --- src/engine/Camera.cpp | 3 ++- src/engine/Camera.hpp | 6 ++--- tests/src/CameraTesting.cpp | 47 ++++++++++++++++++++++++++++++++++++ tests/src/gtest_printers.hpp | 10 ++++++++ 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/src/engine/Camera.cpp b/src/engine/Camera.cpp index 404f173..87932e1 100644 --- a/src/engine/Camera.cpp +++ b/src/engine/Camera.cpp @@ -64,7 +64,8 @@ namespace fgl::engine const glm::vec3 camera_up { rotation_matrix * glm::vec4( constants::WORLD_UP, 0.0f ) }; - view_matrix = Matrix< MatrixType::WorldToCamera >( glm::lookAtLH( pos, pos + forward, camera_up ) ); + view_matrix = + Matrix< MatrixType::WorldToCamera >( glm::lookAtLH( pos, pos + forward, -camera_up ) ); inverse_view_matrix = glm::inverse( view_matrix ); break; diff --git a/src/engine/Camera.hpp b/src/engine/Camera.hpp index 74aa0b0..b00c037 100644 --- a/src/engine/Camera.hpp +++ b/src/engine/Camera.hpp @@ -83,9 +83,9 @@ namespace fgl::engine return WorldCoordinate( glm::inverse( view_matrix )[ 3 ] ); } - const Vector getUp() const { return Vector( glm::normalize( glm::vec3( inverse_view_matrix[ 1 ] ) ) ); } + const Vector getUp() const { return -getDown(); } - const Vector getRight() const { return -Vector( glm::normalize( glm::vec3( inverse_view_matrix[ 0 ] ) ) ); } + const Vector getRight() const { return Vector( glm::normalize( glm::vec3( inverse_view_matrix[ 0 ] ) ) ); } const Vector getForward() const { return Vector( glm::normalize( glm::vec3( inverse_view_matrix[ 2 ] ) ) ); } @@ -93,7 +93,7 @@ namespace fgl::engine const Vector getBackward() const { return -getForward(); } - const Vector getDown() const { return -getUp(); } + const Vector getDown() const { return Vector( glm::normalize( glm::vec3( inverse_view_matrix[ 1 ] ) ) ); } void setViewDirection( glm::vec3 pos, const Vector direction, glm::vec3 up = constants::WORLD_UP ); void setViewTarget( glm::vec3 pos, glm::vec3 target, glm::vec3 up = constants::WORLD_UP ); diff --git a/tests/src/CameraTesting.cpp b/tests/src/CameraTesting.cpp index e91a47c..65b39a7 100644 --- a/tests/src/CameraTesting.cpp +++ b/tests/src/CameraTesting.cpp @@ -32,6 +32,8 @@ TEST_CASE( "Camera", "[camera]" ) { camera.setView( constants::WORLD_CENTER, Rotation( 0.0f ) ); + CAPTURE( camera.view_matrix ); + THEN( "Camera up is WORLD_UP" ) { const auto camera_up { camera.getUp() }; @@ -124,6 +126,48 @@ TEST_CASE( "Camera", "[camera]" ) } } + WHEN( "Camera is translated left by WORLD_LEFT" ) + { + camera.setView( constants::WORLD_CENTER + constants::WORLD_LEFT, Rotation( 0.0f ) ); + + THEN( "camera.getPosition() should be WORLD_UP" ) + { + REQUIRE( camera.getPosition() == constants::WORLD_LEFT ); + } + + THEN( "A point at the origin should be translated to the right" ) + { + const auto matrix { camera.getProjectionViewMatrix() }; + const glm::vec3 point { matrix * glm::vec4( constants::WORLD_CENTER, 1.0f ) }; + + CAPTURE( point ); + + REQUIRE( point.x > 0.0f ); + REQUIRE( point.y == 0.0f ); + } + } + + WHEN( "Camera is translated down by WORLD_DOWN " ) + { + camera.setView( constants::WORLD_CENTER + constants::WORLD_DOWN, Rotation( 0.0f ) ); + + THEN( "camera.getPosition() should be WORLD_DOWN" ) + { + REQUIRE( camera.getPosition() == constants::WORLD_DOWN ); + } + + THEN( "A point at the origin must be translated up" ) + { + const auto matrix { camera.getProjectionViewMatrix() }; + const glm::vec3 point { matrix * glm::vec4( constants::WORLD_CENTER, 1.0f ) }; + + CAPTURE( point ); + + REQUIRE( point.x == 0.0f ); + REQUIRE( point.y > 0.0f ); + } + } + WHEN( "Camera is translated up by WORLD_UP" ) { camera.setView( constants::WORLD_CENTER + constants::WORLD_UP, Rotation( 0.0f ) ); @@ -161,8 +205,11 @@ TEST_CASE( "Camera", "[camera]" ) const auto matrix { camera.getProjectionViewMatrix() }; const auto point { matrix * glm::vec4( constants::WORLD_CENTER, 1.0f ) }; + CAPTURE( point ); + REQUIRE( point.x <= 0.0f ); REQUIRE( point.y <= 0.0f ); + REQUIRE( point.z < 0.0f ); } } } diff --git a/tests/src/gtest_printers.hpp b/tests/src/gtest_printers.hpp index e3ecbda..9ac7714 100644 --- a/tests/src/gtest_printers.hpp +++ b/tests/src/gtest_printers.hpp @@ -9,6 +9,7 @@ #include #include +#include "engine/primitives/Matrix.hpp" #include "engine/primitives/Rotation.hpp" #include "engine/primitives/Vector.hpp" @@ -48,6 +49,15 @@ namespace Catch } }; + template < fgl::engine::MatrixType MType > + struct StringMaker< fgl::engine::Matrix< MType > > + { + static std::string convert( const fgl::engine::Matrix< MType >& mat ) + { + return StringMaker< glm::mat4 >::convert( static_cast< glm::mat4 >( mat ) ); + } + }; + } // namespace Catch #ifndef NDEBUG