diff --git a/racoon.gltf b/racoon_tiny.gltf similarity index 100% rename from racoon.gltf rename to racoon_tiny.gltf diff --git a/src/AssetMacros.h b/src/AssetMacros.h index 550c884..64154b6 100644 --- a/src/AssetMacros.h +++ b/src/AssetMacros.h @@ -16,7 +16,7 @@ typedef std::filesystem::path AssetPath; enum class BUFFER_TARGET : int { - ARRAY_BUFFER = 3496, + ARRAY_BUFFER = 34962, ELEMENT_ARRAY_BUFFER = 34963 }; diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index a0f6519..49fd876 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -48,6 +48,8 @@ namespace SH_COMP static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept; static inline void BuildHeaders(ModelRef asset) noexcept; + template + static void FetchData(int accessorID, std::vector& dst); public: static void LoadAndCompile(AssetPath path) noexcept; }; diff --git a/src/Libraries/MeshCompiler.cpp b/src/Libraries/MeshCompiler.hpp similarity index 62% rename from src/Libraries/MeshCompiler.cpp rename to src/Libraries/MeshCompiler.hpp index e51776d..d659d1a 100644 --- a/src/Libraries/MeshCompiler.cpp +++ b/src/Libraries/MeshCompiler.hpp @@ -24,6 +24,7 @@ #include #include +#include #include "tiny_gltf.h" #include @@ -82,54 +83,25 @@ namespace SH_COMP try { - //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())); + FetchData(primitive.attributes.at(ATT_POSITION.data()), meshIn.vertexPosition); + FetchData(primitive.attributes.at(ATT_NORMAL.data()), meshIn.vertexNormal); + FetchData(primitive.attributes.at(ATT_TEXCOORD.data()), meshIn.texCoords); - auto accessor = &(*accessors)[primitive.attributes.at(ATT_POSITION.data())]; - auto view = &(*bufferViews)[accessor->bufferView]; - meshIn.vertexPosition.resize(accessor->count); - std::memcpy( - meshIn.vertexPosition.data(), - buffer + view->byteOffset, - view->byteLength - ); + std::vector indices_ushort; + FetchData(primitive.indices, indices_ushort); + meshIn.indices.resize(indices_ushort.size()); + std::ranges::copy(indices_ushort, meshIn.indices.begin()); - 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 + std::vector intermediate; + FetchData(primitive.attributes.at(ATT_TANGENT.data()), intermediate); + meshIn.vertexTangent.resize(intermediate.size()); + std::ranges::transform( + intermediate, + meshIn.vertexTangent.begin(), + [](auto const& inTan) + { + return SHVec3{ inTan.x, inTan.y, inTan.z }; + } ); } catch (std::out_of_range e) @@ -141,6 +113,20 @@ namespace SH_COMP } + template + void MeshCompiler::FetchData(int accessorID, std::vector& dst) + { + auto const& accessor = (*accessors)[accessorID]; + auto const& view = (*bufferViews)[accessor.bufferView]; + dst.resize(3484); + std::cout << "buffer line\n"; + std::memcpy( + dst.data(), + buffer + view.byteOffset, + view.byteLength + ); + } + inline void MeshCompiler::BuildHeaders(ModelRef asset) noexcept { // Mesh Headers diff --git a/src/main.cpp b/src/main.cpp index 0b90be0..62a9c00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) } #else - SH_COMP::MeshCompiler::LoadAndCompile("racoon.gltf"); + SH_COMP::MeshCompiler::LoadAndCompile("racoon_tiny.gltf"); #endif return 0;