Work on new OpenGL example - created separate function for converting IndexedSurfacePatches into OpenGL structures.
This commit is contained in:
		| @@ -6,9 +6,9 @@ | |||||||
| #include <windows.h>   // Standard Header For Most Programs | #include <windows.h>   // Standard Header For Most Programs | ||||||
|  |  | ||||||
| #ifdef WIN32 | #ifdef WIN32 | ||||||
| 	#include "glew/glew.h" | #include "glew/glew.h" | ||||||
| #else | #else | ||||||
| 	#include <gl/gl.h>     // The GL Header File | #include <gl/gl.h>     // The GL Header File | ||||||
| #endif | #endif | ||||||
| #include <gl/glut.h>   // The GL Utility Toolkit (Glut) Header | #include <gl/glut.h>   // The GL Utility Toolkit (Glut) Header | ||||||
|  |  | ||||||
| @@ -20,6 +20,12 @@ using namespace std; | |||||||
| using namespace PolyVox; | using namespace PolyVox; | ||||||
| using namespace std; | using namespace std; | ||||||
|  |  | ||||||
|  | struct OpenGLSurfacePatch | ||||||
|  | { | ||||||
|  | 	GLuint indexBuffer; | ||||||
|  | 	GLuint vertexBuffer; | ||||||
|  | }; | ||||||
|  |  | ||||||
| //Global variables are easier for demonstration purposes, especially | //Global variables are easier for demonstration purposes, especially | ||||||
| //as I'm not sure how/if I can pass variables to the GLUT functions. | //as I'm not sure how/if I can pass variables to the GLUT functions. | ||||||
| //Global variables are denoted by the 'g_' prefix | //Global variables are denoted by the 'g_' prefix | ||||||
| @@ -32,8 +38,92 @@ BlockVolume<uint8> 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 | //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]; | IndexedSurfacePatch* g_ispRegionSurfaces[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; | ||||||
| GLuint indexBuffers[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; | //GLuint indexBuffers[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; | ||||||
| GLuint vertexBuffers[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; | //GLuint vertexBuffers[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; | ||||||
|  | OpenGLSurfacePatch g_openGLSurfacePatches[g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions][g_uVolumeSideLengthInRegions]; | ||||||
|  |  | ||||||
|  | OpenGLSurfacePatch BuildOpenGLSurfacePatch(IndexedSurfacePatch& isp) | ||||||
|  | { | ||||||
|  | 	OpenGLSurfacePatch result; | ||||||
|  |  | ||||||
|  | 	const vector<SurfaceVertex>& vecVertices = isp.getVertices(); | ||||||
|  | 	const vector<uint32>& 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); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	glGenBuffers(1, &result.vertexBuffer); | ||||||
|  | 	glBindBuffer(GL_ARRAY_BUFFER, result.vertexBuffer); | ||||||
|  | 	glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(GLfloat) * 6, 0, GL_STATIC_DRAW); | ||||||
|  | 	GLfloat* ptr = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); | ||||||
|  |  | ||||||
|  | 	for(vector<SurfaceVertex>::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<Vector3DFloat>(isp.m_v3dRegionPosition); | ||||||
|  |  | ||||||
|  | 		*ptr = v3dFinalVertexPos.getX(); | ||||||
|  | 		ptr++; | ||||||
|  | 		*ptr = v3dFinalVertexPos.getY(); | ||||||
|  | 		ptr++; | ||||||
|  | 		*ptr = v3dFinalVertexPos.getZ(); | ||||||
|  | 		ptr++; | ||||||
|  |  | ||||||
|  | 		GLfloat red = 1.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; | ||||||
|  | } | ||||||
|  |  | ||||||
| void createSphere(float fRadius, uint8 uValue) | void createSphere(float fRadius, uint8 uValue) | ||||||
| { | { | ||||||
| @@ -104,11 +194,11 @@ void display ( void )   // Create The Display Function | |||||||
|  |  | ||||||
| 				const vector<uint32>& vecIndices = g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]->getIndices(); | 				const vector<uint32>& vecIndices = g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]->getIndices(); | ||||||
|  |  | ||||||
| 				glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[uRegionX][uRegionY][uRegionZ]); | 				glBindBuffer(GL_ARRAY_BUFFER, g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ].vertexBuffer); | ||||||
| 				glVertexPointer(3, GL_FLOAT, 24, 0); | 				glVertexPointer(3, GL_FLOAT, 24, 0); | ||||||
| 				glColorPointer(3, GL_FLOAT, 24, (GLvoid*)12); | 				glColorPointer(3, GL_FLOAT, 24, (GLvoid*)12); | ||||||
|  |  | ||||||
| 				glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffers[uRegionX][uRegionY][uRegionZ]); | 				glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ].indexBuffer); | ||||||
|  |  | ||||||
| 				glEnableClientState(GL_VERTEX_ARRAY); | 				glEnableClientState(GL_VERTEX_ARRAY); | ||||||
| 				glEnableClientState(GL_COLOR_ARRAY); | 				glEnableClientState(GL_COLOR_ARRAY); | ||||||
| @@ -153,10 +243,10 @@ void reshape ( int w, int h )   // Create The Reshape Function (the viewport) | |||||||
| void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function | void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function | ||||||
| { | { | ||||||
| 	switch ( key ) { | 	switch ( key ) { | ||||||
| 	case 27:        // When Escape Is Pressed... | case 27:        // When Escape Is Pressed... | ||||||
| 	exit ( 0 );   // Exit The Program | 	exit ( 0 );   // Exit The Program | ||||||
| 	break;        // Ready For Next Case | 	break;        // Ready For Next Case | ||||||
| 	default:        // Now Wrap It Up | default:        // Now Wrap It Up | ||||||
| 	break; | 	break; | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -164,13 +254,13 @@ void keyboard ( unsigned char key, int x, int y )  // Create Keyboard Function | |||||||
| void arrow_keys ( int a_keys, int x, int y )  // Create Special Function (required for arrow keys) | void arrow_keys ( int a_keys, int x, int y )  // Create Special Function (required for arrow keys) | ||||||
| { | { | ||||||
| 	switch ( a_keys ) { | 	switch ( a_keys ) { | ||||||
| 	case GLUT_KEY_UP:     // When Up Arrow Is Pressed... | case GLUT_KEY_UP:     // When Up Arrow Is Pressed... | ||||||
| 	glutFullScreen ( ); // Go Into Full Screen Mode | 	glutFullScreen ( ); // Go Into Full Screen Mode | ||||||
| 	break; | 	break; | ||||||
| 	case GLUT_KEY_DOWN:               // When Down Arrow Is Pressed... | case GLUT_KEY_DOWN:               // When Down Arrow Is Pressed... | ||||||
| 	glutReshapeWindow ( 500, 500 ); // Go Into A 500 By 500 Window | 	glutReshapeWindow ( 500, 500 ); // Go Into A 500 By 500 Window | ||||||
| 	break; | 	break; | ||||||
| 	default: | default: | ||||||
| 	break; | 	break; | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| @@ -244,81 +334,7 @@ void main ( int argc, char** argv )   // Create Main Function For Bringing It Al | |||||||
| 				//Extract the surface for this region | 				//Extract the surface for this region | ||||||
| 				extractReferenceSurface(&g_volData, Region(regLowerCorner, regUpperCorner), ispCurrent); | 				extractReferenceSurface(&g_volData, Region(regLowerCorner, regUpperCorner), ispCurrent); | ||||||
|  |  | ||||||
| 				const vector<SurfaceVertex>& vecVertices = g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]->getVertices(); | 				g_openGLSurfacePatches[uRegionX][uRegionY][uRegionZ] = BuildOpenGLSurfacePatch(*ispCurrent); | ||||||
| 				const vector<uint32>& vecIndices = g_ispRegionSurfaces[uRegionX][uRegionY][uRegionZ]->getIndices(); |  | ||||||
|  |  | ||||||
| 				glGenBuffers(1, &(indexBuffers[uRegionX][uRegionY][uRegionZ])); |  | ||||||
| 				glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffers[uRegionX][uRegionY][uRegionZ]); |  | ||||||
| 				int s = vecIndices.size() * sizeof(GLint); |  | ||||||
| 				if(s != 0) |  | ||||||
| 				{ |  | ||||||
| 					GLvoid* blah = (GLvoid*)(&(vecIndices[0]));				 |  | ||||||
| 					glBufferData(GL_ELEMENT_ARRAY_BUFFER, s, blah, GL_STATIC_DRAW); |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				glGenBuffers(1, &(vertexBuffers[uRegionX][uRegionY][uRegionZ])); |  | ||||||
| 				glBindBuffer(GL_ARRAY_BUFFER, vertexBuffers[uRegionX][uRegionY][uRegionZ]); |  | ||||||
| 				glBufferData(GL_ARRAY_BUFFER, vecVertices.size() * sizeof(GLfloat) * 6, 0, GL_STATIC_DRAW); |  | ||||||
| 				GLfloat* ptr = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); |  | ||||||
|  |  | ||||||
| 				for(vector<SurfaceVertex>::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 + v3dRegionOffset; |  | ||||||
|  |  | ||||||
| 					*ptr = v3dFinalVertexPos.getX(); |  | ||||||
| 					ptr++; |  | ||||||
| 					*ptr = v3dFinalVertexPos.getY(); |  | ||||||
| 					ptr++; |  | ||||||
| 					*ptr = v3dFinalVertexPos.getZ(); |  | ||||||
| 					ptr++; |  | ||||||
|  |  | ||||||
| 					GLfloat red = 1.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); |  | ||||||
|  |  | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user