2
0
Эх сурвалжийг харах

add skeleton and mesh to node containing the mesh

Angelo Scandaliato 9 жил өмнө
parent
commit
71de606770
1 өөрчлөгдсөн 36 нэмэгдсэн , 13 устгасан
  1. 36 13
      code/glTFExporter.cpp

+ 36 - 13
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<Node>& nodeIn, Ref<Node>& 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<Mesh>& meshRef, Ref<Buffer>& bufferRef)
 {
     std::string skinName = aim->mName.C_Str();
@@ -373,7 +398,6 @@ void ExportSkin(Asset& mAsset, const aiMesh* aim, Ref<Mesh>& meshRef, Ref<Buffer
 
     mat4* inverseBindMatricesData = new mat4[aim->mNumBones];
 
-    //-------------------------------------------------------
     // 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<Mesh>& meshRef, Ref<Buffer
     if (vertexWeightAccessor) p.attributes.weight.push_back(vertexWeightAccessor);
 
 
-    // Create the skinned mesh instance node.
-    Ref<Node> node = mAsset.nodes.Create(mAsset.FindUniqueID(skinName, "node"));
-    // Ref<Node> 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<Node> rootNode = mAsset.nodes.Get(unsigned(0));
+    Ref<Node> 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.