From 34f57911a741603d404ef06c7dda2816b9d9df46 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 23 May 2014 22:18:17 +0200 Subject: [PATCH] Converting OpenGLExample to use common OpenGLWidget. --- examples/OpenGL/CMakeLists.txt | 8 +- .../OpenGL/OpenGLImmediateModeSupport.cpp | 65 ----- examples/OpenGL/OpenGLImmediateModeSupport.h | 34 --- examples/OpenGL/OpenGLSupport.cpp | 66 ----- examples/OpenGL/OpenGLSupport.h | 40 --- .../OpenGLVertexBufferObjectSupport.cpp | 126 --------- .../OpenGL/OpenGLVertexBufferObjectSupport.h | 42 --- examples/OpenGL/OpenGLWidget.cpp | 246 ------------------ examples/OpenGL/OpenGLWidget.h | 99 ------- examples/OpenGL/main.cpp | 14 +- 10 files changed, 12 insertions(+), 728 deletions(-) delete mode 100644 examples/OpenGL/OpenGLImmediateModeSupport.cpp delete mode 100644 examples/OpenGL/OpenGLImmediateModeSupport.h delete mode 100644 examples/OpenGL/OpenGLSupport.cpp delete mode 100644 examples/OpenGL/OpenGLSupport.h delete mode 100644 examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp delete mode 100644 examples/OpenGL/OpenGLVertexBufferObjectSupport.h delete mode 100644 examples/OpenGL/OpenGLWidget.cpp delete mode 100644 examples/OpenGL/OpenGLWidget.h diff --git a/examples/OpenGL/CMakeLists.txt b/examples/OpenGL/CMakeLists.txt index 5cd1ed55..dde5a41b 100644 --- a/examples/OpenGL/CMakeLists.txt +++ b/examples/OpenGL/CMakeLists.txt @@ -26,18 +26,12 @@ PROJECT(OpenGLExample) #Projects source files SET(SRC_FILES main.cpp - OpenGLImmediateModeSupport.cpp - OpenGLSupport.cpp - OpenGLVertexBufferObjectSupport.cpp - OpenGLWidget.cpp + ../common/OpenGLWidget.cpp Shapes.cpp ) #Projects headers files SET(INC_FILES - OpenGLImmediateModeSupport.h - OpenGLSupport.h - OpenGLVertexBufferObjectSupport.h OpenGLWidget.h Shapes.h ) diff --git a/examples/OpenGL/OpenGLImmediateModeSupport.cpp b/examples/OpenGL/OpenGLImmediateModeSupport.cpp deleted file mode 100644 index ab2522bd..00000000 --- a/examples/OpenGL/OpenGLImmediateModeSupport.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#include "OpenGLImmediateModeSupport.h" -#include "OpenGLSupport.h" - -#include "PolyVoxCore/Mesh.h" - -using namespace PolyVox; -using namespace std; - -void renderRegionImmediateMode(PolyVox::Mesh >& mesh, unsigned int uLodLevel) -{ - const vector >& vecVertices = mesh.getVertices(); - const vector& vecIndices = mesh.getIndices(); - - int beginIndex = mesh.m_vecLodRecords[uLodLevel].beginIndex; - int endIndex = mesh.m_vecLodRecords[uLodLevel].endIndex; - - glBegin(GL_TRIANGLES); - //for(vector::const_iterator iterIndex = vecIndices.begin(); iterIndex != vecIndices.end(); ++iterIndex) - for(int index = beginIndex; index < endIndex; ++index) - { - const MarchingCubesVertex & 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(mesh.m_Region.getLowerCorner()); - - - - - uint8_t material = static_cast(static_cast(vertex.getMaterial().getMaterial()) + 0.5); - //uint8_t material = 1; - - - 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/OpenGLImmediateModeSupport.h b/examples/OpenGL/OpenGLImmediateModeSupport.h deleted file mode 100644 index 3bc45a2b..00000000 --- a/examples/OpenGL/OpenGLImmediateModeSupport.h +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#ifndef __OpenGLExample_OpenGLImmediateModeSupport_H__ -#define __OpenGLExample_OpenGLImmediateModeSupport_H__ - -#include "PolyVoxCore/MaterialDensityPair.h" -#include "PolyVoxCore/PolyVoxForwardDeclarations.h" - -#include "glew/glew.h" - -void renderRegionImmediateMode(PolyVox::Mesh >& mesh, unsigned int uLodLevel); - -#endif //__OpenGLExample_OpenGLImmediateModeSupport_H__ diff --git a/examples/OpenGL/OpenGLSupport.cpp b/examples/OpenGL/OpenGLSupport.cpp deleted file mode 100644 index 73efb444..00000000 --- a/examples/OpenGL/OpenGLSupport.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#include "OpenGLSupport.h" - -using namespace PolyVox; - -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; -} \ No newline at end of file diff --git a/examples/OpenGL/OpenGLSupport.h b/examples/OpenGL/OpenGLSupport.h deleted file mode 100644 index 2d4d1185..00000000 --- a/examples/OpenGL/OpenGLSupport.h +++ /dev/null @@ -1,40 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#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(uint8_t materialID); - -#endif //__OpenGLExample_OpenGLSupport_H__ \ No newline at end of file diff --git a/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp b/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp deleted file mode 100644 index 17e9e138..00000000 --- a/examples/OpenGL/OpenGLVertexBufferObjectSupport.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#include "OpenGLSupport.h" -#include "OpenGLVertexBufferObjectSupport.h" - -#include "PolyVoxCore/MaterialDensityPair.h" -#include "PolyVoxCore/Mesh.h" - -using namespace PolyVox; -using namespace std; - -OpenGLMesh BuildOpenGLMesh(const Mesh >& mesh) -{ - //Represents our filled in OpenGL vertex and index buffer objects. - OpenGLMesh result; - - //The source - result.sourceMesh = &mesh; - - //Convienient access to the vertices and indices - const vector >& vecVertices = mesh.getVertices(); - const vector& 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 >::const_iterator iterVertex = vecVertices.begin(); iterVertex != vecVertices.end(); ++iterVertex) - { - const MarchingCubesVertex & 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(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 = static_cast(vertex.getMaterial().getMaterial() + 0.5); - //uint8_t material = 1; - - 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 OpenGLMesh& openGLMesh, unsigned int uLodLevel) -{ - int beginIndex = openGLMesh.sourceMesh->m_vecLodRecords[uLodLevel].beginIndex; - int endIndex = openGLMesh.sourceMesh->m_vecLodRecords[uLodLevel].endIndex; - glBindBuffer(GL_ARRAY_BUFFER, openGLMesh.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, openGLMesh.indexBuffer); - - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - glEnableClientState(GL_COLOR_ARRAY); - - //glDrawElements(GL_TRIANGLES, openGLMesh.noOfIndices, GL_UNSIGNED_INT, 0); - glDrawRangeElements(GL_TRIANGLES, beginIndex, endIndex-1, endIndex - beginIndex,/* openGLMesh.noOfIndices,*/ GL_UNSIGNED_INT, 0); - - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); -} \ No newline at end of file diff --git a/examples/OpenGL/OpenGLVertexBufferObjectSupport.h b/examples/OpenGL/OpenGLVertexBufferObjectSupport.h deleted file mode 100644 index e82a8c29..00000000 --- a/examples/OpenGL/OpenGLVertexBufferObjectSupport.h +++ /dev/null @@ -1,42 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#ifndef __OpenGLExample_OpenGLVertexBufferObjectSupport_H__ -#define __OpenGLExample_OpenGLVertexBufferObjectSupport_H__ - -#include "PolyVoxCore/PolyVoxForwardDeclarations.h" - -#include "glew/glew.h" - -struct OpenGLMesh -{ - GLulong noOfIndices; - GLuint indexBuffer; - GLuint vertexBuffer; - const PolyVox::Mesh >* sourceMesh; -}; - -OpenGLMesh BuildOpenGLMesh(const PolyVox::Mesh >& mesh); -void renderRegionVertexBufferObject(const OpenGLMesh& openGLMesh, unsigned int uLodLevel); - -#endif //__OpenGLExample_OpenGLVertexBufferObjectSupport_H__ diff --git a/examples/OpenGL/OpenGLWidget.cpp b/examples/OpenGL/OpenGLWidget.cpp deleted file mode 100644 index 38c107a9..00000000 --- a/examples/OpenGL/OpenGLWidget.cpp +++ /dev/null @@ -1,246 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#include "OpenGLWidget.h" - -#include - -#include "PolyVoxCore/GradientEstimators.h" -#include "PolyVoxCore/MaterialDensityPair.h" -#include "PolyVoxCore/MarchingCubesSurfaceExtractor.h" - -//Some namespaces we need -using namespace std; -using namespace PolyVox; -using namespace std; - -OpenGLWidget::OpenGLWidget(QWidget *parent) - :QGLWidget(parent) - ,m_volData(0) -{ - m_xRotation = 0; - m_yRotation = 0; - m_uRegionSideLength = 32; - - timer = new QTimer(this); - connect(timer, SIGNAL(timeout()), this, SLOT(update())); - timer->start(0); -} - -void OpenGLWidget::setVolume(PolyVox::LargeVolume* volData) -{ - //First we free anything from the previous volume (if there was one). - m_mapOpenGLMeshes.clear(); - m_mapMeshes.clear(); - m_volData = volData; - - //If we have any volume data then generate the new surface patches. - if(m_volData != 0) - { - m_uVolumeWidthInRegions = volData->getWidth() / m_uRegionSideLength; - m_uVolumeHeightInRegions = volData->getHeight() / m_uRegionSideLength; - m_uVolumeDepthInRegions = volData->getDepth() / m_uRegionSideLength; - - //Our volume is broken down into cuboid regions, and we create one mesh for each region. - //This three-level for loop iterates over each region. - for(uint16_t uRegionZ = 0; uRegionZ < m_uVolumeDepthInRegions; ++uRegionZ) - { - std::cout << "uRegionZ = " << uRegionZ << " of " << m_uVolumeDepthInRegions << std::endl; - for(uint16_t uRegionY = 0; uRegionY < m_uVolumeHeightInRegions; ++uRegionY) - { - for(uint16_t uRegionX = 0; uRegionX < m_uVolumeWidthInRegions; ++uRegionX) - { - //Compute the extents of the current region - //FIXME - This is a little complex? PolyVox could - //provide more functions for dealing with regions? - int32_t regionStartX = uRegionX * m_uRegionSideLength; - int32_t regionStartY = uRegionY * m_uRegionSideLength; - int32_t regionStartZ = uRegionZ * m_uRegionSideLength; - - int32_t regionEndX = regionStartX + m_uRegionSideLength; - int32_t regionEndY = regionStartY + m_uRegionSideLength; - int32_t regionEndZ = regionStartZ + m_uRegionSideLength; - - Vector3DInt32 regLowerCorner(regionStartX, regionStartY, regionStartZ); - Vector3DInt32 regUpperCorner(regionEndX, regionEndY, regionEndZ); - - //Extract the surface for this region - //extractSurface(m_volData, 0, PolyVox::Region(regLowerCorner, regUpperCorner), meshCurrent); - - std::shared_ptr< Mesh > > mesh(new Mesh >); - MarchingCubesSurfaceExtractor< LargeVolume > surfaceExtractor(volData, PolyVox::Region(regLowerCorner, regUpperCorner), mesh.get()); - surfaceExtractor.execute(); - - //decimatedMesh->generateAveragedFaceNormals(true); - - //computeNormalsForVertices(m_volData, *(decimatedMesh.get()), SOBEL_SMOOTHED); - //*meshCurrent = getSmoothedSurface(*meshCurrent); - //mesh->smooth(0.3f); - //meshCurrent->generateAveragedFaceNormals(true); - - if(mesh->m_vecTriangleIndices.size() > 0) - { - - - Vector3DUint8 v3dRegPos(uRegionX,uRegionY,uRegionZ); - if(m_bUseOpenGLVertexBufferObjects) - { - OpenGLMesh openGLMesh = BuildOpenGLMesh(*(mesh.get())); - m_mapOpenGLMeshes.insert(make_pair(v3dRegPos, openGLMesh)); - } - //else - //{ - m_mapMeshes.insert(make_pair(v3dRegPos, mesh)); - //} - //delete meshCurrent; - } - } - } - } - - //Projection matrix is dependant on volume size, so we need to set it up again. - setupProjectionMatrix(); - } -} - -void OpenGLWidget::initializeGL() -{ - m_bUseOpenGLVertexBufferObjects = true; - if(m_bUseOpenGLVertexBufferObjects) - { - //We need GLEW to access recent OpenGL functionality - GLenum err = glewInit(); - if (GLEW_OK != err) - { - /* Problem: glewInit failed, something is seriously wrong. */ - cout << "GLEW Error: " << glewGetErrorString(err) << endl; - } - } - - 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 OpenGLWidget::resizeGL(int w, int h) -{ - //Setup the viewport based on the window size - glViewport(0, 0, w, h); - - //Projection matrix is also dependant on the size of the current volume. - if(m_volData) - { - setupProjectionMatrix(); - } -} - -void OpenGLWidget::paintGL() -{ - if(m_volData) - { - 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 - - //Moves the camera back so we can see the volume - glTranslatef(0.0f, 0.0f, -m_volData->getDiagonalLength()); - - glRotatef(m_xRotation, 1.0f, 0.0f, 0.0f); - glRotatef(m_yRotation, 0.0f, 1.0f, 0.0f); - - //Centre the volume on the origin - glTranslatef(-g_uVolumeSideLength/2,-g_uVolumeSideLength/2,-g_uVolumeSideLength/2); - - for(uint16_t uRegionZ = 0; uRegionZ < m_uVolumeDepthInRegions; ++uRegionZ) - { - for(uint16_t uRegionY = 0; uRegionY < m_uVolumeHeightInRegions; ++uRegionY) - { - for(uint16_t uRegionX = 0; uRegionX < m_uVolumeWidthInRegions; ++uRegionX) - { - Vector3DUint8 v3dRegPos(uRegionX,uRegionY,uRegionZ); - if(m_mapMeshes.find(v3dRegPos) != m_mapMeshes.end()) - { - std::shared_ptr< Mesh > > meshCurrent = m_mapMeshes[v3dRegPos]; - unsigned int uLodLevel = 0; //meshCurrent->m_vecLodRecords.size() - 1; - if(m_bUseOpenGLVertexBufferObjects) - { - renderRegionVertexBufferObject(m_mapOpenGLMeshes[v3dRegPos], uLodLevel); - } - else - { - renderRegionImmediateMode(*meshCurrent, uLodLevel); - } - } - } - } - } - - GLenum errCode; - const GLubyte *errString; - - if ((errCode = glGetError()) != GL_NO_ERROR) - { - errString = gluErrorString(errCode); - cout << "OpenGL Error: " << errString << endl; - } - } -} - -void OpenGLWidget::mousePressEvent(QMouseEvent* event) -{ - m_CurrentMousePos = event->pos(); - m_LastFrameMousePos = m_CurrentMousePos; -} - -void OpenGLWidget::mouseMoveEvent(QMouseEvent* event) -{ - m_CurrentMousePos = event->pos(); - QPoint diff = m_CurrentMousePos - m_LastFrameMousePos; - m_xRotation += diff.x(); - m_yRotation += diff.y(); - m_LastFrameMousePos = m_CurrentMousePos;; -} - -void OpenGLWidget::setupProjectionMatrix(void) -{ - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - float frustumSize = m_volData->getDiagonalLength() / 2.0f; - float aspect = static_cast(width()) / static_cast(height()); - - glOrtho(frustumSize*aspect, -frustumSize*aspect, frustumSize, -frustumSize, 1.0, 5000); -} diff --git a/examples/OpenGL/OpenGLWidget.h b/examples/OpenGL/OpenGLWidget.h deleted file mode 100644 index 3511ac75..00000000 --- a/examples/OpenGL/OpenGLWidget.h +++ /dev/null @@ -1,99 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#ifndef __PolyVox_OpenGLWidget_H__ -#define __PolyVox_OpenGLWidget_H__ - -#include "glew/glew.h" - -#include -#include - -#include "PolyVoxCore/LargeVolume.h" -#include "PolyVoxCore/Mesh.h" -#include "PolyVoxCore/Impl/Utility.h" - -#include "OpenGLImmediateModeSupport.h" -#include "OpenGLVertexBufferObjectSupport.h" -#include "Shapes.h" - -const int32_t g_uVolumeSideLength = 128; - -struct Vector3DUint8Compare -{ - bool operator() (const PolyVox::Vector3DUint8& a, const PolyVox::Vector3DUint8& b) const - { - const uint32_t size = 3; - for(uint32_t ct = 0; ct < size; ++ct) - { - if (a.getElement(ct) < b.getElement(ct)) - return true; - if (b.getElement(ct) < a.getElement(ct)) - return false; - } - return false; - } -}; - -class OpenGLWidget : public QGLWidget - { - - public: - OpenGLWidget(QWidget *parent); - - void setVolume(PolyVox::LargeVolume* volData); - - void mouseMoveEvent(QMouseEvent* event); - void mousePressEvent(QMouseEvent* event); - - protected: - void initializeGL(); - void resizeGL(int w, int h); - void paintGL(); - - private: - void setupProjectionMatrix(void); - QPoint m_LastFrameMousePos; - QPoint m_CurrentMousePos; - - int m_xRotation; - int m_yRotation; - - QTimer *timer; - - bool m_bUseOpenGLVertexBufferObjects; - - //Creates a volume 128x128x128 - PolyVox::LargeVolume* m_volData; - - //Rather than storing one big mesh, the volume is broken into regions and a mesh is stored for each region - std::map m_mapOpenGLMeshes; - std::map > >, Vector3DUint8Compare> m_mapMeshes; - - unsigned int m_uRegionSideLength; - unsigned int m_uVolumeWidthInRegions; - unsigned int m_uVolumeHeightInRegions; - unsigned int m_uVolumeDepthInRegions; - }; - -#endif //__PolyVox_OpenGLWidget_H__ diff --git a/examples/OpenGL/main.cpp b/examples/OpenGL/main.cpp index ab70e957..e27987db 100644 --- a/examples/OpenGL/main.cpp +++ b/examples/OpenGL/main.cpp @@ -22,6 +22,7 @@ freely, subject to the following restrictions: *******************************************************************************/ #include "PolyVoxCore/FilePager.h" +#include "PolyVoxCore/MarchingCubesSurfaceExtractor.h" #include "PolyVoxCore/MaterialDensityPair.h" #include "PolyVoxCore/LargeVolume.h" #include "PolyVoxCore/LowPassFilter.h" @@ -30,8 +31,6 @@ freely, subject to the following restrictions: #include "PolyVoxCore/Mesh.h" #include "PolyVoxCore/Impl/Utility.h" -#include "OpenGLImmediateModeSupport.h" -#include "OpenGLVertexBufferObjectSupport.h" #include "Shapes.h" #include "OpenGLWidget.h" @@ -48,6 +47,8 @@ using namespace std; using namespace PolyVox; using namespace std; +const int32_t g_uVolumeSideLength = 128; + int main(int argc, char *argv[]) { RLEBlockCompressor* compressor = new RLEBlockCompressor(); @@ -98,9 +99,16 @@ int main(int argc, char *argv[]) QTime time; time.start(); - openGLWidget.setVolume(&volData); + //openGLWidget.setVolume(&volData); cout << endl << "Time taken = " << time.elapsed() / 1000.0f << "s" << endl << endl; + auto mesh = extractMarchingCubesMesh(&volData, volData.getEnclosingRegion()); + + //Pass the surface to the OpenGL window + openGLWidget.addMesh(mesh); + //openGLWidget.addMesh(mesh2); + openGLWidget.setViewableRegion(volData.getEnclosingRegion()); + //return 0; return app.exec();