From ebcf266bc71979398231ef93c34b1b54da92347d Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 4 Jul 2009 17:51:14 +0000 Subject: [PATCH] Added some normal smoothing code. --- .../PolyVoxCore/include/IndexedSurfacePatch.h | 1 + .../source/IndexedSurfacePatch.cpp | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/library/PolyVoxCore/include/IndexedSurfacePatch.h b/library/PolyVoxCore/include/IndexedSurfacePatch.h index 420511d9..9432b6d6 100644 --- a/library/PolyVoxCore/include/IndexedSurfacePatch.h +++ b/library/PolyVoxCore/include/IndexedSurfacePatch.h @@ -54,6 +54,7 @@ namespace PolyVox const bool isEmpty(void) const; void smooth(float fAmount, bool bIncludeEdgeVertices = false); + void sumNearbyNormals(bool bNormalise = true); POLYVOX_SHARED_PTR extractSubset(std::set setMaterials); diff --git a/library/PolyVoxCore/source/IndexedSurfacePatch.cpp b/library/PolyVoxCore/source/IndexedSurfacePatch.cpp index 5e52ec1d..ab9d8329 100644 --- a/library/PolyVoxCore/source/IndexedSurfacePatch.cpp +++ b/library/PolyVoxCore/source/IndexedSurfacePatch.cpp @@ -171,10 +171,53 @@ namespace PolyVox } } + void IndexedSurfacePatch::sumNearbyNormals(bool bNormalise) + { + if(m_vecVertices.size() == 0) //FIXME - I don't think we should need this test, but I have seen crashes otherwise... + { + return; + } + + std::vector summedNormals(m_vecVertices.size()); + + //Initialise all normals to zero. Pretty sure this is ok, + //as the vector should stoer all elements contiguously. + memset(&summedNormals[0], 0, summedNormals.size() * sizeof(Vector3DFloat)); + + for(vector::iterator iterIndex = m_vecTriangleIndices.begin(); iterIndex != m_vecTriangleIndices.end();) + { + SurfaceVertex& v0 = m_vecVertices[*iterIndex]; + Vector3DFloat& v0New = summedNormals[*iterIndex]; + iterIndex++; + SurfaceVertex& v1 = m_vecVertices[*iterIndex]; + Vector3DFloat& v1New = summedNormals[*iterIndex]; + iterIndex++; + SurfaceVertex& v2 = m_vecVertices[*iterIndex]; + Vector3DFloat& v2New = summedNormals[*iterIndex]; + iterIndex++; + + Vector3DFloat sumOfNormals = v0.getNormal() + v1.getNormal() + v2.getNormal(); + + v0New += sumOfNormals; + v1New += sumOfNormals; + v2New += sumOfNormals; + } + + for(uint32_t uIndex = 0; uIndex < summedNormals.size(); uIndex++) + { + if(bNormalise) + { + summedNormals[uIndex].normalise(); + } + m_vecVertices[uIndex].setNormal(summedNormals[uIndex]); + } + } + void IndexedSurfacePatch::generateAveragedFaceNormals(bool bNormalise, bool bIncludeEdgeVertices) { Vector3DFloat offset = static_cast(m_Region.getLowerCorner()); + //Initially zero the normals for(vector::iterator iterVertex = m_vecVertices.begin(); iterVertex != m_vecVertices.end(); iterVertex++) { if(m_Region.containsPoint(iterVertex->getPosition() + offset, 0.001))