diff --git a/racoon.fbx b/racoon.fbx new file mode 100644 index 0000000..4d823d9 Binary files /dev/null and b/racoon.fbx differ diff --git a/src/Libraries/MeshCompiler.cpp b/src/Libraries/MeshCompiler.cpp index 18def83..935d5e0 100644 --- a/src/Libraries/MeshCompiler.cpp +++ b/src/Libraries/MeshCompiler.cpp @@ -16,12 +16,14 @@ #include #include +#include + namespace SH_COMP { Assimp::Importer MeshCompiler::aiImporter; - void MeshCompiler::ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes) noexcept + void MeshCompiler::ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes, RigNode*& root) noexcept { for (size_t i{ 0 }; i < node.mNumMeshes; ++i) { @@ -31,9 +33,16 @@ namespace SH_COMP meshes.back().name = node.mName.C_Str(); } - for (size_t i{ 0 }; i < node.mNumChildren; ++i) + if (std::strcmp(node.mName.C_Str(), "Armature") == 0) { - ProcessNode(*node.mChildren[i], scene, meshes); + BuildArmature(&node, root); + } + else + { + for (size_t i{ 0 }; i < node.mNumChildren; ++i) + { + ProcessNode(*node.mChildren[i], scene, meshes, root); + } } } @@ -65,6 +74,12 @@ namespace SH_COMP meshData.vertexTangent.reserve(mesh.mNumVertices); meshData.texCoords.reserve(mesh.mNumVertices); + for (auto i {0}; i < mesh.mNumBones; ++i) + { + auto& bone = *mesh.mBones[i]; + std::cout << "bone" << std::endl; + } + for (size_t i{ 0 }; i < mesh.mNumVertices; ++i) { // Vertex position @@ -175,16 +190,16 @@ namespace SH_COMP void MeshCompiler::LoadFromFile(AssetPath path, MeshAsset& asset) noexcept { - const aiScene* scene = aiImporter.ReadFile(path.string().c_str(), - aiProcess_Triangulate // Make sure we get triangles rather than nvert polygons - | aiProcess_GenUVCoords // Convert any type of mapping to uv mapping - | aiProcess_TransformUVCoords // preprocess UV transformations (scaling, translation ...) - | aiProcess_FindInstances // search for instanced meshes and remove them by references to one master - | aiProcess_CalcTangentSpace // calculate tangents and bitangents if possible - | aiProcess_JoinIdenticalVertices // join identical vertices/ optimize indexing - | aiProcess_FindInvalidData // detect invalid model data, such as invalid normal vectors - | aiProcess_FlipUVs // flip the V to match the Vulkans way of doing UVs - | aiProcess_ValidateDataStructure + const aiScene* scene = aiImporter.ReadFile(path.string().c_str(),0 + //aiProcess_Triangulate // Make sure we get triangles rather than nvert polygons + //| aiProcess_GenUVCoords // Convert any type of mapping to uv mapping + //| aiProcess_TransformUVCoords // preprocess UV transformations (scaling, translation ...) + //| aiProcess_FindInstances // search for instanced meshes and remove them by references to one master + //| aiProcess_CalcTangentSpace // calculate tangents and bitangents if possible + //| aiProcess_JoinIdenticalVertices // join identical vertices/ optimize indexing + //| aiProcess_FindInvalidData // detect invalid model data, such as invalid normal vectors + //| aiProcess_FlipUVs // flip the V to match the Vulkans way of doing UVs + //| aiProcess_ValidateDataStructure ); if (!scene || !scene->HasMeshes()) @@ -195,7 +210,7 @@ namespace SH_COMP //ExtractAnimations(*scene, anims); - ProcessNode(*scene->mRootNode, *scene, asset.meshes); + ProcessNode(*scene->mRootNode, *scene, asset.meshes, asset.rig.root); aiImporter.FreeScene(); } @@ -226,10 +241,28 @@ namespace SH_COMP file.close(); } + void MeshCompiler::BuildArmature(aiNode const* node, RigNode*& root) noexcept + { + std::queue nodes; + nodes.push(node); + root = new RigNode(); + auto current = root; + + while(!nodes.empty()) + { + auto node = nodes.front(); + nodes.pop(); + + current->name = node->mName.C_Str(); + } + } + void MeshCompiler::LoadAndCompile(AssetPath path) noexcept { auto const asset = new MeshAsset(); + 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 a224898..28eac51 100644 --- a/src/Libraries/MeshCompiler.h +++ b/src/Libraries/MeshCompiler.h @@ -30,7 +30,7 @@ namespace SH_COMP static Assimp::Importer aiImporter; - static void ProcessNode(aiNode const& node, aiScene const& scene, MeshVectorRef meshes) noexcept; + 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 GetMesh(aiMesh const& mesh, MeshData& meshData) noexcept; static void BuildHeaders(MeshAsset& asset) noexcept; @@ -41,6 +41,7 @@ namespace SH_COMP static void LoadFromFile(AssetPath path, MeshAsset& asset) noexcept; static void CompileMeshBinary(AssetPath path, MeshAsset const& asset) noexcept; + static void BuildArmature(aiNode const* node, RigNode*& root) noexcept; public: static void LoadAndCompile(AssetPath path) noexcept; }; diff --git a/src/Types/MeshAsset.h b/src/Types/MeshAsset.h index 827e3b8..adff44a 100644 --- a/src/Types/MeshAsset.h +++ b/src/Types/MeshAsset.h @@ -28,6 +28,11 @@ namespace SH_COMP float x, y, z; }; + struct SHMat4 + { + float data[16]; + }; + struct MeshDataHeader { uint32_t vertexCount; @@ -45,6 +50,18 @@ namespace SH_COMP std::vector indices; }; + struct RigNode + { + std::string name; + SHMat4 transform; + std::vector children; + }; + + struct RigData + { + RigNode* root; + }; + struct MeshAssetHeader { size_t meshCount; @@ -53,6 +70,7 @@ namespace SH_COMP struct MeshAsset { MeshAssetHeader header; + RigData rig; std::vector headers; std::vector meshes; }; diff --git a/src/main.cpp b/src/main.cpp index 943b5d9..f672878 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -53,7 +53,7 @@ int main(int argc, char* argv[]) // SH_COMP::MeshCompiler::LoadAndCompile(path); //} - SH_COMP::MeshCompiler::LoadAndCompile("Racoon.gltf"); + SH_COMP::MeshCompiler::LoadAndCompile("Racoon.fbx"); return 0; }