From 8137b1eb46ce545a8759354072a218a94c5d34f6 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 6 Oct 2007 18:31:37 +0000 Subject: [PATCH] Refactoring mesh generation code. --- CMakeLists.txt | 8 +-- ...{SurfacePatch.h => HalfEdgeSurfacePatch.h} | 10 ++-- include/PolyVoxSceneManager.h | 4 +- include/SurfacePatchRenderable.h | 12 ++--- include/SurfaceTypes.h | 2 + ...facePatch.cpp => HalfEdgeSurfacePatch.cpp} | 14 +---- source/PolyVoxSceneManager.cpp | 51 ++++++++++++------- source/SurfacePatchRenderable.cpp | 8 +-- 8 files changed, 58 insertions(+), 51 deletions(-) rename include/{SurfacePatch.h => HalfEdgeSurfacePatch.h} (85%) rename source/{SurfacePatch.cpp => HalfEdgeSurfacePatch.cpp} (95%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4319d131..f93b4668 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,14 +2,15 @@ PROJECT(PolyVoxSceneManager) #Projects source files SET(SRC_FILES + source/AbstractSurfacePatch.cpp source/Block.cpp + source/HalfEdgeSurfacePatch.cpp source/MarchingCubesTables.cpp source/MaterialMap.cpp source/MaterialMapManager.cpp source/MaterialMapSerializer.cpp source/PolyVoxSceneManager.cpp - source/SurfaceEdge.cpp - source/SurfacePatch.cpp + source/SurfaceEdge.cpp source/SurfacePatchRenderable.cpp source/SurfaceTriangle.cpp source/SurfaceVertex.cpp @@ -21,8 +22,10 @@ SET(SRC_FILES #Projects headers files SET(INC_FILES + include/AbstractSurfacePatch.h include/Block.h include/Constants.h + include/HalfEdgeSurfacePatch.h include/IntegralVector3.h include/MarchingCubesTables.h include/MaterialMap.h @@ -30,7 +33,6 @@ SET(INC_FILES include/MaterialMapSerializer.h include/PolyVoxSceneManager.h include/SurfaceEdge.h - include/SurfacePatch.h include/SurfaceTypes.h include/SurfacePatchRenderable.h include/SurfaceTriangle.h diff --git a/include/SurfacePatch.h b/include/HalfEdgeSurfacePatch.h similarity index 85% rename from include/SurfacePatch.h rename to include/HalfEdgeSurfacePatch.h index df16f419..2a61a741 100644 --- a/include/SurfacePatch.h +++ b/include/HalfEdgeSurfacePatch.h @@ -4,6 +4,7 @@ #include #include +#include "AbstractSurfacePatch.h" #include "IntegralVector3.h" #include "SurfaceTypes.h" #include "VolumeIterator.h" @@ -11,7 +12,7 @@ namespace Ogre { - class SurfacePatch + class SurfacePatch : public AbstractSurfacePatch { public: SurfacePatch(); @@ -21,9 +22,7 @@ namespace Ogre SurfaceEdgeIterator getEdgesBegin(void); SurfaceEdgeIterator getEdgesEnd(void); SurfaceTriangleIterator getTrianglesBegin(void); - SurfaceTriangleIterator getTrianglesEnd(void); - SurfaceVertexIterator getVerticesBegin(void); - SurfaceVertexIterator getVerticesEnd(void); + SurfaceTriangleIterator getTrianglesEnd(void); //Users of the class might want these for debugging or info purposes. uint getNoOfEdges(void) const; @@ -42,8 +41,7 @@ namespace Ogre - private: - std::set m_listVertices; + private: std::set m_listTriangles; std::set m_listEdges; diff --git a/include/PolyVoxSceneManager.h b/include/PolyVoxSceneManager.h index dec0528a..a27bb0f7 100644 --- a/include/PolyVoxSceneManager.h +++ b/include/PolyVoxSceneManager.h @@ -26,7 +26,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "MaterialMap.h" #include "SurfacePatchRenderable.h" #include "SurfaceTriangle.h" -#include "SurfacePatch.h" +#include "AbstractSurfacePatch.h" #include "TypeDef.h" #include "Volume.h" #include "SurfaceVertex.h" @@ -81,7 +81,7 @@ namespace Ogre void generateLevelVolume(void); - void generateMeshDataForRegion(uint regionX, uint regionY, uint regionZ, std::map& result) const; + std::map generateMeshDataForRegion(uint regionX, uint regionY, uint regionZ) const; void doRegionGrowing(uint xStart, uint yStart, uint zStart, uchar value); diff --git a/include/SurfacePatchRenderable.h b/include/SurfacePatchRenderable.h index f0a9e6db..af834960 100644 --- a/include/SurfacePatchRenderable.h +++ b/include/SurfacePatchRenderable.h @@ -4,9 +4,9 @@ #include "Ogre.h" #include -#include "SurfacePatch.h" -#include "SurfaceTriangle.h" -#include "SurfaceVertex.h" +#include "AbstractSurfacePatch.h" +//#include "SurfaceTriangle.h" +//#include "SurfaceVertex.h" namespace Ogre { @@ -16,11 +16,11 @@ namespace Ogre class SurfacePatchRenderable : public SimpleRenderable { public: - SurfacePatchRenderable(SurfacePatch& patchToRender, const String& material = "BaseWhiteNoLighting"); + SurfacePatchRenderable(AbstractSurfacePatch* patchToRender, const String& material = "BaseWhiteNoLighting"); ~SurfacePatchRenderable(void); - void updateWithNewSurfacePatch(SurfacePatch& patchToRender); - void setGeometry(SurfacePatch& patchToRender); + void updateWithNewSurfacePatch(AbstractSurfacePatch* patchToRender); + void setGeometry(AbstractSurfacePatch* patchToRender); Real getSquaredViewDepth(const Camera *cam) const; Real getBoundingRadius(void) const; diff --git a/include/SurfaceTypes.h b/include/SurfaceTypes.h index f3337606..794b76b3 100644 --- a/include/SurfaceTypes.h +++ b/include/SurfaceTypes.h @@ -1,6 +1,8 @@ #ifndef __SurfaceTypes_H__ #define __SurfaceTypes_H__ +#include + namespace Ogre { class SurfaceVertex; diff --git a/source/SurfacePatch.cpp b/source/HalfEdgeSurfacePatch.cpp similarity index 95% rename from source/SurfacePatch.cpp rename to source/HalfEdgeSurfacePatch.cpp index b7d9885a..28162886 100644 --- a/source/SurfacePatch.cpp +++ b/source/HalfEdgeSurfacePatch.cpp @@ -1,4 +1,4 @@ -#include "SurfacePatch.h" +#include "HalfEdgeSurfacePatch.h" #include "Constants.h" #include "SurfaceVertex.h" @@ -41,17 +41,7 @@ namespace Ogre SurfaceTriangleIterator SurfacePatch::getTrianglesEnd(void) { return m_listTriangles.end(); - } - - SurfaceVertexIterator SurfacePatch::getVerticesBegin(void) - { - return m_listVertices.begin(); - } - - SurfaceVertexIterator SurfacePatch::getVerticesEnd(void) - { - return m_listVertices.end(); - } + } uint SurfacePatch::getNoOfEdges(void) const { diff --git a/source/PolyVoxSceneManager.cpp b/source/PolyVoxSceneManager.cpp index 230e8791..6f08e6ec 100644 --- a/source/PolyVoxSceneManager.cpp +++ b/source/PolyVoxSceneManager.cpp @@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "MaterialMapManager.h" #include "SurfaceVertex.h" #include "SurfaceEdge.h" +#include "HalfEdgeSurfacePatch.h" #include "PolyVoxSceneManager.h" #include "VolumeIterator.h" #include "VolumeManager.h" @@ -254,8 +255,7 @@ namespace Ogre //Generate the surface //std::vector< std::vector > vertexData; //std::vector< std::vector > indexData; - std::map mapSurfacePatch; - generateMeshDataForRegion(regionX,regionY,regionZ, mapSurfacePatch); + std::map mapSurfacePatch = generateMeshDataForRegion(regionX,regionY,regionZ); //If a SceneNode doesn't exist in this position then create one. std::map::iterator iterSceneNode = sceneNodes.find(UIntVector3(regionX,regionY,regionZ)); @@ -273,7 +273,7 @@ namespace Ogre //For each surface attach it to the scene node. //for(uint meshCt = 1; meshCt < 256; ++meshCt) - for(std::map::iterator iterSurfacePatch = mapSurfacePatch.begin(); iterSurfacePatch != mapSurfacePatch.end(); ++iterSurfacePatch) + for(std::map::iterator iterSurfacePatch = mapSurfacePatch.begin(); iterSurfacePatch != mapSurfacePatch.end(); ++iterSurfacePatch) { /*std::vector vertexData; std::vector indexData; @@ -460,8 +460,10 @@ namespace Ogre } } - void PolyVoxSceneManager::generateMeshDataForRegion(const uint regionX, const uint regionY, const uint regionZ, std::map& result) const + std::map PolyVoxSceneManager::generateMeshDataForRegion(const uint regionX, const uint regionY, const uint regionZ) const { + std::map surfacePatchMapResult; + //LogManager::getSingleton().logMessage("Generating Mesh Data"); //First and last voxels in the region const uint firstX = regionX * OGRE_REGION_SIDE_LENGTH; @@ -618,6 +620,19 @@ namespace Ogre const uchar material1 = vertMaterials[triTable[iCubeIndex][i+1]]; const uchar material2 = vertMaterials[triTable[iCubeIndex][i+2]]; + if(surfacePatchMapResult.find(material0) == surfacePatchMapResult.end()) + { + surfacePatchMapResult.insert(std::make_pair(material0,new SurfacePatch)); + } + if(surfacePatchMapResult.find(material1) == surfacePatchMapResult.end()) + { + surfacePatchMapResult.insert(std::make_pair(material1,new SurfacePatch)); + } + if(surfacePatchMapResult.find(material2) == surfacePatchMapResult.end()) + { + surfacePatchMapResult.insert(std::make_pair(material2,new SurfacePatch)); + } + SurfaceVertex surfaceVertex0Alpha1(vertex0,1.0); SurfaceVertex surfaceVertex1Alpha1(vertex1,1.0); SurfaceVertex surfaceVertex2Alpha1(vertex2,1.0); @@ -625,7 +640,7 @@ namespace Ogre //If all the materials are the same, we just need one triangle for that material with all the alphas set high. if((material0 == material1) && (material1 == material2)) { - result[material0].addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); + surfacePatchMapResult[material0]->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha1); } //If there not all the same, we need one triangle for each unique material. //We'll also need some vertices with low alphas for blending. @@ -637,24 +652,24 @@ namespace Ogre if(material0 == material1) { - result[material0].addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha0); - result[material2].addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha0, surfaceVertex2Alpha1); + surfacePatchMapResult[material0]->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha1, surfaceVertex2Alpha0); + surfacePatchMapResult[material2]->addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha0, surfaceVertex2Alpha1); } else if(material1 == material2) { - result[material1].addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha1, surfaceVertex2Alpha1); - result[material0].addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha0, surfaceVertex2Alpha0); + surfacePatchMapResult[material1]->addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha1, surfaceVertex2Alpha1); + surfacePatchMapResult[material0]->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha0, surfaceVertex2Alpha0); } else if(material2 == material0) { - result[material0].addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha0, surfaceVertex2Alpha1); - result[material1].addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha1, surfaceVertex2Alpha0); + surfacePatchMapResult[material0]->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha0, surfaceVertex2Alpha1); + surfacePatchMapResult[material1]->addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha1, surfaceVertex2Alpha0); } else { - result[material0].addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha0, surfaceVertex2Alpha0); - result[material1].addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha1, surfaceVertex2Alpha0); - result[material2].addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha0, surfaceVertex2Alpha1); + surfacePatchMapResult[material0]->addTriangle(surfaceVertex0Alpha1, surfaceVertex1Alpha0, surfaceVertex2Alpha0); + surfacePatchMapResult[material1]->addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha1, surfaceVertex2Alpha0); + surfacePatchMapResult[material2]->addTriangle(surfaceVertex0Alpha0, surfaceVertex1Alpha0, surfaceVertex2Alpha1); } } }//For each triangle @@ -663,11 +678,11 @@ namespace Ogre //FIXME - can it happen that we have no vertices or triangles? Should exit early? - for(std::map::iterator iterPatch = result.begin(); iterPatch != result.end(); ++iterPatch) + for(std::map::iterator iterPatch = surfacePatchMapResult.begin(); iterPatch != surfacePatchMapResult.end(); ++iterPatch) { - SurfaceVertexIterator iterSurfaceVertex = iterPatch->second.getVerticesBegin(); - while(iterSurfaceVertex != iterPatch->second.getVerticesEnd()) + SurfaceVertexIterator iterSurfaceVertex = iterPatch->second->getVerticesBegin(); + while(iterSurfaceVertex != iterPatch->second->getVerticesEnd()) { Vector3 tempNormal = computeNormal((iterSurfaceVertex->getPosition() + offset).toOgreVector3()/2.0f, CENTRAL_DIFFERENCE); const_cast(*iterSurfaceVertex).setNormal(tempNormal); @@ -684,7 +699,7 @@ namespace Ogre //LogManager::getSingleton().logMessage("Finished Generating Mesh Data"); - //return result; + return surfacePatchMapResult; } Vector3 PolyVoxSceneManager::computeNormal(const Vector3& position, NormalGenerationMethod normalGenerationMethod) const diff --git a/source/SurfacePatchRenderable.cpp b/source/SurfacePatchRenderable.cpp index 69034752..e428ca40 100644 --- a/source/SurfacePatchRenderable.cpp +++ b/source/SurfacePatchRenderable.cpp @@ -7,7 +7,7 @@ namespace Ogre { - SurfacePatchRenderable::SurfacePatchRenderable(SurfacePatch& patchToRender, const String& material) + SurfacePatchRenderable::SurfacePatchRenderable(AbstractSurfacePatch* patchToRender, const String& material) { //Set up what we can of the vertex data mRenderOp.vertexData = new VertexData(); @@ -40,16 +40,16 @@ namespace Ogre delete mRenderOp.indexData; } - void SurfacePatchRenderable::updateWithNewSurfacePatch(SurfacePatch& patchToRender) + void SurfacePatchRenderable::updateWithNewSurfacePatch(AbstractSurfacePatch* patchToRender) { setGeometry(patchToRender); } - void SurfacePatchRenderable::setGeometry(SurfacePatch& patchToRender) + void SurfacePatchRenderable::setGeometry(AbstractSurfacePatch* patchToRender) { std::vector vecVertices; std::vector vecIndices; - patchToRender.fillVertexAndIndexData(vecVertices,vecIndices); + patchToRender->fillVertexAndIndexData(vecVertices,vecIndices); //Initialization stuff mRenderOp.vertexData->vertexCount = vecVertices.size();