diff --git a/src/Libraries/MeshCompiler.cpp b/src/Libraries/MeshCompiler.cpp index b3fdaab..c89a5a3 100644 --- a/src/Libraries/MeshCompiler.cpp +++ b/src/Libraries/MeshCompiler.cpp @@ -22,8 +22,9 @@ namespace SH_COMP { Assimp::Importer MeshCompiler::aiImporter; + uint32_t MeshCompiler::rigNodeIDCounter { 0 }; - void MeshCompiler::ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigNode*& root) noexcept + void MeshCompiler::ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigData& rig) noexcept { for (size_t i{ 0 }; i < node.mNumMeshes; ++i) { @@ -35,38 +36,17 @@ namespace SH_COMP if (std::strcmp(node.mName.C_Str(), "Armature") == 0) { - BuildArmature(node, root); + BuildArmature(node, rig.root); } else { for (size_t i{ 0 }; i < node.mNumChildren; ++i) { - ProcessNode(*node.mChildren[i], scene, meshes, root); + ProcessNode(*node.mChildren[i], scene, meshes, rig); } } } - //void MeshCompiler::ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept - //{ - // if (scene.HasAnimations()) - // { - // std::vector anims(scene.mNumAnimations); - // for (auto i{ 0 }; i < scene.mNumAnimations; ++i) - // { - // auto const& anim{ *scene.mAnimations[i] }; - - // anims[i].name = anim.mName.C_Str(); - - // anims[i].duration = anim.mDuration; - // anims[i].ticksPerSecond = anim.mTicksPerSecond; - - // std::copy_n(anim.mChannels, anim.mNumChannels, anims[i].nodeChannels.data()); - // std::copy_n(anim.mMeshChannels, anim.mNumMeshChannels, anims[i].meshChannels.data()); - // std::copy_n(anim.mMorphMeshChannels, anim.mNumMorphMeshChannels, anims[i].morphMeshChannels.data()); - // } - // } - //} - void MeshCompiler::GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept { meshData.vertexPosition.reserve(mesh.mNumVertices); @@ -335,7 +315,6 @@ namespace SH_COMP void MeshCompiler::ParseAnimations(aiScene const& scene, std::vector& anims) noexcept { - // Size and read for number of animation clips anims.resize(scene.mNumAnimations); for (auto i {0}; i < scene.mNumAnimations; ++i) @@ -421,7 +400,7 @@ namespace SH_COMP ParseAnimations(*scene, asset.anims); - ProcessNode(*scene->mRootNode, *scene, asset.meshes, asset.rig.root); + ProcessNode(*scene->mRootNode, *scene, asset.meshes, asset.rig); aiImporter.FreeScene(); } @@ -449,29 +428,16 @@ namespace SH_COMP file.close(); } - void MeshCompiler::BuildArmature(aiNode const& baseNode, RigNode*& root) noexcept + void MeshCompiler::BuildArmature(aiNode const& baseNode, RigData& rig) noexcept { - RigNode* start = new RigNode(); + std::queue nodesQueue; + nodesQueue.push(&baseNode); - CopyNode(baseNode, start); + RigNode* parent = nullptr; - root = start->children[0]; - } - - void MeshCompiler::CopyNode(aiNode const& source, RigNode* parent) noexcept - { - RigNode* current = new RigNode(); - current->name = source.mName.C_Str(); - std::memcpy(¤t->transform, &source.mTransformation, sizeof(SHMat4)); - - for (auto i {0}; i < source.mNumChildren; ++i) + while(!nodesQueue.empty()) { - CopyNode(*source.mChildren[i], current); - } - if (parent) - { - parent->children.push_back(current); } } @@ -479,8 +445,6 @@ namespace SH_COMP { auto const asset = new ModelAsset(); - asset->rig.root = nullptr; - LoadFromFile(path, *asset); BuildHeaders(*asset); CompileMeshBinary(path, *asset); diff --git a/src/Libraries/MeshCompiler.h b/src/Libraries/MeshCompiler.h index da9f2b6..e74587c 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -33,9 +33,9 @@ namespace SH_COMP using ModelRef = ModelAsset&; static Assimp::Importer aiImporter; + static uint32_t rigNodeIDCounter; - static void ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigNode*& root) noexcept; - //static void ExtractAnimations(aiScene const& scene, AnimVectorRef anims) noexcept; + static void ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigData& rig) noexcept; static void GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept; static void BuildHeaders(ModelRef asset) noexcept; @@ -52,8 +52,7 @@ namespace SH_COMP static void LoadFromFile(AssetPath path, ModelRef asset) noexcept; static void CompileMeshBinary(AssetPath path, ModelConstRef asset) noexcept; - static void BuildArmature(aiNode const& node, RigNode*& root) noexcept; - static void CopyNode(aiNode const& source, RigNode* parent) noexcept; + static void BuildArmature(aiNode const& node, RigData& rig) noexcept; static void ParseAnimations(aiScene const& scene, std::vector& anims) noexcept; public: diff --git a/src/Types/ModelAsset.h b/src/Types/ModelAsset.h index d0e5dcc..95f2d6a 100644 --- a/src/Types/ModelAsset.h +++ b/src/Types/ModelAsset.h @@ -13,25 +13,13 @@ #pragma once #include -#include #include "MeshAsset.h" #include "AnimationAsset.h" +#include "RigAsset.h" namespace SH_COMP { - struct RigNode - { - std::string name; - uint32_t id; - SHMat4 transform; - std::vector children; - }; - - struct RigData - { - RigNode* root; - }; struct ModelAssetHeader { diff --git a/src/Types/RigAsset.h b/src/Types/RigAsset.h index 6f70f09..43b3673 100644 --- a/src/Types/RigAsset.h +++ b/src/Types/RigAsset.h @@ -1 +1,34 @@ #pragma once + +#include +#include +#include + +#include "PseudoMath.h" + +namespace SH_COMP +{ + struct RigDataHeader + { + uint32_t nodeCount; + std::vector charCounts; + }; + + struct RigNodeData + { + std::string name; + SHMat4 transform; + }; + + struct RigNode + { + uint32_t idRef; + std::vector children; + }; + + struct RigData + { + std::map nodeDataCollection; + RigNode root; + }; +}