Drop faces when indices are out of range.

pull/3329/head
Malcolm Tyrrell 2020-07-15 09:05:09 +01:00
parent 67a710efad
commit a56134ba33
1 changed files with 67 additions and 50 deletions

View File

@ -298,25 +298,37 @@ void glTF2Importer::ImportMaterials(glTF2::Asset &r) {
} }
} }
static inline void SetFace(aiFace &face, int a) { static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a) {
face.mNumIndices = 1; if (a >= numVertices) {
face.mIndices = new unsigned int[1]; return;
face.mIndices[0] = a; }
face->mNumIndices = 1;
face->mIndices = new unsigned int[1];
face->mIndices[0] = a;
++face;
} }
static inline void SetFace(aiFace &face, int a, int b) { static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b) {
face.mNumIndices = 2; if ((a >= numVertices) || (b >= numVertices)) {
face.mIndices = new unsigned int[2]; return;
face.mIndices[0] = a; }
face.mIndices[1] = b; face->mNumIndices = 2;
face->mIndices = new unsigned int[2];
face->mIndices[0] = a;
face->mIndices[1] = b;
++face;
} }
static inline void SetFace(aiFace &face, int a, int b, int c) { static inline void SetFaceAndAdvance(aiFace*& face, unsigned int numVertices, unsigned int a, unsigned int b, unsigned int c) {
face.mNumIndices = 3; if ((a >= numVertices) || (b >= numVertices) || (c >= numVertices)) {
face.mIndices = new unsigned int[3]; return;
face.mIndices[0] = a; }
face.mIndices[1] = b; face->mNumIndices = 3;
face.mIndices[2] = c; face->mIndices = new unsigned int[3];
face->mIndices[0] = a;
face->mIndices[1] = b;
face->mIndices[2] = c;
++face;
} }
#ifdef ASSIMP_BUILD_DEBUG #ifdef ASSIMP_BUILD_DEBUG
@ -486,6 +498,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
} }
aiFace *faces = nullptr; aiFace *faces = nullptr;
aiFace *facePtr = nullptr;
size_t nFaces = 0; size_t nFaces = 0;
if (prim.indices) { if (prim.indices) {
@ -497,9 +510,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
switch (prim.mode) { switch (prim.mode) {
case PrimitiveMode_POINTS: { case PrimitiveMode_POINTS: {
nFaces = count; nFaces = count;
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; ++i) { for (unsigned int i = 0; i < count; ++i) {
SetFace(faces[i], data.GetUInt(i)); SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i));
} }
break; break;
} }
@ -510,9 +523,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped."); ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
count = nFaces * 2; count = nFaces * 2;
} }
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 2) { for (unsigned int i = 0; i < count; i += 2) {
SetFace(faces[i / 2], data.GetUInt(i), data.GetUInt(i + 1)); SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1));
} }
break; break;
} }
@ -520,13 +533,13 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
case PrimitiveMode_LINE_LOOP: case PrimitiveMode_LINE_LOOP:
case PrimitiveMode_LINE_STRIP: { case PrimitiveMode_LINE_STRIP: {
nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0);
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
SetFace(faces[0], data.GetUInt(0), data.GetUInt(1)); SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(0), data.GetUInt(1));
for (unsigned int i = 2; i < count; ++i) { for (unsigned int i = 2; i < count; ++i) {
SetFace(faces[i - 1], faces[i - 2].mIndices[1], data.GetUInt(i)); SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], data.GetUInt(i));
} }
if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop
SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]); SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]);
} }
break; break;
} }
@ -537,33 +550,33 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped."); ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
count = nFaces * 3; count = nFaces * 3;
} }
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 3) { for (unsigned int i = 0; i < count; i += 3) {
SetFace(faces[i / 3], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
} }
break; break;
} }
case PrimitiveMode_TRIANGLE_STRIP: { case PrimitiveMode_TRIANGLE_STRIP: {
nFaces = count - 2; nFaces = count - 2;
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < nFaces; ++i) { for (unsigned int i = 0; i < nFaces; ++i) {
//The ordering is to ensure that the triangles are all drawn with the same orientation //The ordering is to ensure that the triangles are all drawn with the same orientation
if ((i + 1) % 2 == 0) { if ((i + 1) % 2 == 0) {
//For even n, vertices n + 1, n, and n + 2 define triangle n //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)); SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2));
} else { } else {
//For odd n, vertices n, n+1, and n+2 define triangle n //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)); SetFaceAndAdvance(facePtr, aim->mNumVertices, data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
} }
} }
break; break;
} }
case PrimitiveMode_TRIANGLE_FAN: case PrimitiveMode_TRIANGLE_FAN:
nFaces = count - 2; nFaces = count - 2;
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2)); SetFaceAndAdvance(facePtr, data.GetUInt(0), data.GetUInt(1), data.GetUInt(2));
for (unsigned int i = 1; i < nFaces; ++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)); SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i + 2));
} }
break; break;
} }
@ -575,9 +588,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
switch (prim.mode) { switch (prim.mode) {
case PrimitiveMode_POINTS: { case PrimitiveMode_POINTS: {
nFaces = count; nFaces = count;
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; ++i) { for (unsigned int i = 0; i < count; ++i) {
SetFace(faces[i], i); SetFaceAndAdvance(facePtr, aim->mNumVertices, i);
} }
break; break;
} }
@ -588,9 +601,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped."); ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
count = (unsigned int)nFaces * 2; count = (unsigned int)nFaces * 2;
} }
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 2) { for (unsigned int i = 0; i < count; i += 2) {
SetFace(faces[i / 2], i, i + 1); SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1);
} }
break; break;
} }
@ -598,13 +611,13 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
case PrimitiveMode_LINE_LOOP: case PrimitiveMode_LINE_LOOP:
case PrimitiveMode_LINE_STRIP: { case PrimitiveMode_LINE_STRIP: {
nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0); nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0);
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
SetFace(faces[0], 0, 1); SetFaceAndAdvance(facePtr, aim->mNumVertices, 0, 1);
for (unsigned int i = 2; i < count; ++i) { for (unsigned int i = 2; i < count; ++i) {
SetFace(faces[i - 1], faces[i - 2].mIndices[1], i); SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[i - 2].mIndices[1], i);
} }
if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop
SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]); SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[count - 2].mIndices[1], faces[0].mIndices[0]);
} }
break; break;
} }
@ -615,42 +628,46 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped."); ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
count = (unsigned int)nFaces * 3; count = (unsigned int)nFaces * 3;
} }
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 3) { for (unsigned int i = 0; i < count; i += 3) {
SetFace(faces[i / 3], i, i + 1, i + 2); SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1, i + 2);
} }
break; break;
} }
case PrimitiveMode_TRIANGLE_STRIP: { case PrimitiveMode_TRIANGLE_STRIP: {
nFaces = count - 2; nFaces = count - 2;
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
for (unsigned int i = 0; i < nFaces; ++i) { for (unsigned int i = 0; i < nFaces; ++i) {
//The ordering is to ensure that the triangles are all drawn with the same orientation //The ordering is to ensure that the triangles are all drawn with the same orientation
if ((i + 1) % 2 == 0) { if ((i + 1) % 2 == 0) {
//For even n, vertices n + 1, n, and n + 2 define triangle n //For even n, vertices n + 1, n, and n + 2 define triangle n
SetFace(faces[i], i + 1, i, i + 2); SetFaceAndAdvance(facePtr, aim->mNumVertices, i + 1, i, i + 2);
} else { } else {
//For odd n, vertices n, n+1, and n+2 define triangle n //For odd n, vertices n, n+1, and n+2 define triangle n
SetFace(faces[i], i, i + 1, i + 2); SetFaceAndAdvance(facePtr, aim->mNumVertices, i, i + 1, i + 2);
} }
} }
break; break;
} }
case PrimitiveMode_TRIANGLE_FAN: case PrimitiveMode_TRIANGLE_FAN:
nFaces = count - 2; nFaces = count - 2;
faces = new aiFace[nFaces]; facePtr = faces = new aiFace[nFaces];
SetFace(faces[0], 0, 1, 2); SetFaceAndAdvance(facePtr, aim->mNumVertices, 0, 1, 2);
for (unsigned int i = 1; i < nFaces; ++i) { for (unsigned int i = 1; i < nFaces; ++i) {
SetFace(faces[i], faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2); SetFaceAndAdvance(facePtr, aim->mNumVertices, faces[0].mIndices[0], faces[i - 1].mIndices[2], i + 2);
} }
break; break;
} }
} }
if (nullptr != faces) { if (faces) {
aim->mFaces = faces; aim->mFaces = faces;
aim->mNumFaces = static_cast<unsigned int>(nFaces); const unsigned int actualNumFaces = facePtr - faces;
ai_assert(CheckValidFacesIndices(faces, static_cast<unsigned>(nFaces), aim->mNumVertices)); if (actualNumFaces < nFaces) {
ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped.");
}
aim->mNumFaces = actualNumFaces;
ai_assert(CheckValidFacesIndices(faces, actualNumFaces, aim->mNumVertices));
} }
if (prim.material) { if (prim.material) {