From 2657d5ba6ffb756bd8e659f6b2e42bb1d6b8eadb Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 11 Jun 2008 21:49:46 +0000 Subject: [PATCH] Some tidying up op OpenGL example. --- examples/OpenGL/main.cpp | 53 ++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index 6f1a01c8..d56b9d45 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -16,8 +16,14 @@ using namespace std; //Global variables are easier for demonstration purposes, especially //as I'm not sure how/if I can pass variables to the GLUT functions. const uint16_t g_uVolumeSideLength = 128; -BlockVolume g_volData(logBase2(g_uVolumeSideLength)); //Creates a volume 128x128x128 -IndexedSurfacePatch* g_ispRegionSurfaces[8][8][8]; +const uint16_t g_uRegionSideLength = 16; +const uint16_t g_uVolumeSideLengthInRegions = g_uVolumeSideLength / g_uRegionSideLength; + +//Creates a volume 128x128x128 +BlockVolume g_volData(logBase2(g_uVolumeSideLength)); + +//Rather than storing one big mesh, the volume is broken into regions and a mesh is stored for each region +IndexedSurfacePatch* g_ispRegionSurfaces[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; void createSphereInVolume(void) { @@ -70,19 +76,21 @@ void display ( void ) // Create The Display Function glTranslatef(-g_uVolumeSideLength/2,-g_uVolumeSideLength/2,-200.0f); glBegin(GL_TRIANGLES); - for(uint16_t z = 0; z < 8; ++z) + for(uint16_t uRegionZ = 0; uRegionZ < g_uVolumeSideLengthInRegions; ++uRegionZ) { - for(uint16_t y = 0; y < 8; ++y) + for(uint16_t uRegionY = 0; uRegionY < g_uVolumeSideLengthInRegions; ++uRegionY) { - for(uint16_t x = 0; x < 8; ++x) + for(uint16_t uRegionX = 0; uRegionX < g_uVolumeSideLengthInRegions; ++uRegionX) { - const vector& vertices = g_ispRegionSurfaces[x][y][z]->getVertices(); - const vector& indices = g_ispRegionSurfaces[x][y][z]->getIndices(); - for(vector::const_iterator indexIter = indices.begin(); indexIter != indices.end(); ++indexIter) + const vector& vecVertices = g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]->getVertices(); + const vector& vecIndices = g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]->getIndices(); + for(vector::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex) { - const SurfaceVertex& vertex = vertices[*indexIter]; - const Vector3DFloat& vertexPos = vertex.getPosition(); - glVertex3f(vertexPos.getX() + (x*16), vertexPos.getY() + (y*16), vertexPos.getZ() + (z*16)); + const SurfaceVertex& vertex = vecVertices[*iterIndex]; + const Vector3DFloat& v3dVertexPos = vertex.getPosition(); + const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength); + const Vector3DFloat v3dFinalVertexPos = v3dVertexPos + v3dRegionOffset; + glVertex3f(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ()); } } } @@ -136,14 +144,17 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al { createSphereInVolume(); - for(uint16_t z = 0; z < 8; ++z) + for(uint16_t uRegionZ = 0; uRegionZ < g_uVolumeSideLengthInRegions; ++uRegionZ) { - for(uint16_t y = 0; y < 8; ++y) + for(uint16_t uRegionY = 0; uRegionY < g_uVolumeSideLengthInRegions; ++uRegionY) { - for(uint16_t x = 0; x < 8; ++x) + for(uint16_t uRegionX = 0; uRegionX < g_uVolumeSideLengthInRegions; ++uRegionX) { - g_ispRegionSurfaces[x][y][z] = new IndexedSurfacePatch(false); - generateReferenceMeshDataForRegion(&g_volData, Region(Vector3DInt32(x * 16, y * 16, z * 16), Vector3DInt32((x+1) * 16, (y+1) * 16, (z+1) * 16)), g_ispRegionSurfaces[x][y][z]); + g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ] = new IndexedSurfacePatch(false); + IndexedSurfacePatch* ispCurrent = g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]; + Vector3DInt32 regLowerCorner(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength); + Vector3DInt32 regUpperCorner((uRegionX + 1) * g_uRegionSideLength, (uRegionY + 1) * g_uRegionSideLength, (uRegionZ + 1) * g_uRegionSideLength); + generateReferenceMeshDataForRegion(&g_volData, Region(regLowerCorner, regUpperCorner), ispCurrent); } } } @@ -152,7 +163,7 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al init (); glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode glutInitWindowSize ( 500, 500 ); // If glutFullScreen wasn't called this is the window size - glutCreateWindow ( "NeHe's OpenGL Framework" ); // Window Title (argv[0] for current directory as title) + glutCreateWindow ( "PolyVox OpenGL Example" ); // Window Title (argv[0] for current directory as title) //glutFullScreen ( ); // Put Into Full Screen glutDisplayFunc ( display ); // Matching Earlier Functions To Their Counterparts glutReshapeFunc ( reshape ); @@ -160,13 +171,13 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al glutSpecialFunc ( arrow_keys ); glutMainLoop ( ); // Initialize The Main Loop - for(uint16_t z = 0; z < 8; ++z) + for(uint16_t uRegionZ = 0; uRegionZ < g_uVolumeSideLengthInRegions; ++uRegionZ) { - for(uint16_t y = 0; y < 8; ++y) + for(uint16_t uRegionY = 0; uRegionY < g_uVolumeSideLengthInRegions; ++uRegionY) { - for(uint16_t x = 0; x < 8; ++x) + for(uint16_t uRegionX = 0; uRegionX < g_uVolumeSideLengthInRegions; ++uRegionX) { - delete g_ispRegionSurfaces[x][y][z]; + delete g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]; } } }