From 74292f7a62f4d2e7c22fa2b826f29ebc81a82269 Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 18 Mar 2009 22:48:44 +0000 Subject: [PATCH] Work on new OpenGL example - Splitting into several files. --- .../OpenGL/OpenGLImmediateModeSupport.cpp | 68 ++++++------------- examples/OpenGL/OpenGLSupport.cpp | 39 +++++++++++ examples/OpenGL/OpenGLSupport.h | 17 +++++ .../OpenGLVertexBufferObjectSupport.cpp | 40 ++--------- examples/OpenGL/main.cpp | 32 ++++----- 5 files changed, 96 insertions(+), 100 deletions(-) diff --git a/examples/OpenGL/OpenGLImmediateModeSupport.cpp b/examples/OpenGL/OpenGLImmediateModeSupport.cpp index be795260..d3a06d3c 100644 --- a/examples/OpenGL/OpenGLImmediateModeSupport.cpp +++ b/examples/OpenGL/OpenGLImmediateModeSupport.cpp @@ -1,4 +1,5 @@ #include "OpenGLImmediateModeSupport.h" +#include "OpenGLSupport.h" #include "PolyVoxCore/IndexedSurfacePatch.h" @@ -8,59 +9,28 @@ using namespace std; void renderRegionImmediateMode(PolyVox::IndexedSurfacePatch& isp) { const vector& vecVertices = isp.getVertices(); - const vector& vecIndices = isp.getIndices(); + 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); + 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; - } + uint8 material = vertex.getMaterial() + 0.5; - glColor3f(red, green, blue); - glNormal3f(vertex.getNormal().getX(), vertex.getNormal().getY(), vertex.getNormal().getZ()); - glVertex3f(v3dFinalVertexPos.getX(), v3dFinalVertexPos.getY(), v3dFinalVertexPos.getZ()); - - - } - glEnd(); + OpenGLColour colour = convertMaterialIDToColour(material); + + glColor3f(colour.red, colour.green, colour.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/OpenGLSupport.cpp b/examples/OpenGL/OpenGLSupport.cpp index e69de29b..8801ae93 100644 --- a/examples/OpenGL/OpenGLSupport.cpp +++ b/examples/OpenGL/OpenGLSupport.cpp @@ -0,0 +1,39 @@ +#include "OpenGLSupport.h" + +using namespace PolyVox; + +OpenGLColour convertMaterialIDToColour(PolyVox::uint8 materialID) +{ + OpenGLColour colour; + + switch(materialID) + { + case 1: + colour.red = 1.0; + colour.green = 0.0; + colour.blue = 0.0; + break; + case 2: + colour.red = 0.0; + colour.green = 1.0; + colour.blue = 0.0; + break; + case 3: + colour.red = 0.0; + colour.green = 0.0; + colour.blue = 1.0; + break; + case 4: + colour.red = 1.0; + colour.green = 1.0; + colour.blue = 0.0; + break; + case 5: + colour.red = 1.0; + colour.green = 0.0; + colour.blue = 1.0; + break; + } + + return colour; +} \ No newline at end of file diff --git a/examples/OpenGL/OpenGLSupport.h b/examples/OpenGL/OpenGLSupport.h index e69de29b..5ecad4e1 100644 --- a/examples/OpenGL/OpenGLSupport.h +++ b/examples/OpenGL/OpenGLSupport.h @@ -0,0 +1,17 @@ +#ifndef __OpenGLExample_OpenGLSupport_H__ +#define __OpenGLExample_OpenGLSupport_H__ + +#include "PolyVoxCore/PolyVoxForwardDeclarations.h" + +#include "glew/glew.h" + +struct OpenGLColour +{ + GLfloat red; + GLfloat green; + GLfloat blue; +}; + +OpenGLColour convertMaterialIDToColour(PolyVox::uint8 materialID); + +#endif //__OpenGLExample_OpenGLSupport_H__ \ No newline at end of file diff --git a/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp b/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp index d3099e96..4a76c295 100644 --- a/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp +++ b/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp @@ -1,3 +1,4 @@ +#include "OpenGLSupport.h" #include "OpenGLVertexBufferObjectSupport.h" #include "PolyVoxCore/IndexedSurfacePatch.h" @@ -49,46 +50,15 @@ OpenGLSurfacePatch BuildOpenGLSurfacePatch(IndexedSurfacePatch& isp) *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; - } + OpenGLColour colour = convertMaterialIDToColour(material); - *ptr = red; + *ptr = colour.red; ptr++; - *ptr = green; + *ptr = colour.green; ptr++; - *ptr = blue; + *ptr = colour.blue; ptr++; } diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index 919de584..bd2000ff 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -12,9 +12,9 @@ #define USE_OPENGL_VERTEX_BUFFERS_OBJECTS #ifdef WIN32 - #include "glew/glew.h" +#include "glew/glew.h" #else - #include // The GL Header File +#include // The GL Header File #endif #include // The GL Utility Toolkit (Glut) Header] @@ -78,7 +78,7 @@ void display ( void ) // Create The Display Function glMatrixMode ( GL_MODELVIEW ); // Select The Model View Matrix glLoadIdentity(); // Reset The Current Modelview Matrix - + //Moves the camera back so we can see the volume glTranslatef(0.0f, 0.0f, -100.0f); @@ -101,9 +101,9 @@ void display ( void ) // Create The Display Function } else { - IndexedSurfacePatch* ispCurrent = g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ]; - renderRegionImmediateMode(*ispCurrent); - + IndexedSurfacePatch* ispCurrent = g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ]; + renderRegionImmediateMode(*ispCurrent); + } } } @@ -183,7 +183,7 @@ default: void main ( int argc, char** argv ) // Create Main Function For Bringing It All Together { - g_bUseOpenGLVertexBufferObjects = false; + 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 @@ -199,13 +199,13 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al if(g_bUseOpenGLVertexBufferObjects) { #ifdef WIN32 - //If we are on Windows we will need GLEW to access recent OpenGL functionality - GLenum err = glewInit(); - if (GLEW_OK != err) - { - /* Problem: glewInit failed, something is seriously wrong. */ - cout << "Error: " << glewGetErrorString(err) << endl; - } + //If we are on Windows we will need GLEW to access recent OpenGL functionality + GLenum err = glewInit(); + if (GLEW_OK != err) + { + /* Problem: glewInit failed, something is seriously wrong. */ + cout << "Error: " << glewGetErrorString(err) << endl; + } #endif } @@ -257,11 +257,11 @@ void main ( int argc, char** argv ) // Create Main Function For Bringing It Al if(g_bUseOpenGLVertexBufferObjects) { - g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ] = BuildOpenGLSurfacePatch(*ispCurrent); + g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ] = BuildOpenGLSurfacePatch(*ispCurrent); } else { - g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ] = ispCurrent; + g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ] = ispCurrent; } //delete ispCurrent; }