From acd1891edd101699a575c0adeaf552fd90c0e5dd Mon Sep 17 00:00:00 2001 From: kj16609 Date: Sun, 18 Feb 2024 18:11:06 -0500 Subject: [PATCH] Even more camera tests --- src/engine/Camera.hpp | 10 +++++++--- src/engine/constants.hpp | 2 +- src/engine/primitives/Coordinate.hpp | 13 +++++++++++++ tests/src/CameraTesting.cpp | 17 +++++++++++++++++ tests/src/gtest_printers.hpp | 4 ++++ 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/engine/Camera.hpp b/src/engine/Camera.hpp index b8eddeb..ee14d46 100644 --- a/src/engine/Camera.hpp +++ b/src/engine/Camera.hpp @@ -74,18 +74,22 @@ namespace fgl::engine void setOrthographicProjection( float left, float right, float top, float bottom, float near, float far ); void setPerspectiveProjection( float fovy, float aspect, float near, float far ); - const Coordinate< CoordinateSpace::World > getPosition() const { return WorldCoordinate( -view_matrix[ 3 ] ); } + const Coordinate< CoordinateSpace::World > getPosition() const + { + //Should maybe store the inverse view matrix + return WorldCoordinate( glm::inverse( view_matrix )[ 3 ] ); + } 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/src/engine/constants.hpp b/src/engine/constants.hpp index 50fc79d..d34cfc7 100644 --- a/src/engine/constants.hpp +++ b/src/engine/constants.hpp @@ -31,6 +31,6 @@ namespace fgl::engine::constants constexpr float FAR_PLANE { 100.0f }; constexpr glm::vec3 CENTER { 0.0f, 0.0f, 0.0f }; - constexpr auto EPSILON { std::numeric_limits< float >::epsilon() }; + constexpr auto EPSILON { std::numeric_limits< float >::epsilon() * 2 }; } // namespace fgl::engine::constants diff --git a/src/engine/primitives/Coordinate.hpp b/src/engine/primitives/Coordinate.hpp index bfd1767..3b7cd4e 100644 --- a/src/engine/primitives/Coordinate.hpp +++ b/src/engine/primitives/Coordinate.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include #include @@ -65,6 +66,18 @@ namespace fgl::engine glm::vec3::operator=( other ); return *this; } + +#ifndef NDEBUG + bool operator==( const Coordinate& other ) const { return *this == static_cast< glm::vec3 >( other ); } + + bool operator==( const glm::vec3 other ) const + { + const auto diff { glm::abs( static_cast< glm::vec3 >( *this ) - other ) }; + return glm::all( glm::lessThanEqual( diff, glm::vec3( constants::EPSILON ) ) ); + //These should have the same behaviour. I'm kind of confused why they don't? + //return glm::all( glm::epsilonEqual( static_cast< glm::vec3 >( *this ), other, constants::EPSILON ) ); + } +#endif }; using ModelCoordinate = Coordinate< CoordinateSpace::Model >; diff --git a/tests/src/CameraTesting.cpp b/tests/src/CameraTesting.cpp index 4f769d7..c782cd0 100644 --- a/tests/src/CameraTesting.cpp +++ b/tests/src/CameraTesting.cpp @@ -66,6 +66,23 @@ TEST_CASE( "Camera", "[camera]" ) SECTION( "Camera projection test - Perspective" ) { + WHEN( "Camera is rotated" ) + { + float x_gen { GENERATE( 0.0f, 90.0f, 180.0f, 270.0f ) }; + float y_gen { GENERATE( 0.0f, 90.0f, 180.0f, 270.0f ) }; + float z_gen { GENERATE( 0.0f, 90.0f, 180.0f, 270.0f ) }; + + const Rotation rotation { x_gen, y_gen, z_gen }; + + camera.setViewYXZ( constants::WORLD_RIGHT, rotation ); + + THEN( "Camera translation should not change" ) + { + const Coordinate< CoordinateSpace::World > position { camera.getPosition() }; + REQUIRE( position == constants::WORLD_RIGHT ); + } + } + WHEN( "Camera is translated right by WORLD_RIGHT" ) { camera.setViewYXZ( constants::WORLD_CENTER + constants::WORLD_RIGHT, Rotation( 0.0f ) ); diff --git a/tests/src/gtest_printers.hpp b/tests/src/gtest_printers.hpp index 85293ea..af9a6e1 100644 --- a/tests/src/gtest_printers.hpp +++ b/tests/src/gtest_printers.hpp @@ -30,6 +30,7 @@ namespace Catch } // namespace Catch +#ifndef NDEBUG namespace glm { inline bool operator==( const glm::vec3& lhs, const glm::vec3& rhs ) @@ -38,3 +39,6 @@ namespace glm } } // namespace glm +#else +#warning "Debug mode not enabled. Tests will pass when checking for floating point equality." +#endif