From e7699ca5858974076d118790949d3954457184eb Mon Sep 17 00:00:00 2001 From: Paul Arden Date: Thu, 5 Jul 2018 09:37:57 +1000 Subject: [PATCH 1/2] Added support for non-indexed meshes in glTF importer. Addresses issue #2046. --- code/glTF2Importer.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++ code/glTFImporter.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp index 15c338716..a523af390 100644 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -521,6 +521,79 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); } } + else { // no indices provided so directly generate from counts + aiFace* faces = 0; + unsigned int nFaces = 0; + + // use the already determined count as it includes checks + unsigned int count = aim->mNumVertices; + + switch (prim.mode) { + case PrimitiveMode_POINTS: { + nFaces = count; + faces = new aiFace[nFaces]; + for (unsigned int i = 0; i < count; ++i) { + SetFace(faces[i], i); + } + break; + } + + case PrimitiveMode_LINES: { + nFaces = count / 2; + faces = new aiFace[nFaces]; + for (unsigned int i = 0; i < count; i += 2) { + SetFace(faces[i / 2], i, i + 1); + } + break; + } + + case PrimitiveMode_LINE_LOOP: + case PrimitiveMode_LINE_STRIP: { + nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); + faces = new aiFace[nFaces]; + SetFace(faces[0], 0, 1); + for (unsigned int i = 2; i < count; ++i) { + SetFace(faces[i - 1], faces[i - 2].mIndices[1], i); + } + if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop + SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]); + } + break; + } + + case PrimitiveMode_TRIANGLES: { + nFaces = count / 3; + faces = new aiFace[nFaces]; + for (unsigned int i = 0; i < count; i += 3) { + SetFace(faces[i / 3], i, i + 1, i + 2); + } + break; + } + 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); + } + break; + } + case PrimitiveMode_TRIANGLE_FAN: + 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); + } + break; + } + + if (faces) { + aim->mFaces = faces; + aim->mNumFaces = nFaces; + ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); + } + } if (prim.material) { diff --git a/code/glTFImporter.cpp b/code/glTFImporter.cpp index 54c32fb8b..ac584dfa3 100644 --- a/code/glTFImporter.cpp +++ b/code/glTFImporter.cpp @@ -426,6 +426,79 @@ void glTFImporter::ImportMeshes(glTF::Asset& r) ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); } } + else { // no indices provided so directly generate from counts + aiFace* faces = 0; + unsigned int nFaces = 0; + + // use the already determined count as it includes checks + unsigned int count = aim->mNumVertices; + + switch (prim.mode) { + case PrimitiveMode_POINTS: { + nFaces = count; + faces = new aiFace[nFaces]; + for (unsigned int i = 0; i < count; ++i) { + SetFace(faces[i], i); + } + break; + } + + case PrimitiveMode_LINES: { + nFaces = count / 2; + faces = new aiFace[nFaces]; + for (unsigned int i = 0; i < count; i += 2) { + SetFace(faces[i / 2], i, i + 1); + } + break; + } + + case PrimitiveMode_LINE_LOOP: + case PrimitiveMode_LINE_STRIP: { + nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); + faces = new aiFace[nFaces]; + SetFace(faces[0], 0, 1); + for (unsigned int i = 2; i < count; ++i) { + SetFace(faces[i - 1], faces[i - 2].mIndices[1], i); + } + if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop + SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]); + } + break; + } + + case PrimitiveMode_TRIANGLES: { + nFaces = count / 3; + faces = new aiFace[nFaces]; + for (unsigned int i = 0; i < count; i += 3) { + SetFace(faces[i / 3], i, i + 1, i + 2); + } + break; + } + 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); + } + break; + } + case PrimitiveMode_TRIANGLE_FAN: + 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); + } + break; + } + + if (faces) { + aim->mFaces = faces; + aim->mNumFaces = nFaces; + ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); + } + } if (prim.material) { From e43e30b7264afb05d1522ba8d106fdb483b73985 Mon Sep 17 00:00:00 2001 From: Paul Arden Date: Thu, 5 Jul 2018 09:49:08 +1000 Subject: [PATCH 2/2] Slight rework for fix to issue #2046 to remove some duplicated code. --- code/glTF2Importer.cpp | 25 ++++++++----------------- code/glTFImporter.cpp | 25 ++++++++----------------- 2 files changed, 16 insertions(+), 34 deletions(-) mode change 100644 => 100755 code/glTF2Importer.cpp mode change 100644 => 100755 code/glTFImporter.cpp diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp old mode 100644 new mode 100755 index a523af390..37f7ed8d5 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -446,10 +446,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) } - if (prim.indices) { - aiFace* faces = 0; - unsigned int nFaces = 0; + aiFace* faces = 0; + unsigned int nFaces = 0; + if (prim.indices) { unsigned int count = prim.indices->count; Accessor::Indexer data = prim.indices->GetIndexer(); @@ -514,16 +514,8 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) } break; } - - if (faces) { - aim->mFaces = faces; - aim->mNumFaces = nFaces; - ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); - } } else { // no indices provided so directly generate from counts - aiFace* faces = 0; - unsigned int nFaces = 0; // use the already determined count as it includes checks unsigned int count = aim->mNumVertices; @@ -587,14 +579,13 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r) } break; } - - if (faces) { - aim->mFaces = faces; - aim->mNumFaces = nFaces; - ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); - } } + if (faces) { + aim->mFaces = faces; + aim->mNumFaces = nFaces; + ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); + } if (prim.material) { aim->mMaterialIndex = prim.material.GetIndex(); diff --git a/code/glTFImporter.cpp b/code/glTFImporter.cpp old mode 100644 new mode 100755 index ac584dfa3..a091ce7df --- a/code/glTFImporter.cpp +++ b/code/glTFImporter.cpp @@ -351,10 +351,10 @@ void glTFImporter::ImportMeshes(glTF::Asset& r) } - if (prim.indices) { - aiFace* faces = 0; - unsigned int nFaces = 0; + aiFace* faces = 0; + unsigned int nFaces = 0; + if (prim.indices) { unsigned int count = prim.indices->count; Accessor::Indexer data = prim.indices->GetIndexer(); @@ -419,16 +419,8 @@ void glTFImporter::ImportMeshes(glTF::Asset& r) } break; } - - if (faces) { - aim->mFaces = faces; - aim->mNumFaces = nFaces; - ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); - } } else { // no indices provided so directly generate from counts - aiFace* faces = 0; - unsigned int nFaces = 0; // use the already determined count as it includes checks unsigned int count = aim->mNumVertices; @@ -492,14 +484,13 @@ void glTFImporter::ImportMeshes(glTF::Asset& r) } break; } - - if (faces) { - aim->mFaces = faces; - aim->mNumFaces = nFaces; - ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); - } } + if (faces) { + aim->mFaces = faces; + aim->mNumFaces = nFaces; + ai_assert(CheckValidFacesIndices(faces, nFaces, aim->mNumVertices)); + } if (prim.material) { aim->mMaterialIndex = prim.material.GetIndex();