From 6615f0c2b8501ff41812f1db3c524494086c324f Mon Sep 17 00:00:00 2001 From: Xiao Qi Date: Mon, 27 Feb 2023 00:56:41 +0800 Subject: [PATCH] Animation WIP --- src/Libraries/MeshCompiler.h | 5 ++- src/Libraries/MeshCompiler.hpp | 65 ++++++++++++++++++++++------------ src/PseudoMath.h | 3 ++ src/Types/MeshAsset.h | 2 +- 4 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index 64d27f6..bc12609 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -45,7 +45,10 @@ namespace SH_COMP static BufferData buffer; static inline void LoadFromFile(AssetPath path, ModelRef asset) noexcept; - static inline void ProcessModel(ModelData const&, ModelRef asset) noexcept; + + static inline void ProcessModel(ModelData const& model, ModelRef asset) noexcept; + static inline void ProcessAnimations(ModelData const& model, ModelRef asset) noexcept; + static inline void BuildHeaders(ModelRef asset) noexcept; template diff --git a/src/Libraries/MeshCompiler.hpp b/src/Libraries/MeshCompiler.hpp index e94fb89..5d05033 100644 --- a/src/Libraries/MeshCompiler.hpp +++ b/src/Libraries/MeshCompiler.hpp @@ -36,7 +36,7 @@ namespace SH_COMP BufferViewReference MeshCompiler::bufferViews{ nullptr }; BufferData MeshCompiler::buffer{ nullptr }; - void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept + inline void MeshCompiler::LoadFromFile(AssetPath path, ModelRef asset) noexcept { ModelData model; tinygltf::TinyGLTF loader; @@ -57,9 +57,10 @@ namespace SH_COMP } ProcessModel(model, asset); + ProcessAnimations(model, asset); } - void MeshCompiler::ProcessModel(ModelData const& data, ModelRef asset) noexcept + inline void MeshCompiler::ProcessModel(ModelData const& data, ModelRef asset) noexcept { #if 0 for (auto i {0}; i < 2; ++i) @@ -86,22 +87,18 @@ namespace SH_COMP 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); - - std::vector indices_ushort; - FetchData(primitive.indices, indices_ushort); - meshIn.indices.resize(indices_ushort.size()); - std::ranges::copy(indices_ushort, meshIn.indices.begin()); + FetchData(primitive.indices, meshIn.indices); 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 }; - } + meshIn.vertexTangent.begin(), + [](auto const& inTan) + { + return SHVec3{ inTan.x, inTan.y, inTan.z }; + } ); } catch (std::out_of_range e) @@ -120,9 +117,10 @@ namespace SH_COMP auto const& view = (*bufferViews)[accessor.bufferView]; auto const typeIdentifier{ static_cast(accessor.componentType) }; auto const sizeIdentifier{ SizeOfType(typeIdentifier) }; - auto const componentCount{ CountOfType(accessor.type) }; + auto const componentCount{ CountOfType(static_cast(accessor.type))}; + auto const totalStrideBytes{ sizeIdentifier * componentCount }; dst.resize(accessor.count); - if (sizeof(T) == sizeIdentifier * componentCount) + if (sizeof(T) == totalStrideBytes) { std::memcpy( dst.data(), @@ -140,18 +138,27 @@ namespace SH_COMP ); auto srcPtr{ tempData.data() }; - auto dstPtr{ dst.data() }; + T* dstPtr{ dst.data() }; size_t index{ 0 }; for (auto i{0}; i < accessor.count; ++i, ++index) { - std::memcpy( - dstPtr, - srcPtr, - sizeIdentifier - ); + auto srcCompPtr{ srcPtr }; + auto dstCompPtr{ reinterpret_cast(dstPtr)}; + for (auto j{0}; j < componentCount; ++j) + { + std::memcpy( + dstPtr, + srcPtr, + sizeIdentifier + ); - srcPtr += sizeIdentifier; - dstPtr += sizeof(T); + srcCompPtr += sizeIdentifier; + ++dstCompPtr; + } + + srcPtr += totalStrideBytes; + + ++dstPtr; } } @@ -182,4 +189,18 @@ namespace SH_COMP delete asset; } + + inline void MeshCompiler::ProcessAnimations(ModelData const& model, ModelRef asset) noexcept + { + asset.anims.resize(model.animations.size()); + for (auto i {0}; i < model.animations.size(); ++i) + { + auto& animData{ model.animations[i] }; + auto& anim{ asset.anims[i] }; + + anim.name = animData.name; + + + } + } } diff --git a/src/PseudoMath.h b/src/PseudoMath.h index 2a23aba..d3adc22 100644 --- a/src/PseudoMath.h +++ b/src/PseudoMath.h @@ -35,4 +35,7 @@ namespace SH_COMP return true; } }; + + using IndexType = uint32_t; + } \ No newline at end of file diff --git a/src/Types/MeshAsset.h b/src/Types/MeshAsset.h index 4020720..83735b4 100644 --- a/src/Types/MeshAsset.h +++ b/src/Types/MeshAsset.h @@ -41,7 +41,7 @@ namespace SH_COMP std::vector vertexTangent; std::vector vertexNormal; std::vector texCoords; - std::vector indices; + std::vector indices; std::vector bonesInfo; std::vector bones; };