From c4cea569e1ddfa1e1e1d3f79fcfd2bf1fd20e2f9 Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Tue, 7 Mar 2023 22:59:50 +0800 Subject: [PATCH] Implemented proper error output when compiling files --- src/Libraries/MeshCompiler.h | 5 +-- src/Libraries/MeshCompiler.hpp | 70 ++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index 94b110c..96578fd 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -39,14 +39,13 @@ namespace SH_COMP class MeshCompiler { - static std::string filename; static AccessorReference accessors; static BufferViewReference bufferViews; static BufferData buffer; - static inline void LoadFromFile(AssetPath path, ModelRef asset) noexcept; + static inline bool LoadFromFile(AssetPath path, ModelRef asset) noexcept; - static inline void ProcessMesh(ModelData const& data, ModelRef asset) noexcept; + static inline bool ProcessMesh(ModelData const& data, ModelRef asset) noexcept; static inline void ProcessAnimationChannels(ModelData const& data, ModelRef asset) noexcept; static inline void ProcessRigNodes(ModelData const& data, ModelRef asset) noexcept; diff --git a/src/Libraries/MeshCompiler.hpp b/src/Libraries/MeshCompiler.hpp index fa1de3b..75bde6c 100644 --- a/src/Libraries/MeshCompiler.hpp +++ b/src/Libraries/MeshCompiler.hpp @@ -25,12 +25,11 @@ namespace SH_COMP { - std::string MeshCompiler::filename{""}; AccessorReference MeshCompiler::accessors{ nullptr }; BufferViewReference MeshCompiler::bufferViews{ nullptr }; BufferData MeshCompiler::buffer{ nullptr }; - inline void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept + inline bool MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept { ModelData model; tinygltf::TinyGLTF loader; @@ -39,27 +38,37 @@ namespace SH_COMP bool result = loader.LoadASCIIFromFile(&model, &error, &warn, path.string()); if (!warn.empty()) - std::cout << "[TinyGLTF Warning] " << filename << " : " << warn << std::endl; + std::cout << "[TinyGLTF Warning] " << warn; if (!error.empty()) - std::cout << "[TinyGLTF Error] " << filename << " : " << error << std::endl; + std::cout << "[TinyGLTF Error] " << error; if (!result) { - std::cout << "TinyGLTF failed to parse: " << filename << "\n"; - std::exit(1); + std::cout << "[TinyGLTF] Failed to parse\n"; + return false; } - ProcessMesh(model, asset); - ProcessRigNodes(model, asset); - ProcessAnimationChannels(model, asset); + if (ProcessMesh(model, asset)) + { + auto const hasAnims {!model.animations.empty()}; + if (hasAnims) + { + ProcessRigNodes(model, asset); + ProcessAnimationChannels(model, asset); + } + return true; + } + + return false; } - inline void MeshCompiler::ProcessMesh(ModelData const& data, ModelRef asset) noexcept + inline bool MeshCompiler::ProcessMesh(ModelData const& data, ModelRef asset) noexcept { accessors = &data.accessors; bufferViews = &data.bufferViews; buffer = data.buffers[0].data.data(); + auto const hasAnims {!data.animations.empty()}; for (auto const& mesh : data.meshes) { @@ -88,19 +97,25 @@ namespace SH_COMP } catch (std::out_of_range e) { - std::cout << "[Model Compiler] Failed to load critical data from gltf: " << filename << "\n"; + std::cout << "[Model Compiler] Failed to load critical data from gltf\n"; + return false; } - try + if (hasAnims) { - FetchData(primitive.attributes.at(ATT_WEIGHTS.data()), meshIn.weights); - FetchData(primitive.attributes.at(ATT_JOINT.data()), meshIn.joints); - } - catch(std::out_of_range e) - { - std::cout << "[Model Compiler] " << filename << ": No weights and joints found for mesh: " << mesh.name << std::endl; + try + { + FetchData(primitive.attributes.at(ATT_WEIGHTS.data()), meshIn.weights); + FetchData(primitive.attributes.at(ATT_JOINT.data()), meshIn.joints); + } + catch(std::out_of_range e) + { + std::cout << "[Model Compiler] No weights and joints found for mesh: " << mesh.name << std::endl; + } } } + + return true; } template @@ -213,11 +228,16 @@ namespace SH_COMP { auto const asset = new ModelAsset(); - filename = path.filename().string(); - - LoadFromFile(path, *asset); - BuildHeaders(*asset); - MeshWriter::CompileMeshBinary(path, *asset); + if (LoadFromFile(path, *asset)) + { + BuildHeaders(*asset); + MeshWriter::CompileMeshBinary(path, *asset); + std::cout << "[Model Compiler] Compiled file: " << path << "\n\n"; + } + else + { + std::cout << "[Model Compiler] Failed to compile file: " << path << "\n\n"; + } delete asset; } @@ -226,7 +246,7 @@ namespace SH_COMP { if (data.animations.empty()) { - std::cout << "[Model Compiler] " << filename << ": Animations do not exist\n"; + std::cout << "[Model Compiler] Animations do not exist\n"; return; } @@ -270,7 +290,7 @@ namespace SH_COMP { if (data.skins.empty()) { - std::cout << "[Model Compiler] " << filename << ": Unable to load rigs without skin, aborting"; + std::cout << "[Model Compiler] Skins not found for asset\n"; return; }