Add drag and drop for scene type models

This commit is contained in:
2024-06-24 17:37:37 -04:00
parent 23a2bfa7fa
commit 73636d8b98
8 changed files with 85 additions and 28 deletions

View File

@@ -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" );

View File

@@ -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 );

View File

@@ -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 )

View File

@@ -5,7 +5,6 @@
#pragma once
#include <cassert>
#include <coroutine>
#include <filesystem>
#include <queue>
#include <string>
@@ -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;

View File

@@ -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 ) );
}
}
}
}

View File

@@ -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 );

View File

@@ -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;

View File

@@ -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() )