From 22514804980780cb88aca5efe3b530c7363930a6 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Tue, 1 Oct 2024 01:37:50 -0400 Subject: [PATCH] More bounding box fixes --- .../debug/logging/formatters/matrix.cpp | 22 +++++++++++++ .../debug/logging/formatters/matrix.hpp | 32 +++++++++++++++++++ src/engine/debug/logging/logging.hpp | 1 + src/engine/primitives/TransformComponent.hpp | 32 +++++++++++++++---- 4 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/engine/debug/logging/formatters/matrix.cpp create mode 100644 src/engine/debug/logging/formatters/matrix.hpp diff --git a/src/engine/debug/logging/formatters/matrix.cpp b/src/engine/debug/logging/formatters/matrix.cpp new file mode 100644 index 0000000..4c4044e --- /dev/null +++ b/src/engine/debug/logging/formatters/matrix.cpp @@ -0,0 +1,22 @@ +// +// Created by kj16609 on 10/1/24. +// + +#include "matrix.hpp" + +#include + +std::format_context::iterator std::formatter< glm::vec4 >::format( const glm::vec4& vec, format_context& ctx ) const +{ + return format_to( ctx.out(), "vec4( x: {:10f}, y: {:10f}, z: {:10f}, w: {:10f} )", vec.x, vec.y, vec.z, vec.w ); +} + +std::format_context::iterator std::formatter< glm::vec3 >::format( const glm::vec3& vec, format_context& ctx ) const +{ + return format_to( ctx.out(), "vec3( x: {}, y: {}, z: {} )", vec.x, vec.y, vec.z ); +} + +std::format_context::iterator std::formatter< glm::mat4 >::format( const glm::mat4& mat, format_context& ctx ) const +{ + return format_to( ctx.out(), "mat4: [\n{},\n{},\n{},\n{} ]", mat[ 0 ], mat[ 1 ], mat[ 2 ], mat[ 3 ] ); +} \ No newline at end of file diff --git a/src/engine/debug/logging/formatters/matrix.hpp b/src/engine/debug/logging/formatters/matrix.hpp new file mode 100644 index 0000000..3997d24 --- /dev/null +++ b/src/engine/debug/logging/formatters/matrix.hpp @@ -0,0 +1,32 @@ +// +// Created by kj16609 on 10/1/24. +// + +#pragma once +#include + +#include + +template <> +struct std::formatter< glm::vec4 > +{ + constexpr format_parse_context::iterator parse( format_parse_context& ctx ) { return ctx.begin(); } + + format_context::iterator format( const glm::vec4& vec, format_context& ctx ) const; +}; + +template <> +struct std::formatter< glm::vec3 > +{ + constexpr format_parse_context::iterator parse( format_parse_context& ctx ) { return ctx.begin(); } + + format_context::iterator format( const glm::vec3& vec, format_context& ctx ) const; +}; + +template <> +struct std::formatter< glm::mat4 > +{ + constexpr format_parse_context::iterator parse( format_parse_context& ctx ) { return ctx.begin(); } + + format_context::iterator format( const glm::mat4& mat, format_context& ctx ) const; +}; \ No newline at end of file diff --git a/src/engine/debug/logging/logging.hpp b/src/engine/debug/logging/logging.hpp index f47d664..1c23ea2 100644 --- a/src/engine/debug/logging/logging.hpp +++ b/src/engine/debug/logging/logging.hpp @@ -12,6 +12,7 @@ #pragma GCC diagnostic pop #include "formatters/filesystem.hpp" +#include "formatters/matrix.hpp" namespace fgl::engine::log { diff --git a/src/engine/primitives/TransformComponent.hpp b/src/engine/primitives/TransformComponent.hpp index edc80d8..5bf6ac6 100644 --- a/src/engine/primitives/TransformComponent.hpp +++ b/src/engine/primitives/TransformComponent.hpp @@ -7,6 +7,7 @@ #include "Rotation.hpp" #include "Scale.hpp" #include "engine/FGL_DEFINES.hpp" +#include "engine/debug/logging/logging.hpp" #include "engine/primitives/points/Coordinate.hpp" namespace fgl::engine @@ -67,6 +68,7 @@ namespace fgl::engine // if any of the values in scale is zero then we need to set it to something very small // otherwise the decompose function will fail + /* if ( transform.scale.x == 0.0f ) transform.scale.x = constants::EPSILON; if ( transform.scale.y == 0.0f ) transform.scale.y = constants::EPSILON; if ( transform.scale.z == 0.0f ) transform.scale.z = constants::EPSILON; @@ -75,15 +77,31 @@ namespace fgl::engine if ( matrix[ 0 ][ 0 ] == 0.0f ) matrix[ 0 ][ 0 ] = constants::EPSILON; if ( matrix[ 1 ][ 1 ] == 0.0f ) matrix[ 1 ][ 1 ] = constants::EPSILON; if ( matrix[ 2 ][ 2 ] == 0.0f ) matrix[ 2 ][ 2 ] = constants::EPSILON; + */ - const auto combined_matrix { matrix * transform.mat() }; + auto combined_matrix { matrix * transform.mat() }; - glm::vec3 scale {}, translation {}; - [[maybe_unused]] glm::vec3 skew {}; - glm::quat quat {}; - [[maybe_unused]] glm::vec4 perspective {}; + if ( combined_matrix[ 0 ][ 0 ] == 0.0f ) combined_matrix[ 0 ][ 0 ] = constants::EPSILON; + if ( combined_matrix[ 1 ][ 1 ] == 0.0f ) combined_matrix[ 1 ][ 1 ] = constants::EPSILON; + if ( combined_matrix[ 2 ][ 2 ] == 0.0f ) combined_matrix[ 2 ][ 2 ] = constants::EPSILON; - glm::decompose( combined_matrix, scale, quat, translation, skew, perspective ); + const auto NaN { std::numeric_limits< float >::quiet_NaN() }; + + glm::vec3 scale { NaN }, translation { NaN }; + [[maybe_unused]] glm::vec3 skew { NaN }; + glm::quat quat { NaN, NaN, NaN, NaN }; + [[maybe_unused]] glm::vec4 perspective { NaN }; + + if ( !glm::decompose( combined_matrix, scale, quat, translation, skew, perspective ) ) + { + log::warn( + "Failed to decompose matrix:\n{},\n{}", + static_cast< glm::mat4 >( matrix ), + static_cast< glm::mat4 >( combined_matrix ) ); + } + + assert( !std::isnan( scale.x ) ); + assert( scale != glm::vec3( 0.0f, 0.0f, 0.0f ) ); return { Coordinate< EvolvedType< MType >() >( translation ), Scale( scale ), Rotation( quat ) }; } @@ -93,4 +111,4 @@ namespace fgl::engine // using ModelTransform = TransformComponent< CoordinateSpace::Model >; using WorldTransform = TransformComponent< CoordinateSpace::World >; -} // namespace fgl::engine +} // namespace fgl::engine \ No newline at end of file