Refactoring mesh generation code.

This commit is contained in:
David Williams 2007-10-07 16:33:53 +00:00
parent 8137b1eb46
commit 52446c765d
9 changed files with 149 additions and 28 deletions

View File

@ -5,6 +5,7 @@ SET(SRC_FILES
source/AbstractSurfacePatch.cpp source/AbstractSurfacePatch.cpp
source/Block.cpp source/Block.cpp
source/HalfEdgeSurfacePatch.cpp source/HalfEdgeSurfacePatch.cpp
source/IndexedSurfacePatch.cpp
source/MarchingCubesTables.cpp source/MarchingCubesTables.cpp
source/MaterialMap.cpp source/MaterialMap.cpp
source/MaterialMapManager.cpp source/MaterialMapManager.cpp
@ -26,6 +27,7 @@ SET(INC_FILES
include/Block.h include/Block.h
include/Constants.h include/Constants.h
include/HalfEdgeSurfacePatch.h include/HalfEdgeSurfacePatch.h
include/IndexedSurfacePatch.h
include/IntegralVector3.h include/IntegralVector3.h
include/MarchingCubesTables.h include/MarchingCubesTables.h
include/MaterialMap.h include/MaterialMap.h

View File

@ -0,0 +1,26 @@
#ifndef __AbstractSurfacePatch_H__
#define __AbstractSurfacePatch_H__
#include "SurfaceTypes.h"
#include "Surfacevertex.h"
namespace Ogre
{
class AbstractSurfacePatch
{
public:
AbstractSurfacePatch();
virtual ~AbstractSurfacePatch();
SurfaceVertexIterator getVerticesBegin(void);
SurfaceVertexIterator getVerticesEnd(void);
virtual void addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2) = 0;
virtual void fillVertexAndIndexData(std::vector<SurfaceVertex>& vecVertices, std::vector<ushort>& vecIndices) = 0;
protected:
std::set<SurfaceVertex> m_listVertices;
};
}
#endif /* __AbstractSurfacePatch_H__ */

View File

@ -1,5 +1,5 @@
#ifndef __SurfacePatch_H__ #ifndef __HalfEdgeSurfacePatch_H__
#define __SurfacePatch_H__ #define __HalfEdgeSurfacePatch_H__
#include <set> #include <set>
#include <list> #include <list>
@ -12,11 +12,11 @@
namespace Ogre namespace Ogre
{ {
class SurfacePatch : public AbstractSurfacePatch class HalfEdgeSurfacePatch : public AbstractSurfacePatch
{ {
public: public:
SurfacePatch(); HalfEdgeSurfacePatch();
~SurfacePatch(); ~HalfEdgeSurfacePatch();
//This allow users of the class to iterate over its contents. //This allow users of the class to iterate over its contents.
SurfaceEdgeIterator getEdgesBegin(void); SurfaceEdgeIterator getEdgesBegin(void);
@ -51,4 +51,4 @@ namespace Ogre
}; };
} }
#endif /* __SurfacePatch_H__ */ #endif /* __HalfEdgeSurfacePatch_H__ */

View File

@ -0,0 +1,29 @@
#ifndef __IndexedSurfacePatch_H__
#define __IndexedSurfacePatch_H__
#include <set>
#include <list>
#include "AbstractSurfacePatch.h"
#include "IntegralVector3.h"
#include "SurfaceTypes.h"
#include "VolumeIterator.h"
namespace Ogre
{
class IndexedSurfacePatch : public AbstractSurfacePatch
{
public:
IndexedSurfacePatch();
~IndexedSurfacePatch();
void addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2);
void fillVertexAndIndexData(std::vector<SurfaceVertex>& vecVertices, std::vector<ushort>& vecIndices);
private:
std::vector<SurfaceVertexIterator> m_vecTriangleIndices;
};
}
#endif /* __IndexedSurfacePatch_H__ */

View File

@ -0,0 +1,22 @@
#include "AbstractSurfacePatch.h"
namespace Ogre
{
AbstractSurfacePatch::AbstractSurfacePatch()
{
}
AbstractSurfacePatch::~AbstractSurfacePatch()
{
}
SurfaceVertexIterator AbstractSurfacePatch::getVerticesBegin(void)
{
return m_listVertices.begin();
}
SurfaceVertexIterator AbstractSurfacePatch::getVerticesEnd(void)
{
return m_listVertices.end();
}
}

View File

@ -12,53 +12,53 @@
namespace Ogre namespace Ogre
{ {
SurfacePatch::SurfacePatch() HalfEdgeSurfacePatch::HalfEdgeSurfacePatch()
{ {
m_listVertices.clear(); m_listVertices.clear();
m_listTriangles.clear(); m_listTriangles.clear();
m_listEdges.clear(); m_listEdges.clear();
} }
SurfacePatch::~SurfacePatch() HalfEdgeSurfacePatch::~HalfEdgeSurfacePatch()
{ {
} }
SurfaceEdgeIterator SurfacePatch::getEdgesBegin(void) SurfaceEdgeIterator HalfEdgeSurfacePatch::getEdgesBegin(void)
{ {
return m_listEdges.begin(); return m_listEdges.begin();
} }
SurfaceEdgeIterator SurfacePatch::getEdgesEnd(void) SurfaceEdgeIterator HalfEdgeSurfacePatch::getEdgesEnd(void)
{ {
return m_listEdges.end(); return m_listEdges.end();
} }
SurfaceTriangleIterator SurfacePatch::getTrianglesBegin(void) SurfaceTriangleIterator HalfEdgeSurfacePatch::getTrianglesBegin(void)
{ {
return m_listTriangles.begin(); return m_listTriangles.begin();
} }
SurfaceTriangleIterator SurfacePatch::getTrianglesEnd(void) SurfaceTriangleIterator HalfEdgeSurfacePatch::getTrianglesEnd(void)
{ {
return m_listTriangles.end(); return m_listTriangles.end();
} }
uint SurfacePatch::getNoOfEdges(void) const uint HalfEdgeSurfacePatch::getNoOfEdges(void) const
{ {
return m_listEdges.size(); return m_listEdges.size();
} }
uint SurfacePatch::getNoOfTriangles(void) const uint HalfEdgeSurfacePatch::getNoOfTriangles(void) const
{ {
return m_listTriangles.size(); return m_listTriangles.size();
} }
uint SurfacePatch::getNoOfVertices(void) const uint HalfEdgeSurfacePatch::getNoOfVertices(void) const
{ {
return m_listVertices.size(); return m_listVertices.size();
} }
void SurfacePatch::addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2) void HalfEdgeSurfacePatch::addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2)
{ {
SurfaceVertexIterator v0Iter = findOrAddVertex(v0); SurfaceVertexIterator v0Iter = findOrAddVertex(v0);
SurfaceVertexIterator v1Iter = findOrAddVertex(v1); SurfaceVertexIterator v1Iter = findOrAddVertex(v1);
@ -91,17 +91,17 @@ namespace Ogre
const_cast<SurfaceEdge&>(*v2v0Iter).setTriangle(iterTriangle); const_cast<SurfaceEdge&>(*v2v0Iter).setTriangle(iterTriangle);
} }
SurfaceVertexIterator SurfacePatch::findOrAddVertex(const SurfaceVertex& vertex) SurfaceVertexIterator HalfEdgeSurfacePatch::findOrAddVertex(const SurfaceVertex& vertex)
{ {
return m_listVertices.insert(vertex).first; return m_listVertices.insert(vertex).first;
} }
SurfaceEdgeIterator SurfacePatch::findEdge(const SurfaceVertexIterator& source, const SurfaceVertexIterator& target) SurfaceEdgeIterator HalfEdgeSurfacePatch::findEdge(const SurfaceVertexIterator& source, const SurfaceVertexIterator& target)
{ {
return m_listEdges.find(SurfaceEdge(target,source)); return m_listEdges.find(SurfaceEdge(target,source));
} }
SurfaceEdgeIterator SurfacePatch::findOrAddEdge(const SurfaceVertexIterator& source, const SurfaceVertexIterator& target) SurfaceEdgeIterator HalfEdgeSurfacePatch::findOrAddEdge(const SurfaceVertexIterator& source, const SurfaceVertexIterator& target)
{ {
SurfaceEdge edge(target,source); SurfaceEdge edge(target,source);
std::pair<SurfaceEdgeIterator, bool> insertionResult = m_listEdges.insert(edge); std::pair<SurfaceEdgeIterator, bool> insertionResult = m_listEdges.insert(edge);
@ -122,7 +122,7 @@ namespace Ogre
return edgeIter; return edgeIter;
} }
bool SurfacePatch::canRemoveVertexFrom(SurfaceVertexIterator vertexIter, std::list<SurfaceVertexIterator> listConnectedIter, bool isEdge) bool HalfEdgeSurfacePatch::canRemoveVertexFrom(SurfaceVertexIterator vertexIter, std::list<SurfaceVertexIterator> listConnectedIter, bool isEdge)
{ {
bool allXMatch = true; bool allXMatch = true;
bool allYMatch = true; bool allYMatch = true;
@ -172,7 +172,7 @@ namespace Ogre
&& (twoEdgesMatch); && (twoEdgesMatch);
} }
std::list<SurfaceVertexIterator> SurfacePatch::findConnectedVertices(SurfaceVertexIterator vertexIter, bool& isEdge) std::list<SurfaceVertexIterator> HalfEdgeSurfacePatch::findConnectedVertices(SurfaceVertexIterator vertexIter, bool& isEdge)
{ {
isEdge = false; isEdge = false;
std::list<SurfaceVertexIterator> result; std::list<SurfaceVertexIterator> result;
@ -234,7 +234,7 @@ namespace Ogre
return result; return result;
} }
uint SurfacePatch::decimate(void) uint HalfEdgeSurfacePatch::decimate(void)
{ {
uint uNoRemoved = 0; uint uNoRemoved = 0;
//LogManager::getSingleton().logMessage("\n\nPerforming decimation"); //LogManager::getSingleton().logMessage("\n\nPerforming decimation");
@ -333,7 +333,7 @@ namespace Ogre
return uNoRemoved; return uNoRemoved;
} }
void SurfacePatch::triangulate(std::list<SurfaceVertexIterator> listVertices) void HalfEdgeSurfacePatch::triangulate(std::list<SurfaceVertexIterator> listVertices)
{ {
std::list<SurfaceVertexIterator>::iterator v0IterIter = listVertices.begin(); std::list<SurfaceVertexIterator>::iterator v0IterIter = listVertices.begin();
std::list<SurfaceVertexIterator>::iterator v1IterIter = listVertices.begin(); std::list<SurfaceVertexIterator>::iterator v1IterIter = listVertices.begin();
@ -354,7 +354,7 @@ namespace Ogre
} }
} }
bool SurfacePatch::isPolygonConvex(std::list<SurfaceVertexIterator> listVertices, Vector3 normal) bool HalfEdgeSurfacePatch::isPolygonConvex(std::list<SurfaceVertexIterator> listVertices, Vector3 normal)
{ {
std::list<SurfaceVertexIterator>::iterator v0IterIter = listVertices.begin(); std::list<SurfaceVertexIterator>::iterator v0IterIter = listVertices.begin();
std::list<SurfaceVertexIterator>::iterator v1IterIter = listVertices.begin(); std::list<SurfaceVertexIterator>::iterator v1IterIter = listVertices.begin();
@ -387,7 +387,7 @@ namespace Ogre
return true; return true;
} }
void SurfacePatch::fillVertexAndIndexData(std::vector<SurfaceVertex>& vecVertices, std::vector<ushort>& vecIndices) void HalfEdgeSurfacePatch::fillVertexAndIndexData(std::vector<SurfaceVertex>& vecVertices, std::vector<ushort>& vecIndices)
{ {
vecVertices.resize(m_listVertices.size()); vecVertices.resize(m_listVertices.size());
std::copy(m_listVertices.begin(), m_listVertices.end(), vecVertices.begin()); std::copy(m_listVertices.begin(), m_listVertices.end(), vecVertices.begin());

View File

@ -0,0 +1,38 @@
#include "IndexedSurfacePatch.h"
namespace Ogre
{
IndexedSurfacePatch::IndexedSurfacePatch()
{
}
IndexedSurfacePatch::~IndexedSurfacePatch()
{
}
void IndexedSurfacePatch::addTriangle(const SurfaceVertex& v0,const SurfaceVertex& v1,const SurfaceVertex& v2)
{
SurfaceVertexIterator v0Iter = m_listVertices.insert(v0).first;
SurfaceVertexIterator v1Iter = m_listVertices.insert(v1).first;
SurfaceVertexIterator v2Iter = m_listVertices.insert(v2).first;
m_vecTriangleIndices.push_back(v0Iter);
m_vecTriangleIndices.push_back(v1Iter);
m_vecTriangleIndices.push_back(v2Iter);
}
void IndexedSurfacePatch::fillVertexAndIndexData(std::vector<SurfaceVertex>& vecVertices, std::vector<ushort>& vecIndices)
{
vecVertices.resize(m_listVertices.size());
std::copy(m_listVertices.begin(), m_listVertices.end(), vecVertices.begin());
//vecIndices.resize(m_listTriangles.size() * 3);
for(std::vector<SurfaceVertexIterator>::iterator iterVertices = m_vecTriangleIndices.begin(); iterVertices != m_vecTriangleIndices.end(); ++iterVertices)
{
std::vector<SurfaceVertex>::iterator iterVertex;
iterVertex = lower_bound(vecVertices.begin(), vecVertices.end(), **iterVertices);
vecIndices.push_back(iterVertex - vecVertices.begin());
}
}
}

View File

@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "SurfaceVertex.h" #include "SurfaceVertex.h"
#include "SurfaceEdge.h" #include "SurfaceEdge.h"
#include "HalfEdgeSurfacePatch.h" #include "HalfEdgeSurfacePatch.h"
#include "IndexedSurfacePatch.h"
#include "PolyVoxSceneManager.h" #include "PolyVoxSceneManager.h"
#include "VolumeIterator.h" #include "VolumeIterator.h"
#include "VolumeManager.h" #include "VolumeManager.h"
@ -622,15 +623,15 @@ namespace Ogre
if(surfacePatchMapResult.find(material0) == surfacePatchMapResult.end()) if(surfacePatchMapResult.find(material0) == surfacePatchMapResult.end())
{ {
surfacePatchMapResult.insert(std::make_pair(material0,new SurfacePatch)); surfacePatchMapResult.insert(std::make_pair(material0,new IndexedSurfacePatch));
} }
if(surfacePatchMapResult.find(material1) == surfacePatchMapResult.end()) if(surfacePatchMapResult.find(material1) == surfacePatchMapResult.end())
{ {
surfacePatchMapResult.insert(std::make_pair(material1,new SurfacePatch)); surfacePatchMapResult.insert(std::make_pair(material1,new IndexedSurfacePatch));
} }
if(surfacePatchMapResult.find(material2) == surfacePatchMapResult.end()) if(surfacePatchMapResult.find(material2) == surfacePatchMapResult.end())
{ {
surfacePatchMapResult.insert(std::make_pair(material2,new SurfacePatch)); surfacePatchMapResult.insert(std::make_pair(material2,new IndexedSurfacePatch));
} }
SurfaceVertex surfaceVertex0Alpha1(vertex0,1.0); SurfaceVertex surfaceVertex0Alpha1(vertex0,1.0);

View File

@ -51,6 +51,9 @@ namespace Ogre
std::vector<ushort> vecIndices; std::vector<ushort> vecIndices;
patchToRender->fillVertexAndIndexData(vecVertices,vecIndices); patchToRender->fillVertexAndIndexData(vecVertices,vecIndices);
LogManager::getSingleton().logMessage("No of Vertices = " + StringConverter::toString(vecVertices.size()));
LogManager::getSingleton().logMessage("No of Indices = " + StringConverter::toString(vecIndices.size()));
//Initialization stuff //Initialization stuff
mRenderOp.vertexData->vertexCount = vecVertices.size(); mRenderOp.vertexData->vertexCount = vecVertices.size();
mRenderOp.indexData->indexCount = vecIndices.size(); mRenderOp.indexData->indexCount = vecIndices.size();