Tidying up basic OpenGL example.
This commit is contained in:
parent
e8b10fd2b9
commit
c906e71bc3
@ -32,7 +32,9 @@ ADD_SUBDIRECTORY(library)
|
|||||||
|
|
||||||
OPTION(ENABLE_EXAMPLES "Should the examples be built" ON)
|
OPTION(ENABLE_EXAMPLES "Should the examples be built" ON)
|
||||||
IF(ENABLE_EXAMPLES)
|
IF(ENABLE_EXAMPLES)
|
||||||
|
ADD_SUBDIRECTORY(examples/Basic)
|
||||||
ADD_SUBDIRECTORY(examples/OpenGL)
|
ADD_SUBDIRECTORY(examples/OpenGL)
|
||||||
|
ADD_DEPENDENCIES(BasicExample PolyVoxCore PolyVoxUtil)
|
||||||
ADD_DEPENDENCIES(OpenGLExample PolyVoxCore PolyVoxUtil)
|
ADD_DEPENDENCIES(OpenGLExample PolyVoxCore PolyVoxUtil)
|
||||||
ENDIF(ENABLE_EXAMPLES)
|
ENDIF(ENABLE_EXAMPLES)
|
||||||
|
|
||||||
|
@ -5,14 +5,18 @@ PROJECT(BasicExample)
|
|||||||
#Projects source files
|
#Projects source files
|
||||||
SET(SRC_FILES
|
SET(SRC_FILES
|
||||||
glew/glew.c
|
glew/glew.c
|
||||||
|
|
||||||
main.cpp
|
main.cpp
|
||||||
|
OpenGLWidget.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
#Projects headers files
|
#Projects headers files
|
||||||
SET(INC_FILES
|
SET(INC_FILES
|
||||||
glew/glew.h
|
glew/glew.h
|
||||||
glew/glxew.h
|
glew/glxew.h
|
||||||
glew/wglew.h
|
glew/wglew.h
|
||||||
|
|
||||||
|
OpenGLWidget.h
|
||||||
)
|
)
|
||||||
|
|
||||||
ADD_DEFINITIONS(-DGLEW_STATIC)
|
ADD_DEFINITIONS(-DGLEW_STATIC)
|
||||||
|
@ -29,14 +29,16 @@ freely, subject to the following restrictions:
|
|||||||
#include "SurfaceMesh.h"
|
#include "SurfaceMesh.h"
|
||||||
#include "PolyVoxImpl/Utility.h"
|
#include "PolyVoxImpl/Utility.h"
|
||||||
|
|
||||||
#include "glew/glew.h"
|
|
||||||
|
|
||||||
|
#include "OpenGLWidget.h"
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <windows.h> // Standard Header For Most Programs
|
#include <windows.h> // Standard Header For Most Programs
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QGLWidget>
|
|
||||||
#include <QTime>
|
#include <QTime>
|
||||||
|
|
||||||
//Some namespaces we need
|
//Some namespaces we need
|
||||||
@ -44,191 +46,10 @@ using namespace std;
|
|||||||
using namespace PolyVox;
|
using namespace PolyVox;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
struct OpenGLColour
|
|
||||||
{
|
|
||||||
GLfloat red;
|
|
||||||
GLfloat green;
|
|
||||||
GLfloat blue;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct OpenGLSurfaceMesh
|
|
||||||
{
|
|
||||||
GLulong noOfIndices;
|
|
||||||
GLuint indexBuffer;
|
|
||||||
GLuint vertexBuffer;
|
|
||||||
const PolyVox::SurfaceMesh* sourceMesh;
|
|
||||||
};
|
|
||||||
|
|
||||||
OpenGLColour convertMaterialIDToColour(uint8_t materialID)
|
|
||||||
{
|
|
||||||
OpenGLColour colour;
|
|
||||||
|
|
||||||
switch(materialID)
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
colour.red = 1.0f;
|
|
||||||
colour.green = 0.0f;
|
|
||||||
colour.blue = 0.0f;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
colour.red = 0.0f;
|
|
||||||
colour.green = 1.0f;
|
|
||||||
colour.blue = 0.0f;
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
colour.red = 0.0f;
|
|
||||||
colour.green = 0.0f;
|
|
||||||
colour.blue = 1.0f;
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
colour.red = 1.0f;
|
|
||||||
colour.green = 1.0f;
|
|
||||||
colour.blue = 0.0f;
|
|
||||||
break;
|
|
||||||
case 5:
|
|
||||||
colour.red = 1.0f;
|
|
||||||
colour.green = 0.0f;
|
|
||||||
colour.blue = 1.0f;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
colour.red = 1.0f;
|
|
||||||
colour.green = 1.0f;
|
|
||||||
colour.blue = 1.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
return colour;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenGLSurfaceMesh BuildOpenGLSurfaceMesh(const PolyVox::SurfaceMesh& mesh);
|
|
||||||
void renderRegionVertexBufferObject(const OpenGLSurfaceMesh& openGLSurfaceMesh, unsigned int uLodLevel);
|
|
||||||
|
|
||||||
OpenGLSurfaceMesh BuildOpenGLSurfaceMesh(const SurfaceMesh& mesh)
|
|
||||||
{
|
|
||||||
//Represents our filled in OpenGL vertex and index buffer objects.
|
|
||||||
OpenGLSurfaceMesh result;
|
|
||||||
|
|
||||||
//The source
|
|
||||||
result.sourceMesh = &mesh;
|
|
||||||
|
|
||||||
//Convienient access to the vertices and indices
|
|
||||||
const vector<SurfaceVertex>& vecVertices = mesh.getVertices();
|
|
||||||
const vector<uint32_t>& vecIndices = mesh.getIndices();
|
|
||||||
|
|
||||||
//If we have any indices...
|
|
||||||
if(!vecIndices.empty())
|
|
||||||
{
|
|
||||||
//Create an OpenGL index buffer
|
|
||||||
glGenBuffers(1, &result.indexBuffer);
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, result.indexBuffer);
|
|
||||||
|
|
||||||
//Get a pointer to the first index
|
|
||||||
GLvoid* pIndices = (GLvoid*)(&(vecIndices[0]));
|
|
||||||
|
|
||||||
//Fill the OpenGL index buffer with our data.
|
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vecIndices.size() * sizeof(uint32_t), pIndices, 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>(mesh.m_Region.getLowerCorner());
|
|
||||||
|
|
||||||
*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++;
|
|
||||||
|
|
||||||
uint8_t material = vertex.getMaterial() + 0.5;
|
|
||||||
|
|
||||||
OpenGLColour colour = convertMaterialIDToColour(material);
|
|
||||||
|
|
||||||
*ptr = colour.red;
|
|
||||||
ptr++;
|
|
||||||
*ptr = colour.green;
|
|
||||||
ptr++;
|
|
||||||
*ptr = colour.blue;
|
|
||||||
ptr++;
|
|
||||||
}
|
|
||||||
|
|
||||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void renderRegionVertexBufferObject(const OpenGLSurfaceMesh& openGLSurfaceMesh, unsigned int uLodLevel)
|
|
||||||
{
|
|
||||||
int beginIndex = openGLSurfaceMesh.sourceMesh->m_vecLodRecords[uLodLevel].beginIndex;
|
|
||||||
int endIndex = openGLSurfaceMesh.sourceMesh->m_vecLodRecords[uLodLevel].endIndex;
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, openGLSurfaceMesh.vertexBuffer);
|
|
||||||
glVertexPointer(3, GL_FLOAT, 36, 0);
|
|
||||||
glNormalPointer(GL_FLOAT, 36, (GLvoid*)12);
|
|
||||||
glColorPointer(3, GL_FLOAT, 36, (GLvoid*)24);
|
|
||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, openGLSurfaceMesh.indexBuffer);
|
|
||||||
|
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
|
||||||
glEnableClientState(GL_NORMAL_ARRAY);
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
|
||||||
|
|
||||||
//glDrawElements(GL_TRIANGLES, openGLSurfaceMesh.noOfIndices, GL_UNSIGNED_INT, 0);
|
|
||||||
glDrawRangeElements(GL_TRIANGLES, beginIndex, endIndex-1, endIndex - beginIndex,/* openGLSurfaceMesh.noOfIndices,*/ GL_UNSIGNED_INT, 0);
|
|
||||||
|
|
||||||
glDisableClientState(GL_COLOR_ARRAY);
|
|
||||||
glDisableClientState(GL_NORMAL_ARRAY);
|
|
||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
void renderRegionImmediateMode(PolyVox::SurfaceMesh& mesh, unsigned int uLodLevel)
|
|
||||||
{
|
|
||||||
const vector<SurfaceVertex>& vecVertices = mesh.getVertices();
|
|
||||||
const vector<uint32_t>& vecIndices = mesh.getIndices();
|
|
||||||
|
|
||||||
int beginIndex = mesh.m_vecLodRecords[uLodLevel].beginIndex;
|
|
||||||
int endIndex = mesh.m_vecLodRecords[uLodLevel].endIndex;
|
|
||||||
|
|
||||||
glBegin(GL_TRIANGLES);
|
|
||||||
//for(vector<PolyVox::uint32_t>::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex)
|
|
||||||
for(int index = beginIndex; index < endIndex; ++index)
|
|
||||||
{
|
|
||||||
const SurfaceVertex& vertex = vecVertices[vecIndices[index]];
|
|
||||||
const Vector3DFloat& v3dVertexPos = vertex.getPosition();
|
|
||||||
//const Vector3DFloat v3dRegionOffset(uRegionX * g_uRegionSideLength, uRegionY * g_uRegionSideLength, uRegionZ * g_uRegionSideLength);
|
|
||||||
const Vector3DFloat v3dFinalVertexPos = v3dVertexPos + static_cast<Vector3DFloat>(mesh.m_Region.getLowerCorner());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t material = vertex.getMaterial() + 0.5;
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
void createSphereInVolume(Volume<MaterialDensityPair44>& volData, float fRadius, uint8_t uValue)
|
void createSphereInVolume(Volume<MaterialDensityPair44>& volData, float fRadius, uint8_t uValue)
|
||||||
{
|
{
|
||||||
//This vector hold the position of the center of the volume
|
//This vector hold the position of the center of the volume
|
||||||
@ -272,78 +93,7 @@ void createCubeInVolume(Volume<MaterialDensityPair44>& volData, Vector3DUint16 l
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class OpenGLWidget : public QGLWidget
|
const uint16_t g_uVolumeSideLength = 64;
|
||||||
{
|
|
||||||
public:
|
|
||||||
OpenGLWidget(QWidget *parent)
|
|
||||||
:QGLWidget(parent)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void initializeGL()
|
|
||||||
{
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
|
|
||||||
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
|
|
||||||
glClearDepth(1.0f); // Depth Buffer Setup
|
|
||||||
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
|
|
||||||
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
|
|
||||||
glEnable ( GL_COLOR_MATERIAL );
|
|
||||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
||||||
|
|
||||||
glEnable(GL_LIGHTING);
|
|
||||||
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
|
|
||||||
glEnable(GL_LIGHT0);
|
|
||||||
|
|
||||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
||||||
|
|
||||||
glShadeModel(GL_SMOOTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
void resizeGL(int w, int h)
|
|
||||||
{
|
|
||||||
//Setup the viewport based on the window size
|
|
||||||
glViewport(0, 0, w, h);
|
|
||||||
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
|
||||||
glLoadIdentity();
|
|
||||||
|
|
||||||
//float frustumSize = m_volData->getDiagonalLength() / 2.0f;
|
|
||||||
float frustumSize = 100.0f;
|
|
||||||
float aspect = static_cast<float>(width()) / static_cast<float>(height());
|
|
||||||
|
|
||||||
glOrtho(frustumSize*aspect, -frustumSize*aspect, frustumSize, -frustumSize, 1.0, 5000);
|
|
||||||
}
|
|
||||||
|
|
||||||
void paintGL()
|
|
||||||
{
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
|
|
||||||
glMatrixMode(GL_MODELVIEW); // Select The Model View Matrix
|
|
||||||
glLoadIdentity(); // Reset The Current Modelview Matrix
|
|
||||||
|
|
||||||
glTranslatef(0.0f,0.0f,-100.0f); // Move Left 1.5 Units And Into The Screen 6.0
|
|
||||||
|
|
||||||
|
|
||||||
//renderRegionVertexBufferObject(mesh, 0);
|
|
||||||
renderRegionImmediateMode(surfaceMesh, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
OpenGLSurfaceMesh mesh;
|
|
||||||
SurfaceMesh surfaceMesh;
|
|
||||||
};
|
|
||||||
|
|
||||||
const uint16_t g_uVolumeSideLength = 32;
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -353,7 +103,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
Volume<MaterialDensityPair44> volData(g_uVolumeSideLength, g_uVolumeSideLength, g_uVolumeSideLength);
|
Volume<MaterialDensityPair44> volData(g_uVolumeSideLength, g_uVolumeSideLength, g_uVolumeSideLength);
|
||||||
|
|
||||||
createSphereInVolume(volData, 10, 1);
|
createSphereInVolume(volData, 30, 1);
|
||||||
|
|
||||||
SurfaceExtractor<MaterialDensityPair44> surfaceExtractor(volData);
|
SurfaceExtractor<MaterialDensityPair44> surfaceExtractor(volData);
|
||||||
|
|
||||||
@ -362,7 +112,8 @@ int main(int argc, char *argv[])
|
|||||||
//OpenGLSurfaceMesh mesh = BuildOpenGLSurfaceMesh(*surface);
|
//OpenGLSurfaceMesh mesh = BuildOpenGLSurfaceMesh(*surface);
|
||||||
|
|
||||||
|
|
||||||
openGLWidget.mesh = BuildOpenGLSurfaceMesh(*(surface.get()));
|
//openGLWidget.mesh = BuildOpenGLSurfaceMesh(*(surface.get()));
|
||||||
openGLWidget.surfaceMesh = *surface;
|
//openGLWidget.surfaceMesh = *surface;
|
||||||
|
openGLWidget.setSurfaceMeshToRender(*surface);
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
@ -158,7 +158,7 @@ void OpenGLWidget::setVolume(PolyVox::Volume<MaterialDensityPair44>* volData)
|
|||||||
|
|
||||||
void OpenGLWidget::initializeGL()
|
void OpenGLWidget::initializeGL()
|
||||||
{
|
{
|
||||||
m_bUseOpenGLVertexBufferObjects = false;
|
m_bUseOpenGLVertexBufferObjects = true;
|
||||||
if(m_bUseOpenGLVertexBufferObjects)
|
if(m_bUseOpenGLVertexBufferObjects)
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
Loading…
x
Reference in New Issue
Block a user