diff --git a/examples/OpenGL/CMakeLists.txt b/examples/OpenGL/CMakeLists.txt index 86dadc73..4126e48a 100644 --- a/examples/OpenGL/CMakeLists.txt +++ b/examples/OpenGL/CMakeLists.txt @@ -6,7 +6,10 @@ PROJECT(OpenGLExample) SET(SRC_FILES glew/glew.c main.cpp - shapes.cpp + OpenGLImmediateModeSupport.cpp + OpenGLSupport.cpp + OpenGLVertexBufferObjectSupport.cpp + Shapes.cpp ) #Projects headers files @@ -14,7 +17,10 @@ SET(INC_FILES glew/glew.h glew/glxew.h glew/wglew.h - shapes.h + OpenGLImmediateModeSupport.h + OpenGLSupport.h + OpenGLVertexBufferObjectSupport.h + Shapes.h ) ADD_DEFINITIONS(-DGLEW_STATIC) diff --git a/examples/OpenGL/OpenGLImmediateModeSupport.cpp b/examples/OpenGL/OpenGLImmediateModeSupport.cpp new file mode 100644 index 00000000..d5d2d820 --- /dev/null +++ b/examples/OpenGL/OpenGLImmediateModeSupport.cpp @@ -0,0 +1,66 @@ +#include "OpenGLImmediateModeSupport.h" + +#include "PolyVoxCore/IndexedSurfacePatch.h" + +using namespace PolyVox; +using namespace std; + +void renderRegion(PolyVox::IndexedSurfacePatch& isp) +{ + const vector& vecVertices = isp.getVertices(); + const vector& vecIndices = isp.getIndices(); + + glBegin(GL_TRIANGLES); + for(vector::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex) + { + 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 + static_cast(isp.m_v3dRegionPosition); + + + + + GLfloat red = 0.0f; + GLfloat green = 0.0f; + GLfloat blue = 0.0f; + + uint8 material = vertex.getMaterial() + 0.5; + + switch(material) + { + case 1: + red = 1.0; + green = 0.0; + blue = 0.0; + break; + case 2: + red = 0.0; + green = 1.0; + blue = 0.0; + break; + case 3: + red = 0.0; + green = 0.0; + blue = 1.0; + break; + case 4: + red = 1.0; + green = 1.0; + blue = 0.0; + break; + case 5: + red = 1.0; + green = 0.0; + blue = 1.0; + break; + } + + glColor3f(red, green, blue); + glNormal3f(vertex.getNormal().getX(), vertex.getNormal().getY(), vertex.getNormal().getZ()); + glVertex3f(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ()); + + + } + glEnd(); +} \ No newline at end of file diff --git a/examples/OpenGL/OpenGLImmediateModeSupport.h b/examples/OpenGL/OpenGLImmediateModeSupport.h new file mode 100644 index 00000000..bd60a3e0 --- /dev/null +++ b/examples/OpenGL/OpenGLImmediateModeSupport.h @@ -0,0 +1,10 @@ +#ifndef __OpenGLExample_OpenGLImmediateModeSupport_H__ +#define __OpenGLExample_OpenGLImmediateModeSupport_H__ + +#include "PolyVoxCore/PolyVoxForwardDeclarations.h" + +#include "glew/glew.h" + +void renderRegion(PolyVox::IndexedSurfacePatch& isp); + +#endif //__OpenGLExample_OpenGLImmediateModeSupport_H__ diff --git a/examples/OpenGL/OpenGLSupport.cpp b/examples/OpenGL/OpenGLSupport.cpp new file mode 100644 index 00000000..e69de29b diff --git a/examples/OpenGL/OpenGLSupport.h b/examples/OpenGL/OpenGLSupport.h new file mode 100644 index 00000000..e69de29b diff --git a/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp b/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp new file mode 100644 index 00000000..56af1818 --- /dev/null +++ b/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp @@ -0,0 +1,98 @@ +#include "OpenGLVertexBufferObjectSupport.h" + +#include "PolyVoxCore/IndexedSurfacePatch.h" + +using namespace PolyVox; +using namespace std; + +OpenGLSurfacePatch BuildOpenGLSurfacePatch(IndexedSurfacePatch& isp) +{ + OpenGLSurfacePatch result; + + const vector& vecVertices = isp.getVertices(); + const vector& vecIndices = isp.getIndices(); + + glGenBuffers(1, &result.indexBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.indexBuffer); + int s = vecIndices.size() * sizeof(GLint); + if(s != 0) + { + GLvoid* blah = (GLvoid*)(&(vecIndices[0])); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, s, blah, GL_STATIC_DRAW); + } + + result.noOfIndices = vecIndices.size(); + + glGenBuffers(1, &result.vertexBuffer); + glBindBuffer(GL_ARRAY_BUFFER, result.vertexBuffer); + glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(GLfloat) * 9, 0, GL_STATIC_DRAW); + GLfloat* ptr = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); + + for(vector::const_iterator iterVertex = vecVertices.begin(); iterVertex != vecVertices.end(); ++iterVertex) + { + const SurfaceVertex& vertex = *iterVertex; + const Vector3DFloat& v3dVertexPos = vertex.getPosition(); + //const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength); + const Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast(isp.m_v3dRegionPosition); + + *ptr = v3dFinalVertexPos.getX(); + ptr++; + *ptr = v3dFinalVertexPos.getY(); + ptr++; + *ptr = v3dFinalVertexPos.getZ(); + ptr++; + + *ptr = vertex.getNormal().getX(); + ptr++; + *ptr = vertex.getNormal().getY(); + ptr++; + *ptr = vertex.getNormal().getZ(); + ptr++; + + GLfloat red = 0.0f; + GLfloat green = 0.0f; + GLfloat blue = 0.0f; + + uint8 material = vertex.getMaterial() + 0.5; + + switch(material) + { + case 1: + red = 1.0; + green = 0.0; + blue = 0.0; + break; + case 2: + red = 0.0; + green = 1.0; + blue = 0.0; + break; + case 3: + red = 0.0; + green = 0.0; + blue = 1.0; + break; + case 4: + red = 1.0; + green = 1.0; + blue = 0.0; + break; + case 5: + red = 1.0; + green = 0.0; + blue = 1.0; + break; + } + + *ptr = red; + ptr++; + *ptr = green; + ptr++; + *ptr = blue; + ptr++; + } + + glUnmapBuffer(GL_ARRAY_BUFFER); + + return result; +} \ No newline at end of file diff --git a/examples/OpenGL/OpenGLVertexBufferObjectSupport.h b/examples/OpenGL/OpenGLVertexBufferObjectSupport.h new file mode 100644 index 00000000..9d3d9a67 --- /dev/null +++ b/examples/OpenGL/OpenGLVertexBufferObjectSupport.h @@ -0,0 +1,17 @@ +#ifndef __OpenGLExample_OpenGLVertexBufferObjectSupport_H__ +#define __OpenGLExample_OpenGLVertexBufferObjectSupport_H__ + +#include "PolyVoxCore/PolyVoxForwardDeclarations.h" + +#include "glew/glew.h" + +struct OpenGLSurfacePatch +{ + GLulong noOfIndices; + GLuint indexBuffer; + GLuint vertexBuffer; +}; + +OpenGLSurfacePatch BuildOpenGLSurfacePatch(PolyVox::IndexedSurfacePatch& isp); + +#endif //__OpenGLExample_OpenGLVertexBufferObjectSupport_H__ diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index eb5de17d..fefbf2e6 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -3,41 +3,28 @@ #include "PolyVoxCore/SurfaceExtractors.h" #include "PolyVoxCore/Utility.h" +#include "OpenGLImmediateModeSupport.h" +#include "OpenGLVertexBufferObjectSupport.h" #include "Shapes.h" #include // Standard Header For Most Programs #define USE_OPENGL_VERTEX_BUFFERS_OBJECTS -#ifdef USE_OPENGL_VERTEX_BUFFERS_OBJECTS - #ifdef WIN32 - #include "glew/glew.h" - #else - #include // The GL Header File - #endif - #include // The GL Utility Toolkit (Glut) Header] +#ifdef WIN32 + #include "glew/glew.h" #else - #include - #include + #include // The GL Header File #endif +#include // The GL Utility Toolkit (Glut) Header] #include - //Some namespaces we need using namespace std; using namespace PolyVox; using namespace std; -#ifdef USE_OPENGL_VERTEX_BUFFERS_OBJECTS -struct OpenGLSurfacePatch -{ - GLulong noOfIndices; - GLuint indexBuffer; - GLuint vertexBuffer; -}; -#endif - //Global variables are easier for demonstration purposes, especially //as I'm not sure how/if I can pass variables to the GLUT functions. //Global variables are denoted by the 'g_' prefix @@ -52,111 +39,14 @@ int g_yOld = 0; int g_frameCounter = 0; +bool g_bUseOpenGLVertexBufferObjects; + //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 -#ifdef USE_OPENGL_VERTEX_BUFFERS_OBJECTS - OpenGLSurfacePatch g_openGLSurfacePatches[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; -#else - IndexedSurfacePatch* g_indexedSurfacePatches[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; -#endif - -#ifdef USE_OPENGL_VERTEX_BUFFERS_OBJECTS -OpenGLSurfacePatch BuildOpenGLSurfacePatch(IndexedSurfacePatch& isp) -{ - OpenGLSurfacePatch result; - - const vector& vecVertices = isp.getVertices(); - const vector& vecIndices = isp.getIndices(); - - glGenBuffers(1, &result.indexBuffer); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.indexBuffer); - int s = vecIndices.size() * sizeof(GLint); - if(s != 0) - { - GLvoid* blah = (GLvoid*)(&(vecIndices[0])); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, s, blah, GL_STATIC_DRAW); - } - - result.noOfIndices = vecIndices.size(); - - glGenBuffers(1, &result.vertexBuffer); - glBindBuffer(GL_ARRAY_BUFFER, result.vertexBuffer); - glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(GLfloat) * 9, 0, GL_STATIC_DRAW); - GLfloat* ptr = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); - - for(vector::const_iterator iterVertex = vecVertices.begin(); iterVertex != vecVertices.end(); ++iterVertex) - { - const SurfaceVertex& vertex = *iterVertex; - const Vector3DFloat& v3dVertexPos = vertex.getPosition(); - //const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength); - const Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast(isp.m_v3dRegionPosition); - - *ptr = v3dFinalVertexPos.getX(); - ptr++; - *ptr = v3dFinalVertexPos.getY(); - ptr++; - *ptr = v3dFinalVertexPos.getZ(); - ptr++; - - *ptr = vertex.getNormal().getX(); - ptr++; - *ptr = vertex.getNormal().getY(); - ptr++; - *ptr = vertex.getNormal().getZ(); - ptr++; - - GLfloat red = 0.0f; - GLfloat green = 0.0f; - GLfloat blue = 0.0f; - - uint8 material = vertex.getMaterial() + 0.5; - - switch(material) - { - case 1: - red = 1.0; - green = 0.0; - blue = 0.0; - break; - case 2: - red = 0.0; - green = 1.0; - blue = 0.0; - break; - case 3: - red = 0.0; - green = 0.0; - blue = 1.0; - break; - case 4: - red = 1.0; - green = 1.0; - blue = 0.0; - break; - case 5: - red = 1.0; - green = 0.0; - blue = 1.0; - break; - } - - *ptr = red; - ptr++; - *ptr = green; - ptr++; - *ptr = blue; - ptr++; - } - - glUnmapBuffer(GL_ARRAY_BUFFER); - - return result; -} -#endif - - +OpenGLSurfacePatch g_openGLSurfacePatches[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; +IndexedSurfacePatch* g_indexedSurfacePatches[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; void timerFunc(int value) { @@ -205,7 +95,8 @@ void display ( void ) // Create The Display Function { for(uint16 uRegionX = 0; uRegionX < g_uVolumeSideLengthInRegions; ++uRegionX) { -#ifdef USE_OPENGL_VERTEX_BUFFERS_OBJECTS + if(g_bUseOpenGLVertexBufferObjects) + { glBindBuffer(GL_ARRAY_BUFFER, g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ].vertexBuffer); glVertexPointer(3, GL_FLOAT, 36, 0); glNormalPointer(GL_FLOAT, 36, (GLvoid*)12); @@ -222,67 +113,13 @@ void display ( void ) // Create The Display Function glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); - -#else - IndexedSurfacePatch* ispCurrent = g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ]; - - const vector& vecVertices = ispCurrent->getVertices(); - const vector& vecIndices = ispCurrent->getIndices(); - - glBegin(GL_TRIANGLES); - for(vector::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex) - { - 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 + static_cast(ispCurrent->m_v3dRegionPosition); - - - - - GLfloat red = 0.0f; - GLfloat green = 0.0f; - GLfloat blue = 0.0f; - - uint8 material = vertex.getMaterial() + 0.5; - - switch(material) - { - case 1: - red = 1.0; - green = 0.0; - blue = 0.0; - break; - case 2: - red = 0.0; - green = 1.0; - blue = 0.0; - break; - case 3: - red = 0.0; - green = 0.0; - blue = 1.0; - break; - case 4: - red = 1.0; - green = 1.0; - blue = 0.0; - break; - case 5: - red = 1.0; - green = 0.0; - blue = 1.0; - break; - } - - glColor3f(red, green, blue); - glNormal3f(vertex.getNormal().getX(), vertex.getNormal().getY(), vertex.getNormal().getZ()); - glVertex3f(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ()); - - } - glEnd(); -#endif + else + { + IndexedSurfacePatch* ispCurrent = g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ]; + renderRegion(*ispCurrent); + + } } } } @@ -361,6 +198,7 @@ default: void main ( int argc, char** argv ) // Create Main Function For Bringing It All Together { + g_bUseOpenGLVertexBufferObjects = true; glutInit ( &argc, argv ); // Erm Just Write It =) glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode glutInitWindowSize ( 500, 500 ); // If glutFullScreen wasn't called this is the window size @@ -373,7 +211,8 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al glutTimerFunc(1000, timerFunc, 0); glutIdleFunc(idle); -#ifdef USE_OPENGL_VERTEX_BUFFERS_OBJECTS + if(g_bUseOpenGLVertexBufferObjects) + { #ifdef WIN32 //If we are on Windows we will need GLEW to access recent OpenGL functionality GLenum err = glewInit(); @@ -383,7 +222,7 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al cout << "Error: " << glewGetErrorString(err) << endl; } #endif -#endif + } //Make our volume contain a sphere in the center. uint16 minPos = 0; @@ -430,12 +269,15 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al //Extract the surface for this region extractReferenceSurface(&g_volData, Region(regLowerCorner, regUpperCorner), ispCurrent); -#ifdef USE_OPENGL_VERTEX_BUFFERS_OBJECTS - g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ] = BuildOpenGLSurfacePatch(*ispCurrent); -#else - g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ] = ispCurrent; -#endif + if(g_bUseOpenGLVertexBufferObjects) + { + g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ] = BuildOpenGLSurfacePatch(*ispCurrent); + } + else + { + g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ] = ispCurrent; + } //delete ispCurrent; } }