From 7d1efebad218c42fcb2f4aea385b933e407f5d4f Mon Sep 17 00:00:00 2001 From: kj16609 Date: Fri, 16 Aug 2024 22:37:40 -0400 Subject: [PATCH] Fixes stupid bounding box fuckery --- src/engine/primitives/TransformComponent.hpp | 14 ++++ .../primitives/boxes/OrientedBoundingBox.cpp | 70 +++++++++++-------- .../primitives/boxes/OrientedBoundingBox.hpp | 22 +----- src/engine/primitives/matricies/Matrix.hpp | 11 --- src/engine/primitives/points/Coordinate.hpp | 13 ++-- src/engine/systems/DrawPair.cpp | 2 +- 6 files changed, 65 insertions(+), 67 deletions(-) diff --git a/src/engine/primitives/TransformComponent.hpp b/src/engine/primitives/TransformComponent.hpp index d6a58e6..9984c02 100644 --- a/src/engine/primitives/TransformComponent.hpp +++ b/src/engine/primitives/TransformComponent.hpp @@ -58,6 +58,20 @@ namespace fgl::engine NormalVector down() const { return -up(); } }; + template < MatrixType MType, CoordinateSpace CType > + TransformComponent< EvolvedType< MType >() > + operator*( const Matrix< MType >& matrix, const TransformComponent< CType >& transform ) + { + const auto combined_matrix { matrix * transform.mat() }; + + [[maybe_unused]] glm::vec3 scale {}, translation {}, skew {}; + glm::quat quat {}; + [[maybe_unused]] glm::vec4 perspective {}; + glm::decompose( combined_matrix, scale, quat, translation, skew, perspective ); + + return { Coordinate< EvolvedType< MType >() >( translation ), Scale( scale ), Rotation( quat ) }; + } + // A game object will be going from world to camera space using GameObjectTransform = TransformComponent< CoordinateSpace::World >; // using ModelTransform = TransformComponent< CoordinateSpace::Model >; diff --git a/src/engine/primitives/boxes/OrientedBoundingBox.cpp b/src/engine/primitives/boxes/OrientedBoundingBox.cpp index dec6836..0131423 100644 --- a/src/engine/primitives/boxes/OrientedBoundingBox.cpp +++ b/src/engine/primitives/boxes/OrientedBoundingBox.cpp @@ -8,8 +8,8 @@ #include -#include "engine/debug/logging/logging.hpp" #include "engine/assets/model/ModelVertex.hpp" +#include "engine/debug/logging/logging.hpp" #include "engine/primitives/lines/LineSegment.hpp" #include "engine/primitives/points/Coordinate.hpp" @@ -88,26 +88,27 @@ namespace fgl::engine template < CoordinateSpace CType > std::array< Coordinate< CType >, interface::BoundingBox::POINT_COUNT > OrientedBoundingBox< CType >::points() const { - std::array< Coordinate< CType >, POINT_COUNT > points {}; - // xp == x positive (Highest x point) // xn == x negative (Lowest x point) - const glm::vec3 xp_yp_zp { m_transform.translation.vec() + static_cast< glm::vec3 >( m_transform.scale ) }; - const glm::vec3 xn_yn_zn { m_transform.translation.vec() - static_cast< glm::vec3 >( m_transform.scale ) }; + //const glm::vec3 xp_yp_zp { m_transform.translation.vec() + static_cast< glm::vec3 >( m_transform.scale ) }; + //const glm::vec3 xn_yn_zn { m_transform.translation.vec() - static_cast< glm::vec3 >( m_transform.scale ) }; - const auto xn { xn_yn_zn.x }; - const auto yn { xn_yn_zn.y }; - const auto zn { xn_yn_zn.z }; + constexpr glm::vec3 xp_yp_zp { 1.0f, 1.0f, 1.0f }; + constexpr glm::vec3 xn_yn_zn { -xp_yp_zp }; - const auto xp { xp_yp_zp.x }; - const auto yp { xp_yp_zp.y }; - const auto zp { xp_yp_zp.z }; + constexpr auto xn { xn_yn_zn.x }; + constexpr auto yn { xn_yn_zn.y }; + constexpr auto zn { xn_yn_zn.z }; + + constexpr auto xp { xp_yp_zp.x }; + constexpr auto yp { xp_yp_zp.y }; + constexpr auto zp { xp_yp_zp.z }; //Top - const glm::vec3 xn_yp_zp { xn, yp, zp }; // (- + +) - const glm::vec3 xn_yp_zn { xn, yp, zn }; // (- + -) - const glm::vec3 xp_yp_zn { xp, yp, zn }; // (+ + -) + constexpr glm::vec3 xn_yp_zp { xn, yp, zp }; // (- + +) + constexpr glm::vec3 xn_yp_zn { xn, yp, zn }; // (- + -) + constexpr glm::vec3 xp_yp_zn { xp, yp, zn }; // (+ + -) /* * Top-Down view (X,Y,Z) @@ -129,15 +130,15 @@ namespace fgl::engine * 6 =========== 7 */ - points[ 0 ] = Coordinate< CType >( xp_yp_zp ); - points[ 1 ] = Coordinate< CType >( xn_yp_zp ); - points[ 2 ] = Coordinate< CType >( xn_yp_zn ); - points[ 3 ] = Coordinate< CType >( xp_yp_zn ); + // points[ 0 ] = Coordinate< CType >( xp_yp_zp ); + // points[ 1 ] = Coordinate< CType >( xn_yp_zp ); + // points[ 2 ] = Coordinate< CType >( xn_yp_zn ); + // points[ 3 ] = Coordinate< CType >( xp_yp_zn ); //Bottom - const glm::vec3 xn_yn_zp { xn, yn, zp }; // (- - +) - const glm::vec3 xp_yn_zn { xp, yn, zn }; // (+ - -) - const glm::vec3 xp_yn_zp { xp, yn, zp }; // (+ - +) + constexpr glm::vec3 xn_yn_zp { xn, yn, zp }; // (- - +) + constexpr glm::vec3 xp_yn_zn { xp, yn, zn }; // (+ - -) + constexpr glm::vec3 xp_yn_zp { xp, yn, zp }; // (+ - +) /* * Bottom-Top view (X,Y,Z) @@ -147,17 +148,28 @@ namespace fgl::engine * (-,-,-) =========== (+,-,-) */ - points[ 4 ] = Coordinate< CType >( xp_yn_zp ); - points[ 5 ] = Coordinate< CType >( xn_yn_zp ); - points[ 6 ] = Coordinate< CType >( xn_yn_zn ); - points[ 7 ] = Coordinate< CType >( xp_yn_zn ); + // points[ 4 ] = Coordinate< CType >( xp_yn_zp ); + // points[ 5 ] = Coordinate< CType >( xn_yn_zp ); + // points[ 6 ] = Coordinate< CType >( xn_yn_zn ); + // points[ 7 ] = Coordinate< CType >( xp_yn_zn ); - for ( auto i = 0; i < points.size(); ++i ) + constexpr static std::array< Coordinate< CType >, POINT_COUNT > const_points { + Coordinate< CType >( xp_yp_zp ), Coordinate< CType >( xn_yp_zp ), Coordinate< CType >( xn_yp_zn ), + Coordinate< CType >( xp_yp_zn ), Coordinate< CType >( xp_yn_zp ), Coordinate< CType >( xn_yn_zp ), + Coordinate< CType >( xn_yn_zn ), Coordinate< CType >( xp_yn_zn ) + }; + + std::array< Coordinate< CType >, POINT_COUNT > points {}; + + static_assert( const_points.size() == points.size() ); + + for ( auto i = 0; i < const_points.size(); ++i ) { - const glm::mat3 rot_mat { m_transform.rotation.mat() }; - const glm::vec3 point { points[ i ].vec() }; + const glm::mat4 rot_mat { m_transform.mat4() }; + const glm::vec3 point { const_points[ i ].vec() }; - points[ i ] = Coordinate< CType >( rot_mat * point ); + points[ i ] = Coordinate< CType >( rot_mat * glm::vec4( point, 1.0f ) ); + FGL_ASSUME( i < POINT_COUNT ); } return points; diff --git a/src/engine/primitives/boxes/OrientedBoundingBox.hpp b/src/engine/primitives/boxes/OrientedBoundingBox.hpp index 654d1a5..8c2b1fe 100644 --- a/src/engine/primitives/boxes/OrientedBoundingBox.hpp +++ b/src/engine/primitives/boxes/OrientedBoundingBox.hpp @@ -81,27 +81,9 @@ namespace fgl::engine assert( bounding_box.m_transform.translation.vec() != constants::DEFAULT_VEC3 ); assert( bounding_box.m_transform.scale != glm::vec3( 0.0f ) ); - // Here's to hoping anything I don't use here (skew, perspecitve) doesn't cost any extra performance - // Compiler optimizer plz - [[maybe_unused]] glm::vec3 scale {}, translation {}, skew {}; - glm::quat quat; - [[maybe_unused]] glm::vec4 perspective {}; - glm::decompose( matrix, scale, quat, translation, skew, perspective ); + const auto new_transform { matrix * bounding_box.m_transform }; - glm::mat4 mat { 1.0f }; - mat = glm::scale( mat, scale ); - mat = glm::translate( mat, translation ); - - const Coordinate< EvolvedType< MType >() > new_middle { Matrix< MType >( mat ) - * bounding_box.m_transform.translation }; - - const glm::vec3 new_scale { bounding_box.m_transform.scale * scale }; - - const Rotation new_rot { quat * static_cast< glm::quat >( bounding_box.m_transform.rotation ) }; - - TransformComponent< EvolvedType< MType >() > transform { new_middle, new_scale, new_rot }; - - return OrientedBoundingBox< EvolvedType< MType >() >( transform ); + return OrientedBoundingBox< EvolvedType< MType >() >( new_transform ); } OrientedBoundingBox< CoordinateSpace::Model > generateBoundingFromVerts( const std::vector< ModelVertex >& verts ); diff --git a/src/engine/primitives/matricies/Matrix.hpp b/src/engine/primitives/matricies/Matrix.hpp index 0ce1872..e7086d8 100644 --- a/src/engine/primitives/matricies/Matrix.hpp +++ b/src/engine/primitives/matricies/Matrix.hpp @@ -39,17 +39,6 @@ namespace fgl::engine glm::mat3 rotmat() const { return glm::mat3( *this ); } - // When extracting a quaternion from a matrix the matrix ***MUST*** be pure. No scale or translation is able to be in the same matrix - glm::quat quat() const - { - // Here's to hoping the compiler will properly optimize this function to oblivion? - [[maybe_unused]] glm::vec3 scale, translation, skew; - glm::quat quat; - [[maybe_unused]] glm::vec4 perspective; - glm::decompose( *this, scale, quat, translation, skew, perspective ); - return quat; - } - explicit Matrix( const float i_value = 1.0f ) : glm::mat4( i_value ) {} explicit Matrix( const glm::mat4& matrix ) : glm::mat4( matrix ) {} diff --git a/src/engine/primitives/points/Coordinate.hpp b/src/engine/primitives/points/Coordinate.hpp index 42e7e61..c8b9a19 100644 --- a/src/engine/primitives/points/Coordinate.hpp +++ b/src/engine/primitives/points/Coordinate.hpp @@ -28,17 +28,18 @@ namespace fgl::engine using glm::vec3::y; using glm::vec3::z; - Coordinate( const Coordinate& other ) noexcept = default; - Coordinate( Coordinate&& other ) = default; + constexpr Coordinate( const Coordinate& other ) noexcept = default; + constexpr Coordinate( Coordinate&& other ) = default; - Coordinate() noexcept : glm::vec3( constants::DEFAULT_VEC3 ) {} + constexpr Coordinate() noexcept : glm::vec3( constants::DEFAULT_VEC3 ) {} - explicit Coordinate( const glm::vec3 position ) noexcept : glm::vec3( position ) {} + constexpr explicit Coordinate( const glm::vec3 position ) noexcept : glm::vec3( position ) {} - explicit Coordinate( const float i_x, const float i_y, const float i_z ) noexcept : glm::vec3( i_x, i_y, i_z ) + constexpr explicit Coordinate( const float i_x, const float i_y, const float i_z ) noexcept : + glm::vec3( i_x, i_y, i_z ) {} - explicit Coordinate( const float value ) : glm::vec3( value ) {} + constexpr explicit Coordinate( const float value ) : glm::vec3( value ) {} explicit Coordinate( const Vector& vector ); diff --git a/src/engine/systems/DrawPair.cpp b/src/engine/systems/DrawPair.cpp index 107ec28..d7e342e 100644 --- a/src/engine/systems/DrawPair.cpp +++ b/src/engine/systems/DrawPair.cpp @@ -8,9 +8,9 @@ #include +#include "engine/assets/model/Model.hpp" #include "engine/debug/drawers.hpp" #include "engine/gameobjects/components/ModelComponent.hpp" -#include "engine/assets/model/Model.hpp" #include "engine/tree/octtree/OctTreeNode.hpp" namespace fgl::engine