From 6a0191ff9cbcdc6248f0254e5aa5979387f152ff Mon Sep 17 00:00:00 2001 From: kj16609 Date: Thu, 3 Oct 2024 17:20:27 -0400 Subject: [PATCH] Fixes rotation loading being incorrect in gltf loader (w,x,y,z) -> (x,y,z,w) --- .../assets/model/builders/SceneBuilder.cpp | 75 +++++++++++-------- src/engine/debug/logging/formatters/glm.cpp | 14 ++++ src/engine/debug/logging/formatters/glm.hpp | 17 +++++ src/engine/debug/logging/logging.hpp | 1 + 4 files changed, 76 insertions(+), 31 deletions(-) create mode 100644 src/engine/debug/logging/formatters/glm.cpp create mode 100644 src/engine/debug/logging/formatters/glm.hpp diff --git a/src/engine/assets/model/builders/SceneBuilder.cpp b/src/engine/assets/model/builders/SceneBuilder.cpp index 93929f8..59b136b 100644 --- a/src/engine/assets/model/builders/SceneBuilder.cpp +++ b/src/engine/assets/model/builders/SceneBuilder.cpp @@ -461,17 +461,50 @@ namespace fgl::engine { const auto node { root.nodes[ node_idx ] }; - const glm::vec3 translation { convertToVec3( node.translation ) }; - const glm::quat rotation { static_cast< float >( node.rotation[ 0 ] ), - static_cast< float >( node.rotation[ 1 ] ), - static_cast< float >( node.rotation[ 2 ] ), - static_cast< float >( node.rotation[ 3 ] ) }; - const glm::vec3 scale { convertToVec3( node.scale ) }; + FGL_ASSERT( node.matrix.size() == 0, "No matrix handler in loadTransform" ); WorldTransform transform_component {}; - transform_component.rotation = rotation; - transform_component.scale = scale; - transform_component.translation = WorldCoordinate( translation ); + + if ( node.rotation.size() == 4 ) + { + const auto& x { node.rotation[ 0 ] }; + const auto& y { node.rotation[ 1 ] }; + const auto& z { node.rotation[ 2 ] }; + const auto& w { node.rotation[ 3 ] }; + + // Quat is stored as (x,y,z,w) in gltf + const glm::quat rotation { static_cast< float >( w ), + static_cast< float >( x ), + static_cast< float >( y ), + static_cast< float >( z ) }; + + transform_component.rotation = rotation; + } + else if ( node.rotation.size() != 0 ) + { + log::warn( "Invalid rotation size: {}", node.rotation.size() ); + throw std::runtime_error( "Invalid rotation size" ); + } + + if ( node.scale.size() == 3 ) + { + const glm::vec3 scale { convertToVec3( node.scale ) }; + transform_component.scale = scale; + } + else if ( node.scale.size() != 0 ) + { + log::warn( "Odd scale size: {}", node.scale.size() ); + } + + if ( node.translation.size() == 3 ) + { + const glm::vec3 translation { convertToVec3( node.translation ) }; + transform_component.translation = WorldCoordinate( translation ); + } + else if ( node.translation.size() != 0 ) + { + log::warn( "Odd translation size: {}", node.translation.size() ); + } return transform_component; } @@ -523,29 +556,9 @@ namespace fgl::engine obj.addFlag( IS_VISIBLE | IS_ENTITY ); - //TODO: Set transform from node - const std::vector< double > translation { node.translation }; - const std::vector< double > rotation { node.rotation }; - const std::vector< double > scale { node.scale }; + const auto transform { loadTransform( node_idx, root ) }; - if ( rotation.size() == 4 ) - obj.getTransform().rotation = glm::quat( - static_cast< float >( rotation[ 0 ] ), - static_cast< float >( rotation[ 1 ] ), - static_cast< float >( rotation[ 2 ] ), - static_cast< float >( rotation[ 3 ] ) ); - - if ( scale.size() == 3 ) - obj.getTransform().scale = glm::vec3( - static_cast< float >( scale[ 0 ] ), - static_cast< float >( scale[ 1 ] ), - static_cast< float >( scale[ 2 ] ) ); - - if ( translation.size() == 3 ) - obj.getTransform().translation = WorldCoordinate( - static_cast< float >( translation[ 0 ] ), - static_cast< float >( translation[ 1 ] ), - static_cast< float >( translation[ 2 ] ) ); + obj.getTransform() = transform; if ( node.name.empty() ) obj.setName( "Unnamed Game Object" ); diff --git a/src/engine/debug/logging/formatters/glm.cpp b/src/engine/debug/logging/formatters/glm.cpp new file mode 100644 index 0000000..64b4ee1 --- /dev/null +++ b/src/engine/debug/logging/formatters/glm.cpp @@ -0,0 +1,14 @@ +// +// Created by kj16609 on 10/3/24. +// + +#include "glm.hpp" + +#define GLM_ENABLE_EXPERIMENTAL +#include + +std::format_context::iterator std::formatter< glm::qua< float > >::format( const glm::quat& quat, format_context& ctx ) + const +{ + return format_to( ctx.out(), "quat( w: {:10f}, x: {:10f}, y: {:10f}, z: {:10f} )", quat.w, quat.x, quat.y, quat.z ); +} \ No newline at end of file diff --git a/src/engine/debug/logging/formatters/glm.hpp b/src/engine/debug/logging/formatters/glm.hpp new file mode 100644 index 0000000..6a87534 --- /dev/null +++ b/src/engine/debug/logging/formatters/glm.hpp @@ -0,0 +1,17 @@ +// +// Created by kj16609 on 10/3/24. +// + +#pragma once + +#include + +#include + +template <> +struct std::formatter< glm::quat > +{ + constexpr format_parse_context::iterator parse( format_parse_context& ctx ) { return ctx.begin(); } + + format_context::iterator format( const glm::quat& quat, 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 1c23ea2..b393fab 100644 --- a/src/engine/debug/logging/logging.hpp +++ b/src/engine/debug/logging/logging.hpp @@ -13,6 +13,7 @@ #include "formatters/filesystem.hpp" #include "formatters/matrix.hpp" +#include "formatters/glm.hpp" namespace fgl::engine::log {