From c207e745349a18da5366286d2e8a18a21e2ce41c Mon Sep 17 00:00:00 2001 From: Jeremy Cowles Date: Sun, 17 Sep 2017 10:06:57 -0700 Subject: [PATCH] Fix glTF 2.0 multi-primitive support Previously, only one primitive was supported, in fact memory was corrupted when more than one primitive was found per glTF mesh. In this change, each primitive is unrolled as a new Assimp Mesh, resulting in multiple Assimp meshes per node when multiple primitives exist per glTF mesh. This is required in the general case, since glTF primitives can have different material bindings and primitive modes. --- code/glTF2Importer.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp index 34f12b5af..6de482981 100644 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -518,13 +518,22 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector& } if (node.mesh) { - ainode->mNumMeshes = 1; - ainode->mMeshes = new unsigned int[1]; - int k = 0; int idx = node.mesh.GetIndex(); - for (unsigned int j = meshOffsets[idx]; j < meshOffsets[idx + 1]; ++j, ++k) { + ai_assert(idx >= 0 && idx < meshOffsets.size()); + + unsigned int offBegin = meshOffsets[idx]; + unsigned int offEnd = meshOffsets[idx + 1]; + int k = 0; + + ai_assert(offEnd >= offBegin); + + ainode->mNumMeshes = offEnd - offBegin; + ainode->mMeshes = new unsigned int[ainode->mNumMeshes]; + + for (unsigned int j = offBegin; j < offEnd; ++j, ++k) { + ai_assert(k < ainode->mNumMeshes); ainode->mMeshes[k] = j; } }