From fcf2d6357e7793226a3bd49972be30d402c1f959 Mon Sep 17 00:00:00 2001 From: Angelo Scandaliato Date: Sat, 15 Oct 2016 17:21:55 -0700 Subject: [PATCH] check if bones exist before creating skin --- code/glTFExporter.cpp | 66 +++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/code/glTFExporter.cpp b/code/glTFExporter.cpp index 3c13db894..074d3654a 100644 --- a/code/glTFExporter.cpp +++ b/code/glTFExporter.cpp @@ -517,17 +517,23 @@ void glTFExporter::ExportMeshes() } //---------------------------------------- - // For the skin - std::string skinName = mAsset->FindUniqueID("skin", "skin"); - Ref skinRef = mAsset->skins.Create(skinName); - skinRef->name = skinName; - // std::vector inverseBindMatricesData; - std::vector inverseBindMatricesData; + // Initialize variables for the skin + bool createSkin = false; + for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) { + const aiMesh* aim = mScene->mMeshes[idx_mesh]; + if(aim->HasBones()) { + createSkin = true; + break; + } + } - // Identity Matrix =====> skinRef->bindShapeMatrix - // Temporary. Hard-coded identity matrix here - skinRef->bindShapeMatrix.isPresent = true; - IdentityMatrix4(skinRef->bindShapeMatrix.value); + Ref skinRef; + std::string skinName = mAsset->FindUniqueID("skin", "skin"); + std::vector inverseBindMatricesData; + if(createSkin) { + skinRef = mAsset->skins.Create(skinName); + skinRef->name = skinName; + } //---------------------------------------- for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) { @@ -725,24 +731,30 @@ void glTFExporter::ExportMeshes() //---------------------------------------- // Finish the skin // Create the Accessor for skinRef->inverseBindMatrices - mat4* invBindMatrixData = new mat4[inverseBindMatricesData.size()]; - for (int idx_joint = 0; idx_joint < inverseBindMatricesData.size(); ++idx_joint) { - CopyValue(inverseBindMatricesData[idx_joint], invBindMatrixData[idx_joint]); + if (createSkin) { + mat4* invBindMatrixData = new mat4[inverseBindMatricesData.size()]; + for (int idx_joint = 0; idx_joint < inverseBindMatricesData.size(); ++idx_joint) { + CopyValue(inverseBindMatricesData[idx_joint], invBindMatrixData[idx_joint]); + } + + Ref invBindMatrixAccessor = ExportData(*mAsset, skinName, b, inverseBindMatricesData.size(), invBindMatrixData, AttribType::MAT4, AttribType::MAT4, ComponentType_FLOAT); + if (invBindMatrixAccessor) skinRef->inverseBindMatrices = invBindMatrixAccessor; + + // Identity Matrix =====> skinRef->bindShapeMatrix + // Temporary. Hard-coded identity matrix here + skinRef->bindShapeMatrix.isPresent = true; + IdentityMatrix4(skinRef->bindShapeMatrix.value); + + // Find node that contains this mesh and add "skeletons" and "skin" attributes to that node. + Ref rootNode = mAsset->nodes.Get(unsigned(0)); + Ref meshNode; + std::string meshID = mAsset->meshes.Get(unsigned(0))->id; + FindMeshNode(rootNode, meshNode, meshID); + + Ref rootJoint = FindSkeletonRootJoint(skinRef); + meshNode->skeletons.push_back(rootJoint); + meshNode->skin = skinRef; } - - Ref invBindMatrixAccessor = ExportData(*mAsset, skinName, b, inverseBindMatricesData.size(), invBindMatrixData, AttribType::MAT4, AttribType::MAT4, ComponentType_FLOAT); - if (invBindMatrixAccessor) skinRef->inverseBindMatrices = invBindMatrixAccessor; - - // Find node that contains this mesh and add "skeletons" and "skin" attributes to that node. - Ref rootNode = mAsset->nodes.Get(unsigned(0)); - Ref meshNode; - std::string meshID = mAsset->meshes.Get(unsigned(0))->id; - FindMeshNode(rootNode, meshNode, meshID); - - Ref rootJoint = FindSkeletonRootJoint(skinRef); - meshNode->skeletons.push_back(rootJoint); - meshNode->skin = skinRef; - //---------------------------------------- } /*