diff --git a/src/AssetMacros.h b/src/AssetMacros.h index 2123775..550c884 100644 --- a/src/AssetMacros.h +++ b/src/AssetMacros.h @@ -71,13 +71,16 @@ constexpr std::string_view FBX_EXTENSION{ ".fbx" }; constexpr std::string_view GLTF_EXTENSION{ ".gltf" }; // ATTRIBUTE NAMES +// BASIC NEEDED constexpr std::string_view ATT_POSITION {"POSITION"}; constexpr std::string_view ATT_NORMAL { "NORMAL" }; constexpr std::string_view ATT_TANGENT { "TANGENT" }; -constexpr std::string_view ATT_JOINT { "JOINTS_0" }; -constexpr std::string_view ATT_COLOUR { "COLOR_0" }; constexpr std::string_view ATT_TEXCOORD { "TEXCOORD_0" }; + +// VARIABLE ATTRIBUTES constexpr std::string_view ATT_WEIGHTS { "WEIGHTS_0" }; +constexpr std::string_view ATT_JOINT{ "JOINTS_0" }; +constexpr std::string_view ATT_COLOUR{ "COLOR_0" }; constexpr std::string_view EXTERNALS[] = { FBX_EXTENSION, diff --git a/src/Libraries/MeshCompiler.cpp b/src/Libraries/MeshCompiler.cpp index 1707d0a..94a21af 100644 --- a/src/Libraries/MeshCompiler.cpp +++ b/src/Libraries/MeshCompiler.cpp @@ -12,6 +12,15 @@ *****************************************************************************/ +#define TINYGLTF_IMPLEMENTATION +#define TINYGLTF_NO_EXTERNAL_IMAGE +#define TINYGLTF_USE_CPP14 +#define TINYGLTF_NO_INCLUDE_STB_IMAGE +#define TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE +#define TINYGLTF_NO_STB_IMAGE_WRITE +#define TINYGLTF_NO_STB_IMAGE +#define TINYGLTF_USE_CPP14 + #include "MeshCompiler.h" #include "MeshWriter.h" @@ -24,8 +33,9 @@ namespace SH_COMP { - - uint32_t MeshCompiler::rigNodeIDCounter { 0 }; + AccessorReference MeshCompiler::accessors{ nullptr }; + BufferViewReference MeshCompiler::bufferViews{ nullptr }; + BufferData MeshCompiler::buffer{ nullptr }; void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept { @@ -52,9 +62,9 @@ namespace SH_COMP void MeshCompiler::ProcessModel(ModelData const& data, ModelRef asset) noexcept { - auto const& accessors { data.accessors }; - auto const& bufferViews { data.bufferViews }; - auto const& bufferData { data.buffers[0].data.data() }; + accessors = &data.accessors; + bufferViews = &data.bufferViews; + buffer = data.buffers[0].data.data(); for (auto const& mesh : data.meshes) { @@ -64,33 +74,86 @@ namespace SH_COMP try { - // Get Accessors - auto const& positionAccessor { accessors[primitive.attributes.at(ATT_POSITION.data())]}; - auto const& normalAccessor { accessors[primitive.attributes.at(ATT_NORMAL.data())]}; - auto const& tangentAccessor { accessors[primitive.attributes.at(ATT_TANGENT.data())]}; + //meshIn.vertexPosition = FetchData(primitive.attributes.at(ATT_POSITION.data())); + //meshIn.vertexNormal = FetchData(primitive.attributes.at(ATT_NORMAL.data())); + //meshIn.vertexTangent = FetchData(primitive.attributes.at(ATT_TANGENT.data())); + //meshIn.texCoords = FetchData(primitive.attributes.at(ATT_TEXCOORD.data())); - meshIn.vertexPosition.resize(positionAccessor.count); - auto const& positionView { bufferViews[positionAccessor.bufferView] }; + auto accessor = &(*accessors)[primitive.attributes.at(ATT_POSITION.data())]; + auto view = &(*bufferViews)[accessor->bufferView]; + meshIn.vertexPosition.resize(accessor->count); std::memcpy( - meshIn.vertexPosition.data(), - bufferData + positionView.byteOffset, - positionView.byteLength + meshIn.vertexPosition.data(), + buffer + view->byteOffset, + view->byteLength ); - meshIn.vertexNormal.resize(normalAccessor.count); + accessor = &(*accessors)[primitive.indices]; + view = &(*bufferViews)[accessor->bufferView]; + meshIn.indices.resize(accessor->count); + std::memcpy( + meshIn.indices.data(), + buffer + view->byteOffset, + view->byteLength + ); + + accessor = &(*accessors)[primitive.attributes.at(ATT_NORMAL.data())]; + view = &(*bufferViews)[accessor->bufferView]; + meshIn.vertexNormal.resize(accessor->count); + std::memcpy( + meshIn.vertexNormal.data(), + buffer + view->byteOffset, + view->byteLength + ); + + accessor = &(*accessors)[primitive.attributes.at(ATT_TEXCOORD.data())]; + view = &(*bufferViews)[accessor->bufferView]; + meshIn.texCoords.resize(accessor->count); + std::memcpy( + meshIn.texCoords.data(), + buffer + view->byteOffset, + view->byteLength + ); + + accessor = &(*accessors)[primitive.attributes.at(ATT_TANGENT.data())]; + view = &(*bufferViews)[accessor->bufferView]; + meshIn.vertexTangent.resize(accessor->count); + std::memcpy( + meshIn.vertexTangent.data(), + buffer + view->byteOffset, + view->byteLength + ); } catch (std::out_of_range e) { - std::cout << "[Model Compiler] Failed to load gltf\n"; + std::cout << "[Model Compiler] Failed to load critical data from gltf\n"; } } } + inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept + { + // Mesh Headers + asset.meshHeaders.resize(asset.meshes.size()); + asset.header.meshCount = asset.meshes.size(); + for (auto i{ 0 }; i < asset.header.meshCount; ++i) + { + auto const& mesh = asset.meshes[i]; + auto& head = asset.meshHeaders[i]; + + head.charCount = mesh.name.size(); + head.indexCount = mesh.indices.size(); + head.vertexCount = mesh.vertexPosition.size(); + head.boneCount = mesh.bonesInfo.size(); + } + } + void MeshCompiler::LoadAndCompile(AssetPath path) noexcept { auto const asset = new ModelAsset(); LoadFromFile(path, *asset); + BuildHeaders(*asset); MeshWriter::CompileMeshBinary(path, *asset); delete asset; diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index ad5f97e..a0f6519 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -12,15 +12,6 @@ *****************************************************************************/ #pragma once -#define TINYGLTF_IMPLEMENTATION -#define TINYGLTF_NO_EXTERNAL_IMAGE -#define TINYGLTF_USE_CPP14 -#define TINYGLTF_NO_INCLUDE_STB_IMAGE -#define TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE -#define TINYGLTF_NO_STB_IMAGE_WRITE -#define TINYGLTF_NO_STB_IMAGE -#define TINYGLTF_USE_CPP14 - #include #include "Types/AnimationAsset.h" @@ -30,27 +21,32 @@ //Forward Declare namespace tinygltf { + struct Accessor; + struct BufferView; class Model; } namespace SH_COMP { - class tinygltf::Model; + using MeshVectorRef = std::vector&; + using AnimVectorRef = std::vector&; + + using ModelRef = ModelAsset&; + using ModelData = tinygltf::Model; + using AccessorReference = std::vector const*; + using BufferViewReference = std::vector const*; + using BufferData = unsigned char const*; + class MeshCompiler { - using MeshVectorRef = std::vector&; - using AnimVectorRef = std::vector&; - - using ModelRef = ModelAsset&; - using ModelData = tinygltf::Model; - - static uint32_t rigNodeIDCounter; + static AccessorReference accessors; + static BufferViewReference bufferViews; + static BufferData buffer; static void LoadFromFile(AssetPath path, ModelRef asset) noexcept; - - - static void ProcessModel(ModelData const&, ModelRef asset) noexcept; + static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept; + static inline void BuildHeaders(ModelRef asset) noexcept; public: static void LoadAndCompile(AssetPath path) noexcept;