Work on new OpenGL example - Splitting into several files.
This commit is contained in:
@ -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)
|
||||
|
66
examples/OpenGL/OpenGLImmediateModeSupport.cpp
Normal file
66
examples/OpenGL/OpenGLImmediateModeSupport.cpp
Normal file
@ -0,0 +1,66 @@
|
||||
#include "OpenGLImmediateModeSupport.h"
|
||||
|
||||
#include "PolyVoxCore/IndexedSurfacePatch.h"
|
||||
|
||||
using namespace PolyVox;
|
||||
using namespace std;
|
||||
|
||||
void renderRegion(PolyVox::IndexedSurfacePatch& isp)
|
||||
{
|
||||
const vector<SurfaceVertex>& vecVertices = isp.getVertices();
|
||||
const vector<uint32>& vecIndices = isp.getIndices();
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
for(vector<uint32>::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<Vector3DFloat>(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();
|
||||
}
|
10
examples/OpenGL/OpenGLImmediateModeSupport.h
Normal file
10
examples/OpenGL/OpenGLImmediateModeSupport.h
Normal file
@ -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__
|
0
examples/OpenGL/OpenGLSupport.cpp
Normal file
0
examples/OpenGL/OpenGLSupport.cpp
Normal file
0
examples/OpenGL/OpenGLSupport.h
Normal file
0
examples/OpenGL/OpenGLSupport.h
Normal file
98
examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp
Normal file
98
examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp
Normal file
@ -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<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);
|
||||
}
|
||||
|
||||
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<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++;
|
||||
|
||||
*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;
|
||||
}
|
17
examples/OpenGL/OpenGLVertexBufferObjectSupport.h
Normal file
17
examples/OpenGL/OpenGLVertexBufferObjectSupport.h
Normal file
@ -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__
|
@ -3,41 +3,28 @@
|
||||
#include "PolyVoxCore/SurfaceExtractors.h"
|
||||
#include "PolyVoxCore/Utility.h"
|
||||
|
||||
#include "OpenGLImmediateModeSupport.h"
|
||||
#include "OpenGLVertexBufferObjectSupport.h"
|
||||
#include "Shapes.h"
|
||||
|
||||
#include <windows.h> // 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 <gl/gl.h> // The GL Header File
|
||||
#endif
|
||||
#include <gl/glut.h> // The GL Utility Toolkit (Glut) Header]
|
||||
#ifdef WIN32
|
||||
#include "glew/glew.h"
|
||||
#else
|
||||
#include <gl/gl.h>
|
||||
#include <gl/glut.h>
|
||||
#include <gl/gl.h> // The GL Header File
|
||||
#endif
|
||||
#include <gl/glut.h> // The GL Utility Toolkit (Glut) Header]
|
||||
|
||||
#include <iostream>
|
||||
|
||||
|
||||
//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<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
|
||||
#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<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);
|
||||
}
|
||||
|
||||
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<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++;
|
||||
|
||||
*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<SurfaceVertex>& vecVertices = ispCurrent->getVertices();
|
||||
const vector<uint32>& vecIndices = ispCurrent->getIndices();
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
for(vector<uint32>::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex)
|
||||
}
|
||||
else
|
||||
{
|
||||
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<Vector3DFloat>(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());
|
||||
|
||||
IndexedSurfacePatch* ispCurrent = g_indexedSurfacePatches[uRegionX][uRegionY][uRegionZ];
|
||||
renderRegion(*ispCurrent);
|
||||
|
||||
}
|
||||
glEnd();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user