From ce6a5e5d561c96ef8db0a5098576d51c7a340633 Mon Sep 17 00:00:00 2001 From: Sergio Acereda Date: Mon, 23 Apr 2018 11:33:48 +0200 Subject: [PATCH] ImproveCacheLocality crashes if non triangular faces --- code/ImproveCacheLocality.cpp | 13 ++++++++----- code/VertexTriangleAdjacency.cpp | 24 ++++++++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/code/ImproveCacheLocality.cpp b/code/ImproveCacheLocality.cpp index cc6e9db46..ac079ade5 100644 --- a/code/ImproveCacheLocality.cpp +++ b/code/ImproveCacheLocality.cpp @@ -276,8 +276,9 @@ float ImproveCacheLocalityProcess::ProcessMesh( aiMesh* pMesh, unsigned int mesh // so iterate through all vertices of the current triangle const aiFace* pcFace = &pMesh->mFaces[ fidx ]; - for (unsigned int* p = pcFace->mIndices, *p2 = pcFace->mIndices+3;p != p2;++p) { - const unsigned int dp = *p; + unsigned nind = pcFace->mNumIndices; + for (unsigned ind = 0; ind < nind; ind++) { + unsigned dp = pcFace->mIndices[ind]; // the current vertex won't have any free triangles after this step if (ivdx != (int)dp) { @@ -375,9 +376,11 @@ float ImproveCacheLocalityProcess::ProcessMesh( aiMesh* pMesh, unsigned int mesh // sort the output index buffer back to the input array piCSIter = piIBOutput; for (aiFace* pcFace = pMesh->mFaces; pcFace != pcEnd;++pcFace) { - pcFace->mIndices[0] = *piCSIter++; - pcFace->mIndices[1] = *piCSIter++; - pcFace->mIndices[2] = *piCSIter++; + unsigned nind = pcFace->mNumIndices; + unsigned * ind = pcFace->mIndices; + if (nind > 0) ind[0] = *piCSIter++; + if (nind > 1) ind[1] = *piCSIter++; + if (nind > 2) ind[2] = *piCSIter++; } // delete temporary storage diff --git a/code/VertexTriangleAdjacency.cpp b/code/VertexTriangleAdjacency.cpp index 5886ca372..b41fd029d 100644 --- a/code/VertexTriangleAdjacency.cpp +++ b/code/VertexTriangleAdjacency.cpp @@ -88,10 +88,13 @@ VertexTriangleAdjacency::VertexTriangleAdjacency(aiFace *pcFaces, *piEnd++ = 0u; // first pass: compute the number of faces referencing each vertex - for (aiFace* pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace) { - pi[pcFace->mIndices[0]]++; - pi[pcFace->mIndices[1]]++; - pi[pcFace->mIndices[2]]++; + for (aiFace* pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace) + { + unsigned nind = pcFace->mNumIndices; + unsigned * ind = pcFace->mIndices; + if (nind > 0) pi[ind[0]]++; + if (nind > 1) pi[ind[1]]++; + if (nind > 2) pi[ind[2]]++; } // second pass: compute the final offset table @@ -109,15 +112,12 @@ VertexTriangleAdjacency::VertexTriangleAdjacency(aiFace *pcFaces, this->mAdjacencyTable = new unsigned int[iSum]; iSum = 0; for (aiFace* pcFace = pcFaces; pcFace != pcFaceEnd; ++pcFace,++iSum) { + unsigned nind = pcFace->mNumIndices; + unsigned * ind = pcFace->mIndices; - unsigned int idx = pcFace->mIndices[0]; - mAdjacencyTable[pi[idx]++] = iSum; - - idx = pcFace->mIndices[1]; - mAdjacencyTable[pi[idx]++] = iSum; - - idx = pcFace->mIndices[2]; - mAdjacencyTable[pi[idx]++] = iSum; + if (nind > 0) mAdjacencyTable[pi[ind[0]]++] = iSum; + if (nind > 1) mAdjacencyTable[pi[ind[1]]++] = iSum; + if (nind > 2) mAdjacencyTable[pi[ind[2]]++] = iSum; } // fourth pass: undo the offset computations made during the third pass // We could do this in a separate buffer, but this would be TIMES slower.