From 71de606770e16d025dba2822903022190fe011c8 Mon Sep 17 00:00:00 2001 From: Angelo Scandaliato Date: Sat, 8 Oct 2016 15:34:00 -0700 Subject: [PATCH] add skeleton and mesh to node containing the mesh --- code/glTFExporter.cpp | 49 +++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/code/glTFExporter.cpp b/code/glTFExporter.cpp index 749dca527..0963ea8a0 100644 --- a/code/glTFExporter.cpp +++ b/code/glTFExporter.cpp @@ -152,7 +152,10 @@ glTFExporter::glTFExporter(const char* filename, IOSystem* pIOSystem, const aiSc } } - +/* + * Copy a 4x4 matrix from struct aiMatrix to typedef mat4. + * Also converts from row-major to column-major storage. + */ static void CopyValue(const aiMatrix4x4& v, glTF::mat4& o) { o[ 0] = v.a1; o[ 1] = v.b1; o[ 2] = v.c1; o[ 3] = v.d1; @@ -364,6 +367,28 @@ void glTFExporter::ExportMaterials() } } +/* + * Search through node hierarchy and find the node containing the given meshID. + * Returns true on success, and false otherwise. + */ +bool FindMeshNode(Ref& nodeIn, Ref& meshNode, std::string meshID) +{ + for (unsigned int i = 0; i < nodeIn->meshes.size(); ++i) { + if (meshID.compare(nodeIn->meshes[i]->id) == 0) { + meshNode = nodeIn; + return true; + } + } + + for (unsigned int i = 0; i < nodeIn->children.size(); ++i) { + if(FindMeshNode(nodeIn->children[i], meshNode, meshID)) { + return true; + } + } + + return false; +} + void ExportSkin(Asset& mAsset, const aiMesh* aim, Ref& meshRef, Ref& bufferRef) { std::string skinName = aim->mName.C_Str(); @@ -373,7 +398,6 @@ void ExportSkin(Asset& mAsset, const aiMesh* aim, Ref& meshRef, RefmNumBones]; - //------------------------------------------------------- // Store the vertex joint and weight data. vec4* vertexJointData = new vec4[aim->mNumVertices]; vec4* vertexWeightData = new vec4[aim->mNumVertices]; @@ -427,13 +451,13 @@ void ExportSkin(Asset& mAsset, const aiMesh* aim, Ref& meshRef, Ref node = mAsset.nodes.Create(mAsset.FindUniqueID(skinName, "node")); - // Ref node = mAsset.nodes.Get(aim->mBones[0]->mName.C_Str()); - node->meshes.push_back(meshRef); - node->name = node->id; - node->skeletons.push_back(mAsset.nodes.Get(aim->mBones[0]->mName.C_Str())); - node->skin = skinRef; + // Find node that contains this mesh and add "skeletons" and "skin" attributes to that node. + Ref rootNode = mAsset.nodes.Get(unsigned(0)); + Ref meshNode; + FindMeshNode(rootNode, meshNode, meshRef->id); + + meshNode->skeletons.push_back(mAsset.nodes.Get(aim->mBones[0]->mName.C_Str())); + meshNode->skin = skinRef; } void glTFExporter::ExportMeshes() @@ -558,10 +582,9 @@ void glTFExporter::ExportMeshes() } /*************** Skins ****************/ - ///TODO: Fix skinning animation - // if(aim->HasBones()) { - // ExportSkin(*mAsset, aim, m, b); - // } + if(aim->HasBones()) { + ExportSkin(*mAsset, aim, m, b); + } /****************** Compression ******************/ ///TODO: animation: weights, joints.