From b990efce2400a3df8b4de46715e3bf2531e41572 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 27 Jun 2008 16:44:18 +0000 Subject: [PATCH] Work on improving mesh decimation. --- PolyVoxCore/include/SurfaceAdjusters.h | 1 + PolyVoxCore/source/SurfaceAdjusters.cpp | 74 ++++++++++++++++++++++++ PolyVoxCore/source/SurfaceExtractors.cpp | 10 +++- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/PolyVoxCore/include/SurfaceAdjusters.h b/PolyVoxCore/include/SurfaceAdjusters.h index e791af95..771b7c51 100644 --- a/PolyVoxCore/include/SurfaceAdjusters.h +++ b/PolyVoxCore/include/SurfaceAdjusters.h @@ -33,6 +33,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. namespace PolyVox { POLYVOX_API void smoothRegionGeometry(BlockVolume* volumeData, RegionGeometry& regGeom); + POLYVOX_API void adjustDecimatedGeometry(BlockVolume* volumeData, RegionGeometry& regGeom, std::uint8_t val); } #endif \ No newline at end of file diff --git a/PolyVoxCore/source/SurfaceAdjusters.cpp b/PolyVoxCore/source/SurfaceAdjusters.cpp index e82caf57..2982f64a 100644 --- a/PolyVoxCore/source/SurfaceAdjusters.cpp +++ b/PolyVoxCore/source/SurfaceAdjusters.cpp @@ -9,6 +9,8 @@ #include +using namespace std; + namespace PolyVox { void smoothRegionGeometry(BlockVolume* volumeData, RegionGeometry& regGeom) @@ -74,4 +76,76 @@ namespace PolyVox ++iterSurfaceVertex; } //while(iterSurfaceVertex != vecVertices.end()) } + + void adjustDecimatedGeometry(BlockVolume* volumeData, RegionGeometry& regGeom, uint8_t val) + { + BlockVolumeIterator volIter(*volumeData); + + std::vector& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices; + std::vector::iterator iterSurfaceVertex = vecVertices.begin(); + while(iterSurfaceVertex != vecVertices.end()) + { + Vector3DFloat v3dPos = iterSurfaceVertex->getPosition() + static_cast(regGeom.m_v3dRegionPosition); + Vector3DInt32 v3dFloor = static_cast(v3dPos); + + BlockVolumeIterator volIter(*volumeData); + + //Check all corners are within the volume, allowing a boundary for gradient estimation + bool lowerCornerInside = volumeData->containsPoint(v3dFloor,1); + bool upperCornerInside = volumeData->containsPoint(v3dFloor+Vector3DInt32(1,1,1),1); + + if(lowerCornerInside && upperCornerInside) //If this test fails the vertex will be left as it was + { + //volIter.setPosition(static_cast(v3dFloor)); + //const uint8_t uFloor = volIter.getVoxel(); + if(((v3dPos.getX() - v3dFloor.getX()) < 0.001) && ((v3dPos.getY() - v3dFloor.getY()) < 0.001) && ((v3dPos.getZ() - v3dFloor.getZ()) < 0.001)) + //int x = v3dPos.getX(); + //if(x % 2 != 0) + //if((iterSurfaceVertex->getNormal().getX() > 0.5f) || (iterSurfaceVertex->getNormal().getX() < -0.5f)) + { + //exit(0); + //volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(1,0,0))); + //const uint8_t uCeil = volIter.getVoxel(); + //if(uFloor == uCeil) //In this case they must both be zero + { + //if(iterSurfaceVertex->getNormal().getX() > 0) + { + iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() - iterSurfaceVertex->getNormal() * 0.5f); + v3dPos = iterSurfaceVertex->getPosition() + static_cast(regGeom.m_v3dRegionPosition); + v3dFloor = static_cast(v3dPos); + + volIter.setPosition(static_cast(v3dFloor)); + const uint8_t uFloor = volIter.getVoxel(); + + uint8_t uCeil; + if((iterSurfaceVertex->getNormal().getX() > 0.5f) || (iterSurfaceVertex->getNormal().getX() < -0.5f)) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(1,0,0))); + uCeil = volIter.getVoxel(); + } + if((iterSurfaceVertex->getNormal().getY() > 0.5f) || (iterSurfaceVertex->getNormal().getY() < -0.5f)) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(0,1,0))); + uCeil = volIter.getVoxel(); + } + if((iterSurfaceVertex->getNormal().getZ() > 0.5f) || (iterSurfaceVertex->getNormal().getZ() < -0.5f)) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(0,0,1))); + uCeil = volIter.getVoxel(); + } + + if(uFloor == uCeil) + { + //NOTE: The normal should actually be multiplied by 1.0f. This works + //for the simple cube but causes depth fighting on more complex shapes. + iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() - iterSurfaceVertex->getNormal() * 0.5f); + } + } + } + } + } + + ++iterSurfaceVertex; + } //while(iterSurfaceVertex != vecVertices.end()) + } } \ No newline at end of file diff --git a/PolyVoxCore/source/SurfaceExtractors.cpp b/PolyVoxCore/source/SurfaceExtractors.cpp index f8a31ff4..9f40b119 100644 --- a/PolyVoxCore/source/SurfaceExtractors.cpp +++ b/PolyVoxCore/source/SurfaceExtractors.cpp @@ -37,9 +37,15 @@ namespace PolyVox //for(int ct = 0; ct < 2; ct++) Vector3DInt32 temp = regionGeometry.m_v3dRegionPosition; //temp /= 16; - if(temp.getX() % 32 == 0) + if(temp.getY() % 32 == 0) { - smoothRegionGeometry(volume.getVolumeData(), regionGeometry); + //smoothRegionGeometry(volume.getVolumeData(), regionGeometry); + generateDecimatedMeshDataForRegion(volume.getVolumeData(), 0, *iterChangedRegions, regionGeometry.m_patchSingleMaterial); + } + else + { + generateDecimatedMeshDataForRegion(volume.getVolumeData(), 1, *iterChangedRegions, regionGeometry.m_patchSingleMaterial); + //adjustDecimatedGeometry(volume.getVolumeData(), regionGeometry, 1); } //computeNormalsForVertices(volume.getVolumeData(), regionGeometry, CENTRAL_DIFFERENCE);