From 7150be613062d8100259d01ef36491037601911d Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 15 Jun 2008 17:12:55 +0000 Subject: [PATCH] Tidying up normal generation code. --- PolyVoxCore/CMakeLists.txt | 3 + PolyVoxCore/include/GradientEstimators.h | 5 + PolyVoxCore/include/GradientEstimators.inl | 96 +++++----- PolyVoxCore/include/SurfaceAdjusters.h | 38 ++++ PolyVoxCore/include/SurfaceExtractors.h | 1 - PolyVoxCore/include/SurfaceVertex.h | 1 + PolyVoxCore/source/GradientEstimators.cpp | 174 ++++++++++++++++++ PolyVoxCore/source/SurfaceAdjusters.cpp | 21 +++ PolyVoxCore/source/SurfaceExtractors.cpp | 129 ++----------- .../source/SurfaceExtractorsDecimated.cpp | 21 ++- PolyVoxCore/source/SurfaceVertex.cpp | 8 +- TODO.txt | 27 +++ 12 files changed, 352 insertions(+), 172 deletions(-) create mode 100644 PolyVoxCore/include/SurfaceAdjusters.h create mode 100644 PolyVoxCore/source/GradientEstimators.cpp create mode 100644 PolyVoxCore/source/SurfaceAdjusters.cpp create mode 100644 TODO.txt diff --git a/PolyVoxCore/CMakeLists.txt b/PolyVoxCore/CMakeLists.txt index 6455d3e4..5eef3f17 100644 --- a/PolyVoxCore/CMakeLists.txt +++ b/PolyVoxCore/CMakeLists.txt @@ -9,10 +9,12 @@ SET(POLYVOX_VERSION "${POLYVOX_VERSION_MAJOR}.${POLYVOX_VERSION_MINOR}.${POLYVOX #Projects source files SET(SRC_FILES + source/GradientEstimators.cpp source/IndexedSurfacePatch.cpp source/MarchingCubesTables.cpp source/Region.cpp source/RegionGeometry.cpp + source/SurfaceAdjusters.cpp source/SurfaceExtractors.cpp source/SurfaceExtractorsDecimated.cpp source/SurfaceVertex.cpp @@ -39,6 +41,7 @@ SET(INC_FILES include/PolyVoxForwardDeclarations.h include/Region.h include/RegionGeometry.h + include/SurfaceAdjusters.h include/SurfaceExtractors.h include/SurfaceExtractorsDecimated.h include/SurfaceVertex.h diff --git a/PolyVoxCore/include/GradientEstimators.h b/PolyVoxCore/include/GradientEstimators.h index 8b65f543..cd4efa1b 100644 --- a/PolyVoxCore/include/GradientEstimators.h +++ b/PolyVoxCore/include/GradientEstimators.h @@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "BlockVolumeIterator.h" +#include + namespace PolyVox { template @@ -37,6 +39,9 @@ namespace PolyVox template Vector3DFloat computeSobelGradient(const BlockVolumeIterator& volIter); + + POLYVOX_API void computeNormalsForVertices(BlockVolume* volumeData, RegionGeometry& regGeom, NormalGenerationMethod normalGenerationMethod); + POLYVOX_API Vector3DFloat computeNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod); } #include "GradientEstimators.inl" diff --git a/PolyVoxCore/include/GradientEstimators.inl b/PolyVoxCore/include/GradientEstimators.inl index 2f44cfbc..798691c6 100644 --- a/PolyVoxCore/include/GradientEstimators.inl +++ b/PolyVoxCore/include/GradientEstimators.inl @@ -36,9 +36,9 @@ namespace PolyVox return Vector3DFloat ( - static_cast(voxel1px) - static_cast(voxel1nx), - static_cast(voxel1py) - static_cast(voxel1ny), - static_cast(voxel1pz) - static_cast(voxel1nz) + static_cast(voxel1nx) - static_cast(voxel1px), + static_cast(voxel1ny) - static_cast(voxel1py), + static_cast(voxel1nz) - static_cast(voxel1pz) ); } @@ -61,9 +61,9 @@ namespace PolyVox return Vector3DFloat ( - static_cast(voxel1px) - static_cast(voxel1nx), - static_cast(voxel1py) - static_cast(voxel1ny), - static_cast(voxel1pz) - static_cast(voxel1nz) + static_cast(voxel1nx) - static_cast(voxel1px), + static_cast(voxel1ny) - static_cast(voxel1py), + static_cast(voxel1nz) - static_cast(voxel1pz) ); } @@ -134,50 +134,50 @@ namespace PolyVox const VoxelType pVoxel1px1py0pz = volIter.peekVoxel1px1py0pz() > 0 ? 1: 0; const VoxelType pVoxel1px1py1pz = volIter.peekVoxel1px1py1pz() > 0 ? 1: 0; + const int xGrad(- weights[0][0][0] * pVoxel1nx1ny1nz - + weights[1][0][0] * pVoxel1nx1ny0pz - weights[2][0][0] * + pVoxel1nx1ny1pz - weights[0][1][0] * pVoxel1nx0py1nz - + weights[1][1][0] * pVoxel1nx0py0pz - weights[2][1][0] * + pVoxel1nx0py1pz - weights[0][2][0] * pVoxel1nx1py1nz - + weights[1][2][0] * pVoxel1nx1py0pz - weights[2][2][0] * + pVoxel1nx1py1pz + weights[0][0][2] * pVoxel1px1ny1nz + + weights[1][0][2] * pVoxel1px1ny0pz + weights[2][0][2] * + pVoxel1px1ny1pz + weights[0][1][2] * pVoxel1px0py1nz + + weights[1][1][2] * pVoxel1px0py0pz + weights[2][1][2] * + pVoxel1px0py1pz + weights[0][2][2] * pVoxel1px1py1nz + + weights[1][2][2] * pVoxel1px1py0pz + weights[2][2][2] * + pVoxel1px1py1pz); + const int yGrad(- weights[0][0][0] * pVoxel1nx1ny1nz - + weights[1][0][0] * pVoxel1nx1ny0pz - weights[2][0][0] * + pVoxel1nx1ny1pz + weights[0][2][0] * pVoxel1nx1py1nz + + weights[1][2][0] * pVoxel1nx1py0pz + weights[2][2][0] * + pVoxel1nx1py1pz - weights[0][0][1] * pVoxel0px1ny1nz - + weights[1][0][1] * pVoxel0px1ny0pz - weights[2][0][1] * + pVoxel0px1ny1pz + weights[0][2][1] * pVoxel0px1py1nz + + weights[1][2][1] * pVoxel0px1py0pz + weights[2][2][1] * + pVoxel0px1py1pz - weights[0][0][2] * pVoxel1px1ny1nz - + weights[1][0][2] * pVoxel1px1ny0pz - weights[2][0][2] * + pVoxel1px1ny1pz + weights[0][2][2] * pVoxel1px1py1nz + + weights[1][2][2] * pVoxel1px1py0pz + weights[2][2][2] * + pVoxel1px1py1pz); - const int xGrad(- weights[0][0][0] * ( pVoxel1nx1ny1nz) - - weights[1][0][0] * ( pVoxel1nx1ny0pz) - weights[2][0][0] * - ( pVoxel1nx1ny1pz) - weights[0][1][0] * ( pVoxel1nx0py1nz) - - weights[1][1][0] * ( pVoxel1nx0py0pz) - weights[2][1][0] * - ( pVoxel1nx0py1pz) - weights[0][2][0] * ( pVoxel1nx1py1nz) - - weights[1][2][0] * ( pVoxel1nx1py0pz) - weights[2][2][0] * - ( pVoxel1nx1py1pz) + weights[0][0][2] * ( pVoxel1px1ny1nz) + - weights[1][0][2] * ( pVoxel1px1ny0pz) + weights[2][0][2] * - ( pVoxel1px1ny1pz) + weights[0][1][2] * ( pVoxel1px0py1nz) + - weights[1][1][2] * ( pVoxel1px0py0pz) + weights[2][1][2] * - ( pVoxel1px0py1pz) + weights[0][2][2] * ( pVoxel1px1py1nz) + - weights[1][2][2] * ( pVoxel1px1py0pz) + weights[2][2][2] * - ( pVoxel1px1py1pz)); + const int zGrad(- weights[0][0][0] * pVoxel1nx1ny1nz + + weights[2][0][0] * pVoxel1nx1ny1pz - weights[0][1][0] * + pVoxel1nx0py1nz + weights[2][1][0] * pVoxel1nx0py1pz - + weights[0][2][0] * pVoxel1nx1py1nz + weights[2][2][0] * + pVoxel1nx1py1pz - weights[0][0][1] * pVoxel0px1ny1nz + + weights[2][0][1] * pVoxel0px1ny1pz - weights[0][1][1] * + pVoxel0px0py1nz + weights[2][1][1] * pVoxel0px0py1pz - + weights[0][2][1] * pVoxel0px1py1nz + weights[2][2][1] * + pVoxel0px1py1pz - weights[0][0][2] * pVoxel1px1ny1nz + + weights[2][0][2] * pVoxel1px1ny1pz - weights[0][1][2] * + pVoxel1px0py1nz + weights[2][1][2] * pVoxel1px0py1pz - + weights[0][2][2] * pVoxel1px1py1nz + weights[2][2][2] * + pVoxel1px1py1pz); - const int yGrad(- weights[0][0][0] * ( pVoxel1nx1ny1nz) - - weights[1][0][0] * ( pVoxel1nx1ny0pz) - weights[2][0][0] * - ( pVoxel1nx1ny1pz) + weights[0][2][0] * ( pVoxel1nx1py1nz) + - weights[1][2][0] * ( pVoxel1nx1py0pz) + weights[2][2][0] * - ( pVoxel1nx1py1pz) - weights[0][0][1] * ( pVoxel0px1ny1nz) - - weights[1][0][1] * ( pVoxel0px1ny0pz) - weights[2][0][1] * - ( pVoxel0px1ny1pz) + weights[0][2][1] * ( pVoxel0px1py1nz) + - weights[1][2][1] * ( pVoxel0px1py0pz) + weights[2][2][1] * - ( pVoxel0px1py1pz) - weights[0][0][2] * ( pVoxel1px1ny1nz) - - weights[1][0][2] * ( pVoxel1px1ny0pz) - weights[2][0][2] * - ( pVoxel1px1ny1pz) + weights[0][2][2] * ( pVoxel1px1py1nz) + - weights[1][2][2] * ( pVoxel1px1py0pz) + weights[2][2][2] * - ( pVoxel1px1py1pz)); - - const int zGrad(- weights[0][0][0] * ( pVoxel1nx1ny1nz) + - weights[2][0][0] * ( pVoxel1nx1ny1pz) - weights[0][1][0] * - ( pVoxel1nx0py1nz) + weights[2][1][0] * ( pVoxel1nx0py1pz) - - weights[0][2][0] * ( pVoxel1nx1py1nz) + weights[2][2][0] * - ( pVoxel1nx1py1pz) - weights[0][0][1] * ( pVoxel0px1ny1nz) + - weights[2][0][1] * ( pVoxel0px1ny1pz) - weights[0][1][1] * - ( pVoxel0px0py1nz) + weights[2][1][1] * ( pVoxel0px0py1pz) - - weights[0][2][1] * ( pVoxel0px1py1nz) + weights[2][2][1] * - ( pVoxel0px1py1pz) - weights[0][0][2] * ( pVoxel1px1ny1nz) + - weights[2][0][2] * ( pVoxel1px1ny1pz) - weights[0][1][2] * - ( pVoxel1px0py1nz) + weights[2][1][2] * ( pVoxel1px0py1pz) - - weights[0][2][2] * ( pVoxel1px1py1nz) + weights[2][2][2] * - ( pVoxel1px1py1pz)); - - return Vector3DFloat(static_cast(xGrad),static_cast(yGrad),static_cast(zGrad)); + //Note: The above actually give gradients going from low density to high density. + //For our normals we want the the other way around, so we switch the components as we return them. + return Vector3DFloat(static_cast(-xGrad),static_cast(-yGrad),static_cast(-zGrad)); } } diff --git a/PolyVoxCore/include/SurfaceAdjusters.h b/PolyVoxCore/include/SurfaceAdjusters.h new file mode 100644 index 00000000..dc96a564 --- /dev/null +++ b/PolyVoxCore/include/SurfaceAdjusters.h @@ -0,0 +1,38 @@ +#pragma region License +/****************************************************************************** +This file is part of the PolyVox library +Copyright (C) 2006 David Williams + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +******************************************************************************/ +#pragma endregion + +#ifndef __PolyVox_SurfaceAdjusters_H__ +#define __PolyVox_SurfaceAdjusters_H__ + +#pragma region Headers +#include "Constants.h" +#include "PolyVoxForwardDeclarations.h" +#include "TypeDef.h" + +#include "boost/cstdint.hpp" +#pragma endregion + +namespace PolyVox +{ + POLYVOX_API void smoothRegionGeometry(BlockVolume* volumeData, RegionGeometry& regGeom); +} + +#endif \ No newline at end of file diff --git a/PolyVoxCore/include/SurfaceExtractors.h b/PolyVoxCore/include/SurfaceExtractors.h index 56327815..5ac13811 100644 --- a/PolyVoxCore/include/SurfaceExtractors.h +++ b/PolyVoxCore/include/SurfaceExtractors.h @@ -45,7 +45,6 @@ namespace PolyVox POLYVOX_API void generateRoughVerticesForSlice(BlockVolumeIterator& volIter, Region& regSlice, const Vector3DFloat& offset, boost::uint8_t* bitmask, IndexedSurfacePatch* singleMaterialPatch,boost::int32_t vertexIndicesX[],boost::int32_t vertexIndicesY[],boost::int32_t vertexIndicesZ[]); POLYVOX_API void generateReferenceMeshDataForRegion(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); - POLYVOX_API Vector3DFloat computeNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod); POLYVOX_API void generateSmoothMeshDataForRegion(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch); POLYVOX_API Vector3DFloat computeSmoothNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod); diff --git a/PolyVoxCore/include/SurfaceVertex.h b/PolyVoxCore/include/SurfaceVertex.h index 1f387d90..4f643dbd 100644 --- a/PolyVoxCore/include/SurfaceVertex.h +++ b/PolyVoxCore/include/SurfaceVertex.h @@ -45,6 +45,7 @@ namespace PolyVox void setAlpha(float alphaToSet); void setMaterial(float materialToSet); void setNormal(const Vector3DFloat& normalToSet); + void setPosition(const Vector3DFloat& positionToSet); std::string tostring(void) const; diff --git a/PolyVoxCore/source/GradientEstimators.cpp b/PolyVoxCore/source/GradientEstimators.cpp new file mode 100644 index 00000000..e969c63d --- /dev/null +++ b/PolyVoxCore/source/GradientEstimators.cpp @@ -0,0 +1,174 @@ +#include "GradientEstimators.h" +#include "IndexedSurfacePatch.h" +#include "RegionGeometry.h" +#include "SurfaceVertex.h" + +#include "boost/cstdint.hpp" + +using namespace boost; + +namespace PolyVox +{ + POLYVOX_API void computeNormalsForVertices(BlockVolume* volumeData, RegionGeometry& regGeom, NormalGenerationMethod normalGenerationMethod) + { + std::vector& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices; + std::vector::iterator iterSurfaceVertex = vecVertices.begin(); + while(iterSurfaceVertex != vecVertices.end()) + { + const Vector3DFloat& v3dPos = iterSurfaceVertex->getPosition() + static_cast(regGeom.m_v3dRegionPosition); + const 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 + { + Vector3DFloat v3dGradient; //To store the result + + if(normalGenerationMethod == SOBEL) + { + volIter.setPosition(static_cast(v3dFloor)); + const Vector3DFloat gradFloor = computeSobelGradient(volIter); + if((v3dPos.getX() - v3dFloor.getX()) > 0.001) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(1,0,0))); + } + if((v3dPos.getY() - v3dFloor.getY()) > 0.001) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(0,1,0))); + } + if((v3dPos.getZ() - v3dFloor.getZ()) > 0.001) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(0,0,1))); + } + const Vector3DFloat gradCeil = computeSobelGradient(volIter); + v3dGradient = (gradFloor + gradCeil); + } + if(normalGenerationMethod == CENTRAL_DIFFERENCE) + { + volIter.setPosition(static_cast(v3dFloor)); + const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter); + if((v3dPos.getX() - v3dFloor.getX()) > 0.001) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(1,0,0))); + } + if((v3dPos.getY() - v3dFloor.getY()) > 0.001) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(0,1,0))); + } + if((v3dPos.getZ() - v3dFloor.getZ()) > 0.001) + { + volIter.setPosition(static_cast(v3dFloor+Vector3DInt32(0,0,1))); + } + const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); + v3dGradient = (gradFloor + gradCeil); + } + if(v3dGradient.lengthSquared() > 0.0001) + { + //If we got a normal of significant length then update it. + //Otherwise leave it as it was (should be the 'simple' version) + v3dGradient.normalise(); + iterSurfaceVertex->setNormal(v3dGradient); + } + } //(lowerCornerInside && upperCornerInside) + ++iterSurfaceVertex; + } + } + + Vector3DFloat computeNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod) + { + const float posX = position.getX(); + const float posY = position.getY(); + const float posZ = position.getZ(); + + const uint16_t floorX = static_cast(posX); + const uint16_t floorY = static_cast(posY); + const uint16_t floorZ = static_cast(posZ); + + //Check all corners are within the volume, allowing a boundary for gradient estimation + bool lowerCornerInside = volumeData->containsPoint(Vector3DInt32(floorX, floorY, floorZ),1); + bool upperCornerInside = volumeData->containsPoint(Vector3DInt32(floorX+1, floorY+1, floorZ+1),1); + if((!lowerCornerInside) || (!upperCornerInside)) + { + normalGenerationMethod = SIMPLE; + } + + Vector3DFloat result; + + BlockVolumeIterator volIter(*volumeData); //FIXME - save this somewhere - could be expensive to create? + + + if(normalGenerationMethod == SOBEL) + { + volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); + const Vector3DFloat gradFloor = computeSobelGradient(volIter); + if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 + { + volIter.setPosition(static_cast(posX+1.0),static_cast(posY),static_cast(posZ)); + } + if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 + { + volIter.setPosition(static_cast(posX),static_cast(posY+1.0),static_cast(posZ)); + } + if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 + { + volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ+1.0)); + } + const Vector3DFloat gradCeil = computeSobelGradient(volIter); + result = ((gradFloor + gradCeil) * -1.0f); + if(result.lengthSquared() < 0.0001) + { + //Operation failed - fall back on simple gradient estimation + normalGenerationMethod = SIMPLE; + } + } + if(normalGenerationMethod == CENTRAL_DIFFERENCE) + { + volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); + const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter); + if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 + { + volIter.setPosition(static_cast(posX+1.0),static_cast(posY),static_cast(posZ)); + } + if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 + { + volIter.setPosition(static_cast(posX),static_cast(posY+1.0),static_cast(posZ)); + } + if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 + { + volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ+1.0)); + } + const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); + result = ((gradFloor + gradCeil) * -1.0f); + if(result.lengthSquared() < 0.0001) + { + //Operation failed - fall back on simple gradient estimation + normalGenerationMethod = SIMPLE; + } + } + if(normalGenerationMethod == SIMPLE) + { + volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); + const uint8_t uFloor = volIter.getVoxel() > 0 ? 1 : 0; + if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 + { + uint8_t uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0; + result = Vector3DFloat(static_cast(uFloor - uCeil),0.0,0.0); + } + else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 + { + uint8_t uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0; + result = Vector3DFloat(0.0,static_cast(uFloor - uCeil),0.0); + } + else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 + { + uint8_t uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0; + result = Vector3DFloat(0.0, 0.0,static_cast(uFloor - uCeil)); + } + } + return result; + } +} diff --git a/PolyVoxCore/source/SurfaceAdjusters.cpp b/PolyVoxCore/source/SurfaceAdjusters.cpp new file mode 100644 index 00000000..c953fe42 --- /dev/null +++ b/PolyVoxCore/source/SurfaceAdjusters.cpp @@ -0,0 +1,21 @@ +#include "SurfaceAdjusters.h" + +#include "IndexedSurfacePatch.h" +#include "RegionGeometry.h" + +#include + +namespace PolyVox +{ + void smoothRegionGeometry(BlockVolume* volumeData, RegionGeometry& regGeom) + { + std::vector& vecVertices = regGeom.m_patchSingleMaterial->m_vecVertices; + std::vector::iterator iterSurfaceVertex = vecVertices.begin(); + while(iterSurfaceVertex != vecVertices.end()) + { + iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() + iterSurfaceVertex->getNormal()); + //iterSurfaceVertex->setPosition(iterSurfaceVertex->getPosition() + Vector3DFloat(10.0f,0.0f,0.0f)); + ++iterSurfaceVertex; + } + } +} \ No newline at end of file diff --git a/PolyVoxCore/source/SurfaceExtractors.cpp b/PolyVoxCore/source/SurfaceExtractors.cpp index bffbb8bd..a8ca4b4a 100644 --- a/PolyVoxCore/source/SurfaceExtractors.cpp +++ b/PolyVoxCore/source/SurfaceExtractors.cpp @@ -1,5 +1,4 @@ #include "SurfaceExtractors.h" -#include "SurfaceExtractorsDecimated.h" #include "BlockVolume.h" #include "GradientEstimators.h" @@ -7,6 +6,8 @@ #include "MarchingCubesTables.h" #include "Region.h" #include "RegionGeometry.h" +#include "SurfaceAdjusters.h" +#include "SurfaceExtractorsDecimated.h" #include "VolumeChangeTracker.h" #include "BlockVolumeIterator.h" @@ -31,6 +32,10 @@ namespace PolyVox generateDecimatedMeshDataForRegion(volume.getVolumeData(), 0, *iterChangedRegions, regionGeometry.m_patchSingleMaterial); + computeNormalsForVertices(volume.getVolumeData(), regionGeometry, CENTRAL_DIFFERENCE); + + //smoothRegionGeometry(volume.getVolumeData(), regionGeometry); + //genMultiFromSingle(regionGeometry.m_patchSingleMaterial, regionGeometry.m_patchMultiMaterial); regionGeometry.m_bContainsSingleMaterialPatch = regionGeometry.m_patchSingleMaterial->getVertices().size() > 0; @@ -122,15 +127,6 @@ namespace PolyVox delete[] vertexIndicesY1; delete[] vertexIndicesZ0; delete[] vertexIndicesZ1; - - - std::vector::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); - while(iterSurfaceVertex != singleMaterialPatch->getVertices().end()) - { - Vector3DFloat tempNormal = computeNormal(volumeData, static_cast(iterSurfaceVertex->getPosition() + offset), CENTRAL_DIFFERENCE); - const_cast(*iterSurfaceVertex).setNormal(tempNormal); - ++iterSurfaceVertex; - } } boost::uint32_t computeInitialRoughBitmaskForSlice(BlockVolumeIterator& volIter, const Region& regSlice, const Vector3DFloat& offset, uint8_t* bitmask) @@ -428,9 +424,10 @@ namespace PolyVox { if((x + offset.getX()) != regSlice.getUpperCorner().getX()) { + const uint8_t v100 = volIter.peekVoxel1px0py0pz(); const Vector3DFloat v3dPosition(x + 0.5f, y, z); - const Vector3DFloat v3dNormal(1.0f, 0.0f, 0.0f); - const uint8_t uMaterial = v000 | volIter.peekVoxel1px0py0pz(); //Because one of these is 0, the or operation takes the max. + const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f, 0.0f, 0.0f); + const uint8_t uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesX[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; @@ -440,9 +437,10 @@ namespace PolyVox { if((y + offset.getY()) != regSlice.getUpperCorner().getY()) { + const uint8_t v010 = volIter.peekVoxel0px1py0pz(); const Vector3DFloat v3dPosition(x, y + 0.5f, z); - const Vector3DFloat v3dNormal(0.0f, 1.0f, 0.0f); - const uint8_t uMaterial = v000 | volIter.peekVoxel0px1py0pz(); + const Vector3DFloat v3dNormal(0.0f, v000 > v010 ? 1.0f : -1.0f, 0.0f); + const uint8_t uMaterial = v000 | v010; SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesY[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; @@ -452,9 +450,10 @@ namespace PolyVox { //if((z + offset.getZ()) != upperCorner.getZ()) { + const uint8_t v001 = volIter.peekVoxel0px0py1pz(); const Vector3DFloat v3dPosition(x, y, z + 0.5f); - const Vector3DFloat v3dNormal(0.0f, 0.0f, 1.0f); - const uint8_t uMaterial = v000 | volIter.peekVoxel0px0py1pz(); + const Vector3DFloat v3dNormal(0.0f, 0.0f, v000 > v001 ? 1.0f : -1.0f); + const uint8_t uMaterial = v000 | v001; SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesZ[getIndex(x,y)] = singleMaterialPatch->m_vecVertices.size()-1; @@ -736,7 +735,7 @@ namespace PolyVox //for(std::map::iterator iterPatch = surfacePatchMapResult.begin(); iterPatch != surfacePatchMapResult.end(); ++iterPatch) - { + /*{ std::vector::iterator iterSurfaceVertex = singleMaterialPatch->getVertices().begin(); while(iterSurfaceVertex != singleMaterialPatch->getVertices().end()) @@ -745,101 +744,7 @@ namespace PolyVox const_cast(*iterSurfaceVertex).setNormal(tempNormal); ++iterSurfaceVertex; } - } - } - - Vector3DFloat computeNormal(BlockVolume* volumeData, const Vector3DFloat& position, NormalGenerationMethod normalGenerationMethod) - { - const float posX = position.getX(); - const float posY = position.getY(); - const float posZ = position.getZ(); - - const uint16_t floorX = static_cast(posX); - const uint16_t floorY = static_cast(posY); - const uint16_t floorZ = static_cast(posZ); - - //Check all corners are within the volume, allowing a boundary for gradient estimation - bool lowerCornerInside = volumeData->containsPoint(Vector3DInt32(floorX, floorY, floorZ),1); - bool upperCornerInside = volumeData->containsPoint(Vector3DInt32(floorX+1, floorY+1, floorZ+1),1); - if((!lowerCornerInside) || (!upperCornerInside)) - { - normalGenerationMethod = SIMPLE; - } - - Vector3DFloat result; - - BlockVolumeIterator volIter(*volumeData); //FIXME - save this somewhere - could be expensive to create? - - - if(normalGenerationMethod == SOBEL) - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); - const Vector3DFloat gradFloor = computeSobelGradient(volIter); - if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX+1.0),static_cast(posY),static_cast(posZ)); - } - if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY+1.0),static_cast(posZ)); - } - if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ+1.0)); - } - const Vector3DFloat gradCeil = computeSobelGradient(volIter); - result = ((gradFloor + gradCeil) * -1.0f); - if(result.lengthSquared() < 0.0001) - { - //Operation failed - fall back on simple gradient estimation - normalGenerationMethod = SIMPLE; - } - } - if(normalGenerationMethod == CENTRAL_DIFFERENCE) - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); - const Vector3DFloat gradFloor = computeCentralDifferenceGradient(volIter); - if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX+1.0),static_cast(posY),static_cast(posZ)); - } - if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY+1.0),static_cast(posZ)); - } - if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ+1.0)); - } - const Vector3DFloat gradCeil = computeCentralDifferenceGradient(volIter); - result = ((gradFloor + gradCeil) * -1.0f); - if(result.lengthSquared() < 0.0001) - { - //Operation failed - fall back on simple gradient estimation - normalGenerationMethod = SIMPLE; - } - } - if(normalGenerationMethod == SIMPLE) - { - volIter.setPosition(static_cast(posX),static_cast(posY),static_cast(posZ)); - const uint8_t uFloor = volIter.getVoxel() > 0 ? 1 : 0; - if((posX - floorX) > 0.25) //The result should be 0.0 or 0.5 - { - uint8_t uCeil = volIter.peekVoxel1px0py0pz() > 0 ? 1 : 0; - result = Vector3DFloat(static_cast(uFloor - uCeil),0.0,0.0); - } - else if((posY - floorY) > 0.25) //The result should be 0.0 or 0.5 - { - uint8_t uCeil = volIter.peekVoxel0px1py0pz() > 0 ? 1 : 0; - result = Vector3DFloat(0.0,static_cast(uFloor - uCeil),0.0); - } - else if((posZ - floorZ) > 0.25) //The result should be 0.0 or 0.5 - { - uint8_t uCeil = volIter.peekVoxel0px0py1pz() > 0 ? 1 : 0; - result = Vector3DFloat(0.0, 0.0,static_cast(uFloor - uCeil)); - } - } - return result; + }*/ } void generateSmoothMeshDataForRegion(BlockVolume* volumeData, Region region, IndexedSurfacePatch* singleMaterialPatch) diff --git a/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp b/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp index b93deaa2..54b58d16 100644 --- a/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp +++ b/PolyVoxCore/source/SurfaceExtractorsDecimated.cpp @@ -434,10 +434,11 @@ namespace PolyVox { if(x != regSlice.getUpperCorner().getX()) { + volIter.setPosition(x + uStepSize,y,z); + const uint8_t v100 = volIter.getMaxedVoxel(uLevel); const Vector3DFloat v3dPosition(x - offset.getX() + 0.5f * uStepSize, y - offset.getY(), z - offset.getZ()); - const Vector3DFloat v3dNormal(1.0,0.0,0.0); - volIter.setPosition(x+uStepSize,y,z); - const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(uLevel); //Because one of these is 0, the or operation takes the max. + const Vector3DFloat v3dNormal(v000 > v100 ? 1.0f : -1.0f,0.0,0.0); + const uint8_t uMaterial = v000 | v100; //Because one of these is 0, the or operation takes the max. SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesX[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; @@ -447,10 +448,11 @@ namespace PolyVox { if(y != regSlice.getUpperCorner().getY()) { + volIter.setPosition(x,y + uStepSize,z); + const uint8_t v010 = volIter.getMaxedVoxel(uLevel); const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY() + 0.5f * uStepSize, z - offset.getZ()); - const Vector3DFloat v3dNormal(0.0,1.0,0.0); - volIter.setPosition(x,y+uStepSize,z); - const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(uLevel); //Because one of these is 0, the or operation takes the max. + const Vector3DFloat v3dNormal(0.0,v000 > v010 ? 1.0f : -1.0f,0.0); + const uint8_t uMaterial = v000 | v010; //Because one of these is 0, the or operation takes the max. SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesY[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; @@ -460,10 +462,11 @@ namespace PolyVox { //if(z != regSlice.getUpperCorner.getZ()) { + volIter.setPosition(x,y,z + uStepSize); + const uint8_t v001 = volIter.getMaxedVoxel(uLevel); const Vector3DFloat v3dPosition(x - offset.getX(), y - offset.getY(), z - offset.getZ() + 0.5f * uStepSize); - const Vector3DFloat v3dNormal(0.0,0.0,1.0); - volIter.setPosition(x,y,z+uStepSize); - const uint8_t uMaterial = v000 | volIter.getMaxedVoxel(uLevel); //Because one of these is 0, the or operation takes the max. + const Vector3DFloat v3dNormal(0.0,0.0,v000 > v001 ? 1.0f : -1.0f); + const uint8_t uMaterial = v000 | v001; //Because one of these is 0, the or operation takes the max. const SurfaceVertex surfaceVertex(v3dPosition, v3dNormal, uMaterial, 1.0); singleMaterialPatch->m_vecVertices.push_back(surfaceVertex); vertexIndicesZ[getDecimatedIndex(x - offset.getX(),y - offset.getY())] = singleMaterialPatch->m_vecVertices.size()-1; diff --git a/PolyVoxCore/source/SurfaceVertex.cpp b/PolyVoxCore/source/SurfaceVertex.cpp index a74e2f38..286c6031 100644 --- a/PolyVoxCore/source/SurfaceVertex.cpp +++ b/PolyVoxCore/source/SurfaceVertex.cpp @@ -79,8 +79,12 @@ namespace PolyVox void SurfaceVertex::setNormal(const Vector3DFloat& normalToSet) { normal = normalToSet; - normal.normalise(); - } + } + + void SurfaceVertex::setPosition(const Vector3DFloat& positionToSet) + { + position = positionToSet; + } std::string SurfaceVertex::tostring(void) const { diff --git a/TODO.txt b/TODO.txt new file mode 100644 index 00000000..c5779f6b --- /dev/null +++ b/TODO.txt @@ -0,0 +1,27 @@ +For Version 0.1 +=============== +Implement Memory Pool +Clean up normal code - make normal generation a seperate pass. +Implement mesh smoothing. +Refine interface to mesh generateion - flags structure? +Refine interface to volumes and iterators. +Implement block volume tidy() funtion. +Remove hard-coded region size. +Remove boost dependancy? +Seperate namespaces - PolyVoxCore, PolyVoxUtil, PolyVoxImpl +Move getChangedRegionGeometry() out of PolyVon and into Thermite? +Remove/refactor IndexedSurfacePatch? Incorporate into RegionGeometry? +Change vertex format to ints? +Check licensing, #regions, etc. +Decimated version of marching cubes should use less memory. +Unit test - compare output to reference implementation +Sort awkward use of 'offset' in decimated marching cubes. +Add API docs +Add manual +Finish OpenGL sample. + +For Version 0.2 +=============== +Detect detatched regions. +Handle mesh generation for detatched regions. +Generate ambient lighting from volume? \ No newline at end of file