Refactoring mesh generation code.
This commit is contained in:
parent
e276ada7ce
commit
8137b1eb46
@ -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
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <set>
|
||||
#include <list>
|
||||
|
||||
#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<SurfaceVertex> m_listVertices;
|
||||
private:
|
||||
std::set<SurfaceTriangle> m_listTriangles;
|
||||
std::set<SurfaceEdge> m_listEdges;
|
||||
|
@ -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<uchar, SurfacePatch>& result) const;
|
||||
std::map<uchar, AbstractSurfacePatch*> generateMeshDataForRegion(uint regionX, uint regionY, uint regionZ) const;
|
||||
|
||||
void doRegionGrowing(uint xStart, uint yStart, uint zStart, uchar value);
|
||||
|
||||
|
@ -4,9 +4,9 @@
|
||||
#include "Ogre.h"
|
||||
#include <vector>
|
||||
|
||||
#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;
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef __SurfaceTypes_H__
|
||||
#define __SurfaceTypes_H__
|
||||
|
||||
#include <set>
|
||||
|
||||
namespace Ogre
|
||||
{
|
||||
class SurfaceVertex;
|
||||
|
@ -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
|
||||
{
|
@ -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<SurfaceVertex> > vertexData;
|
||||
//std::vector< std::vector<SurfaceTriangle> > indexData;
|
||||
std::map<uchar, SurfacePatch> mapSurfacePatch;
|
||||
generateMeshDataForRegion(regionX,regionY,regionZ, mapSurfacePatch);
|
||||
std::map<uchar, AbstractSurfacePatch*> mapSurfacePatch = generateMeshDataForRegion(regionX,regionY,regionZ);
|
||||
|
||||
//If a SceneNode doesn't exist in this position then create one.
|
||||
std::map<UIntVector3, SceneNode*>::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<uchar, SurfacePatch>::iterator iterSurfacePatch = mapSurfacePatch.begin(); iterSurfacePatch != mapSurfacePatch.end(); ++iterSurfacePatch)
|
||||
for(std::map<uchar, AbstractSurfacePatch*>::iterator iterSurfacePatch = mapSurfacePatch.begin(); iterSurfacePatch != mapSurfacePatch.end(); ++iterSurfacePatch)
|
||||
{
|
||||
/*std::vector<SurfaceVertex> vertexData;
|
||||
std::vector<uint> indexData;
|
||||
@ -460,8 +460,10 @@ namespace Ogre
|
||||
}
|
||||
}
|
||||
|
||||
void PolyVoxSceneManager::generateMeshDataForRegion(const uint regionX, const uint regionY, const uint regionZ, std::map<uchar, SurfacePatch>& result) const
|
||||
std::map<uchar, AbstractSurfacePatch*> PolyVoxSceneManager::generateMeshDataForRegion(const uint regionX, const uint regionY, const uint regionZ) const
|
||||
{
|
||||
std::map<uchar, AbstractSurfacePatch*> 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<uchar, SurfacePatch>::iterator iterPatch = result.begin(); iterPatch != result.end(); ++iterPatch)
|
||||
for(std::map<uchar, AbstractSurfacePatch*>::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<SurfaceVertex&>(*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
|
||||
|
@ -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<SurfaceVertex> vecVertices;
|
||||
std::vector<ushort> vecIndices;
|
||||
patchToRender.fillVertexAndIndexData(vecVertices,vecIndices);
|
||||
patchToRender->fillVertexAndIndexData(vecVertices,vecIndices);
|
||||
|
||||
//Initialization stuff
|
||||
mRenderOp.vertexData->vertexCount = vecVertices.size();
|
||||
|
Loading…
x
Reference in New Issue
Block a user