Initial work on progressive mesh generation. Currently based on Stan Melax's PolyChop.

This commit is contained in:
David Williams
2009-10-20 22:02:58 +00:00
parent 5eb538e925
commit c695a7bc86
16 changed files with 1006 additions and 26 deletions

View File

@ -6,15 +6,19 @@
using namespace PolyVox;
using namespace std;
void renderRegionImmediateMode(PolyVox::IndexedSurfacePatch& isp)
void renderRegionImmediateMode(PolyVox::IndexedSurfacePatch& isp, unsigned int uLodLevel)
{
const vector<SurfaceVertex>& vecVertices = isp.getVertices();
const vector<PolyVox::uint32_t>& vecIndices = isp.getIndices();
int beginIndex = isp.m_vecLodRecords[uLodLevel].beginIndex;
int endIndex = isp.m_vecLodRecords[uLodLevel].endIndex;
glBegin(GL_TRIANGLES);
for(vector<PolyVox::uint32_t>::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex)
//for(vector<PolyVox::uint32_t>::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex)
for(int index = beginIndex; index < endIndex; ++index)
{
const SurfaceVertex& vertex = vecVertices[*iterIndex];
const SurfaceVertex& vertex = vecVertices[vecIndices[index]];
const Vector3DFloat& v3dVertexPos = vertex.getPosition();
//const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength);
const Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast<Vector3DFloat>(isp.m_Region.getLowerCorner());

View File

@ -5,6 +5,6 @@
#include "glew/glew.h"
void renderRegionImmediateMode(PolyVox::IndexedSurfacePatch& isp);
void renderRegionImmediateMode(PolyVox::IndexedSurfacePatch& isp, unsigned int uLodLevel);
#endif //__OpenGLExample_OpenGLImmediateModeSupport_H__

View File

@ -11,6 +11,9 @@ OpenGLSurfacePatch BuildOpenGLSurfacePatch(const IndexedSurfacePatch& isp)
//Represents our filled in OpenGL vertex and index buffer objects.
OpenGLSurfacePatch result;
//The source
result.sourceISP = &isp;
//Convienient access to the vertices and indices
const vector<SurfaceVertex>& vecVertices = isp.getVertices();
const vector<PolyVox::uint32_t>& vecIndices = isp.getIndices();
@ -74,8 +77,10 @@ OpenGLSurfacePatch BuildOpenGLSurfacePatch(const IndexedSurfacePatch& isp)
return result;
}
void renderRegionVertexBufferObject(const OpenGLSurfacePatch& openGLSurfacePatch)
void renderRegionVertexBufferObject(const OpenGLSurfacePatch& openGLSurfacePatch, unsigned int uLodLevel)
{
int beginIndex = openGLSurfacePatch.sourceISP->m_vecLodRecords[uLodLevel].beginIndex;
int endIndex = openGLSurfacePatch.sourceISP->m_vecLodRecords[uLodLevel].endIndex;
glBindBuffer(GL_ARRAY_BUFFER, openGLSurfacePatch.vertexBuffer);
glVertexPointer(3, GL_FLOAT, 36, 0);
glNormalPointer(GL_FLOAT, 36, (GLvoid*)12);
@ -87,7 +92,8 @@ void renderRegionVertexBufferObject(const OpenGLSurfacePatch& openGLSurfacePatch
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLES, openGLSurfacePatch.noOfIndices, GL_UNSIGNED_INT, 0);
//glDrawElements(GL_TRIANGLES, openGLSurfacePatch.noOfIndices, GL_UNSIGNED_INT, 0);
glDrawRangeElements(GL_TRIANGLES, beginIndex, endIndex-1, endIndex - beginIndex,/* openGLSurfacePatch.noOfIndices,*/ GL_UNSIGNED_INT, 0);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

View File

@ -10,9 +10,10 @@ struct OpenGLSurfacePatch
GLulong noOfIndices;
GLuint indexBuffer;
GLuint vertexBuffer;
const PolyVox::IndexedSurfacePatch* sourceISP;
};
OpenGLSurfacePatch BuildOpenGLSurfacePatch(const PolyVox::IndexedSurfacePatch& isp);
void renderRegionVertexBufferObject(const OpenGLSurfacePatch& openGLSurfacePatch);
void renderRegionVertexBufferObject(const OpenGLSurfacePatch& openGLSurfacePatch, unsigned int uLodLevel);
#endif //__OpenGLExample_OpenGLVertexBufferObjectSupport_H__

View File

@ -16,7 +16,7 @@ OpenGLWidget::OpenGLWidget(QWidget *parent)
{
m_xRotation = 0;
m_yRotation = 0;
m_uRegionSideLength = 32.0f;
m_uRegionSideLength = 32;
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
@ -72,18 +72,23 @@ void OpenGLWidget::setVolume(PolyVox::Volume<PolyVox::uint8_t>* volData)
//isp->smooth(0.3f);
//ispCurrent->generateAveragedFaceNormals(true);
if(isp->m_vecTriangleIndices.size() > 0)
{
isp->makeProgressiveMesh();
Vector3DUint8 v3dRegPos(uRegionX,uRegionY,uRegionZ);
if(m_bUseOpenGLVertexBufferObjects)
{
OpenGLSurfacePatch openGLSurfacePatch = BuildOpenGLSurfacePatch(*(isp.get()));
m_mapOpenGLSurfacePatches.insert(make_pair(v3dRegPos, openGLSurfacePatch));
Vector3DUint8 v3dRegPos(uRegionX,uRegionY,uRegionZ);
if(m_bUseOpenGLVertexBufferObjects)
{
OpenGLSurfacePatch openGLSurfacePatch = BuildOpenGLSurfacePatch(*(isp.get()));
m_mapOpenGLSurfacePatches.insert(make_pair(v3dRegPos, openGLSurfacePatch));
}
//else
//{
m_mapIndexedSurfacePatches.insert(make_pair(v3dRegPos, isp));
//}
//delete ispCurrent;
}
else
{
m_mapIndexedSurfacePatches.insert(make_pair(v3dRegPos, isp));
}
//delete ispCurrent;
}
}
}
@ -161,15 +166,18 @@ void OpenGLWidget::paintGL()
for(PolyVox::uint16_t uRegionX = 0; uRegionX < m_uVolumeWidthInRegions; ++uRegionX)
{
Vector3DUint8 v3dRegPos(uRegionX,uRegionY,uRegionZ);
if(m_bUseOpenGLVertexBufferObjects)
{
renderRegionVertexBufferObject(m_mapOpenGLSurfacePatches[v3dRegPos]);
}
else
if(m_mapIndexedSurfacePatches.find(v3dRegPos) != m_mapIndexedSurfacePatches.end())
{
POLYVOX_SHARED_PTR<IndexedSurfacePatch> ispCurrent = m_mapIndexedSurfacePatches[v3dRegPos];
renderRegionImmediateMode(*ispCurrent);
unsigned int uLodLevel = 0; //ispCurrent->m_vecLodRecords.size() - 1;
if(m_bUseOpenGLVertexBufferObjects)
{
renderRegionVertexBufferObject(m_mapOpenGLSurfacePatches[v3dRegPos], uLodLevel);
}
else
{
renderRegionImmediateMode(*ispCurrent, uLodLevel);
}
}
}
}

View File

@ -89,6 +89,7 @@ int main(int argc, char *argv[])
//createCubeInVolume(volData, Vector3DUint16(1, 1, 1), Vector3DUint16(maxPos-1, maxPos-1, midPos/4), 255);
volData.calculateSizeInChars();
cout << "Tidying memory...";
volData.tidyUpMemory(0);
cout << "done." << endl;