From 73636d8b982201b0814a95976292cebfd1c4fbcc Mon Sep 17 00:00:00 2001 From: kj16609 Date: Mon, 24 Jun 2024 17:37:37 -0400 Subject: [PATCH] Add drag and drop for scene type models --- src/engine/filesystem/FileBrowser.cpp | 27 ++++++++++++------ src/engine/filesystem/FileBrowser.hpp | 2 +- src/engine/filesystem/scanner/FileScanner.cpp | 4 +-- src/engine/filesystem/scanner/FileScanner.hpp | 6 ++-- src/engine/gui/preview.cpp | 22 +++++++++++++++ src/engine/model/Model.cpp | 10 +++++-- src/engine/model/Model.hpp | 14 +++++++--- src/engine/model/builders/SceneBuilder.cpp | 28 +++++++++++++++---- 8 files changed, 85 insertions(+), 28 deletions(-) diff --git a/src/engine/filesystem/FileBrowser.cpp b/src/engine/filesystem/FileBrowser.cpp index 11a7e53..83c7f18 100644 --- a/src/engine/filesystem/FileBrowser.cpp +++ b/src/engine/filesystem/FileBrowser.cpp @@ -17,7 +17,9 @@ namespace fgl::engine::filesystem //! Textures for files (pre-rendered image, images, ect) inline static std::unordered_map< std::filesystem::path, Texture > file_textures {}; - inline static std::optional< DirInfo > current { std::nullopt }; + + inline static std::unique_ptr< DirInfo > current { nullptr }; + inline static std::once_flag flag {}; inline static std::shared_ptr< Texture > folder_texture { nullptr }; inline static std::shared_ptr< Texture > file_texture { nullptr }; @@ -34,7 +36,7 @@ namespace fgl::engine::filesystem file_texture = getTextureStore().load( "./assets/file.png", vk::Format::eR8G8B8A8Unorm ); up_texture = getTextureStore().load( "./assets/up.png", vk::Format::eR8G8B8A8Unorm ); - current = DirInfo( test_path ); + current = std::make_unique< DirInfo >( test_path ); } void FileBrowser::drawGui( FrameInfo& info ) @@ -60,9 +62,10 @@ namespace fgl::engine::filesystem } */ - if ( !current.has_value() ) + if ( !current ) { - log::warn( "Current has no value!" ); + log::critical( "Current has no value!" ); + std::abort(); } auto size { ImGui::GetWindowSize() }; @@ -91,9 +94,11 @@ namespace fgl::engine::filesystem if ( current->hasParent() ) { ImGui::TableNextColumn(); - drawUp( *current ); + drawUp( current ); } + assert( current ); + //List folders first for ( std::size_t i = 0; i < current->folderCount(); ++i ) { @@ -172,6 +177,8 @@ namespace fgl::engine::filesystem if ( folder_texture->drawImGuiButton( { desired_size, desired_size } ) ) { openFolder( data ); + ImGui::PopID(); + return; } ImGui::Text( data.path.filename().c_str() ); @@ -187,16 +194,18 @@ namespace fgl::engine::filesystem void FileBrowser::openFolder( DirInfo dir ) { - current = dir; + current = std::make_unique< DirInfo >( dir.path ); } - void FileBrowser::drawUp( const DirInfo& data ) + void FileBrowser::drawUp( const std::unique_ptr< DirInfo >& data ) { - ImGui::PushID( data.path.c_str() ); + auto up { data->up() }; + + ImGui::PushID( up->path.c_str() ); if ( up_texture->drawImGuiButton( { desired_size, desired_size } ) ) { - openFolder( data.up() ); + openFolder( *up ); } ImGui::Text( "Go Up" ); diff --git a/src/engine/filesystem/FileBrowser.hpp b/src/engine/filesystem/FileBrowser.hpp index c384628..d9a58df 100644 --- a/src/engine/filesystem/FileBrowser.hpp +++ b/src/engine/filesystem/FileBrowser.hpp @@ -22,7 +22,7 @@ namespace fgl::engine::filesystem static void openFolder( DirInfo dir ); - static void drawUp( const DirInfo& up ); + static void drawUp( const std::unique_ptr< DirInfo >& up ); static void drawGui( FrameInfo& info ); static void drawFile( const FileInfo& data ); static void drawFolder( const DirInfo& data ); diff --git a/src/engine/filesystem/scanner/FileScanner.cpp b/src/engine/filesystem/scanner/FileScanner.cpp index 7dfc16b..8112835 100644 --- a/src/engine/filesystem/scanner/FileScanner.cpp +++ b/src/engine/filesystem/scanner/FileScanner.cpp @@ -40,9 +40,9 @@ namespace fgl::engine::filesystem //TODO: Never trust file extensions static const std::unordered_map< EngineFileType, std::vector< std::string_view > > extension_map { - { EngineFileType::MODEL, { ".obj", ".gltf" } }, + { EngineFileType::MODEL, { ".obj" } }, { EngineFileType::TEXTURE, { ".png", ".jpg" } }, - { EngineFileType::SCENE, { ".glb" } } + { EngineFileType::SCENE, { ".glb", ".gltf" } } }; for ( const auto& [ type, strings ] : extension_map ) diff --git a/src/engine/filesystem/scanner/FileScanner.hpp b/src/engine/filesystem/scanner/FileScanner.hpp index 477d31d..74f749e 100644 --- a/src/engine/filesystem/scanner/FileScanner.hpp +++ b/src/engine/filesystem/scanner/FileScanner.hpp @@ -5,7 +5,6 @@ #pragma once #include -#include #include #include #include @@ -25,10 +24,11 @@ namespace fgl::engine::filesystem public: - inline DirInfo up() const + inline std::unique_ptr< DirInfo > up() const { assert( std::filesystem::exists( path ) ); - return DirInfo( path.parent_path() ); + + return std::make_unique< DirInfo >( path.parent_path() ); } std::size_t fileCount() const; diff --git a/src/engine/gui/preview.cpp b/src/engine/gui/preview.cpp index 96f1d37..b036b61 100644 --- a/src/engine/gui/preview.cpp +++ b/src/engine/gui/preview.cpp @@ -17,6 +17,7 @@ #include "engine/FrameInfo.hpp" #include "engine/filesystem/scanner/FileScanner.hpp" #include "engine/filesystem/types.hpp" +#include "engine/model/builders/SceneBuilder.hpp" #include "engine/rendering/SwapChain.hpp" namespace fgl::engine::gui @@ -62,6 +63,27 @@ namespace fgl::engine::gui } case filesystem::SCENE: { + SceneBuilder builder { info.model_vertex_buffer, info.model_index_buffer }; + + builder.loadScene( data->path ); + + std::vector< GameObject > objs {}; + auto models { builder.getModels() }; + objs.reserve( models.size() ); + for ( auto& model : models ) + { + GameObject obj { GameObject::createGameObject() }; + + obj.getModel() = std::move( model ); + obj.addFlag( IS_ENTITY | IS_VISIBLE ); + + objs.emplace_back( std::move( obj ) ); + } + + for ( auto& obj : objs ) + { + info.game_objects.addGameObject( std::move( obj ) ); + } } } } diff --git a/src/engine/model/Model.cpp b/src/engine/model/Model.cpp index 309d100..ea60024 100644 --- a/src/engine/model/Model.cpp +++ b/src/engine/model/Model.cpp @@ -68,13 +68,17 @@ namespace fgl::engine return draw_commands; } - Model::Model( ModelBuilder& builder, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ) : - Model( std::move( builder.m_primitives ), bounding_box ) + Model::Model( + ModelBuilder& builder, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box, std::string name ) : + Model( std::move( builder.m_primitives ), bounding_box, name ) {} Model::Model( - std::vector< Primitive >&& primitives, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ) : + std::vector< Primitive >&& primitives, + const OrientedBoundingBox< CoordinateSpace::Model > bounding_box, + std::string name ) : m_draw_parameters( buildParameters( primitives ) ), + m_name( name ), m_bounding_box( bounding_box ) { assert( bounding_box.middle.vec() != constants::DEFAULT_VEC3 ); diff --git a/src/engine/model/Model.hpp b/src/engine/model/Model.hpp index 0344e2e..e2aeb8a 100644 --- a/src/engine/model/Model.hpp +++ b/src/engine/model/Model.hpp @@ -12,6 +12,7 @@ #include "Primitive.hpp" #include "engine/buffers/Buffer.hpp" +#include "engine/primitives/TransformComponent.hpp" #include "engine/primitives/boxes/OrientedBoundingBox.hpp" #include "engine/rendering/Device.hpp" @@ -32,6 +33,8 @@ namespace fgl::engine static OrientedBoundingBox< CoordinateSpace::Model > buildBoundingBox( const std::vector< Primitive >& primitives ); + TransformComponent m_model_transform; + std::vector< vk::DrawIndexedIndirectCommand > m_draw_parameters; std::string m_name { "Unnamed model" }; @@ -65,12 +68,15 @@ namespace fgl::engine const std::string& getName() const { return m_name; } - void setName( std::string str ) { m_name = str; } - - Model( ModelBuilder& builder, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ); + Model( + ModelBuilder& builder, + const OrientedBoundingBox< CoordinateSpace::Model > bounding_box, + std::string name = {} ); Model( - std::vector< Primitive >&& primitives, const OrientedBoundingBox< CoordinateSpace::Model > bounding_box ); + std::vector< Primitive >&& primitives, + const OrientedBoundingBox< CoordinateSpace::Model > bounding_box, + std::string name = {} ); ~Model() = default; diff --git a/src/engine/model/builders/SceneBuilder.cpp b/src/engine/model/builders/SceneBuilder.cpp index d27c7ff..9c38f41 100644 --- a/src/engine/model/builders/SceneBuilder.cpp +++ b/src/engine/model/builders/SceneBuilder.cpp @@ -329,7 +329,7 @@ namespace fgl::engine const auto bounding_box { createModelBoundingBox( finished_primitives ) }; - return std::make_shared< Model >( std::move( finished_primitives ), bounding_box ); + return std::make_shared< Model >( std::move( finished_primitives ), bounding_box, mesh.name ); } void SceneBuilder::handleNode( const int node_idx, const tinygltf::Model& root ) @@ -345,7 +345,6 @@ namespace fgl::engine log::debug( "Skin IDX: {}", skin_idx ); std::shared_ptr< Model > model { loadModel( mesh_idx, root ) }; - model->setName( node.name ); assert( model ); @@ -373,12 +372,29 @@ namespace fgl::engine tinygltf::TinyGLTF loader {}; tinygltf::Model gltf_model {}; - std::string err; - std::string warn; + std::string err {}; + std::string warn {}; - if ( !loader.LoadBinaryFromFile( &gltf_model, &err, &warn, path ) ) + if ( path.extension() == ".gltf" ) { - throw std::runtime_error( "Failed to load binary model" ); + //Must use the ASCII loader + if ( !loader.LoadASCIIFromFile( &gltf_model, &err, &warn, path ) ) + { + log::info( "Failed to scene at {}", path ); + if ( !warn.empty() ) log::warn( warn ); + if ( !err.empty() ) log::error( err ); + throw std::runtime_error( "Failed to load binary model" ); + } + } + else + { + if ( !loader.LoadBinaryFromFile( &gltf_model, &err, &warn, path ) ) + { + log::info( "Failed to scene at {}", path ); + if ( !warn.empty() ) log::warn( warn ); + if ( !err.empty() ) log::error( err ); + throw std::runtime_error( "Failed to load binary model" ); + } } if ( !err.empty() )