From ae0f82d5b7972ac7353329501bfa3690227f8d97 Mon Sep 17 00:00:00 2001 From: Alexandre Avenel Date: Tue, 31 Jul 2018 23:06:55 +0200 Subject: [PATCH] Fix #2077 : GLTF segfault using triangle strip --- code/glTF2Importer.cpp | 44 +++++++++++++++++++++++++++--------------- code/glTFImporter.cpp | 5 ----- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp index adbeb90f2..0089ef072 100755 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -550,9 +550,18 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) case PrimitiveMode_TRIANGLE_STRIP: { nFaces = count - 2; faces = new aiFace[nFaces]; - SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); - for (unsigned int i = 3; i < count; ++i) { - SetFace(faces[i - 2], faces[i - 1].mIndices[1], faces[i - 1].mIndices[2], data.GetUInt(i)); + for (unsigned int i = 0; i < nFaces; ++i) { + //The ordering is to ensure that the triangles are all drawn with the same orientation + if ((i + 1) % 2 == 0) + { + //For even n, vertices n + 1, n, and n + 2 define triangle n + SetFace(faces[i], data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2)); + } + else + { + //For odd n, vertices n, n+1, and n+2 define triangle n + SetFace(faces[i], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); + } } break; } @@ -560,8 +569,8 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) nFaces = count - 2; faces = new aiFace[nFaces]; SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); - for (unsigned int i = 3; i < count; ++i) { - SetFace(faces[i - 2], faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i)); + for (unsigned int i = 1; i < nFaces; ++i) { + SetFace(faces[i], faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2)); } break; } @@ -615,9 +624,18 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) case PrimitiveMode_TRIANGLE_STRIP: { nFaces = count - 2; faces = new aiFace[nFaces]; - SetFace(faces[0], 0, 1, 2); - for (unsigned int i = 3; i < count; ++i) { - SetFace(faces[i - 2], faces[i - 1].mIndices[1], faces[i - 1].mIndices[2], i); + for (unsigned int i = 0; i < nFaces; ++i) { + //The ordering is to ensure that the triangles are all drawn with the same orientation + if ((i+1) % 2 == 0) + { + //For even n, vertices n + 1, n, and n + 2 define triangle n + SetFace(faces[i], i+1, i, i+2); + } + else + { + //For odd n, vertices n, n+1, and n+2 define triangle n + SetFace(faces[i], i, i+1, i+2); + } } break; } @@ -625,8 +643,8 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) nFaces = count - 2; faces = new aiFace[nFaces]; SetFace(faces[0], 0, 1, 2); - for (unsigned int i = 3; i < count; ++i) { - SetFace(faces[i - 2], faces[0].mIndices[0], faces[i - 1].mIndices[2], i); + for (unsigned int i = 1; i < nFaces; ++i) { + SetFace(faces[i], faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2); } break; } @@ -848,12 +866,6 @@ void glTF2Importer::InternReadFile(const std::string& pFile, aiScene* pScene, IO ImportNodes(asset); - // TODO: it does not split the loaded vertices, should it? - //pScene->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT; - MakeVerboseFormatProcess process; - process.Execute(pScene); - - if (pScene->mNumMeshes == 0) { pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE; } diff --git a/code/glTFImporter.cpp b/code/glTFImporter.cpp index a091ce7df..c68969dc6 100755 --- a/code/glTFImporter.cpp +++ b/code/glTFImporter.cpp @@ -740,11 +740,6 @@ void glTFImporter::InternReadFile(const std::string& pFile, aiScene* pScene, IOS ImportNodes(asset); - // TODO: it does not split the loaded vertices, should it? - //pScene->mFlags |= AI_SCENE_FLAGS_NON_VERBOSE_FORMAT; - MakeVerboseFormatProcess process; - process.Execute(pScene); - if (pScene->mNumMeshes == 0) { pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;