Converting OpenGLExample to use common OpenGLWidget.
This commit is contained in:
parent
d34fd6b3c8
commit
34f57911a7
@ -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
|
||||
)
|
||||
|
@ -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<MarchingCubesVertex<MaterialDensityPair44> >& mesh, unsigned int uLodLevel)
|
||||
{
|
||||
const vector<MarchingCubesVertex<MaterialDensityPair44> >& 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 MarchingCubesVertex<MaterialDensityPair44> & 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 = static_cast<uint8_t>(static_cast<double>(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();
|
||||
}
|
@ -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<PolyVox::MarchingCubesVertex<PolyVox::MaterialDensityPair44> >& mesh, unsigned int uLodLevel);
|
||||
|
||||
#endif //__OpenGLExample_OpenGLImmediateModeSupport_H__
|
@ -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;
|
||||
}
|
@ -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__
|
@ -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<MarchingCubesVertex<MaterialDensityPair44> >& 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<MarchingCubesVertex<MaterialDensityPair44> >& 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<MarchingCubesVertex<MaterialDensityPair44> >::const_iterator iterVertex = vecVertices.begin(); iterVertex != vecVertices.end(); ++iterVertex)
|
||||
{
|
||||
const MarchingCubesVertex<MaterialDensityPair44> & 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 = static_cast<uint8_t>(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);
|
||||
}
|
@ -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<PolyVox::MarchingCubesVertex<PolyVox::MaterialDensityPair44> >* sourceMesh;
|
||||
};
|
||||
|
||||
OpenGLMesh BuildOpenGLMesh(const PolyVox::Mesh<PolyVox::MarchingCubesVertex<PolyVox::MaterialDensityPair44> >& mesh);
|
||||
void renderRegionVertexBufferObject(const OpenGLMesh& openGLMesh, unsigned int uLodLevel);
|
||||
|
||||
#endif //__OpenGLExample_OpenGLVertexBufferObjectSupport_H__
|
@ -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 <QMouseEvent>
|
||||
|
||||
#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<MaterialDensityPair44>* 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<MarchingCubesVertex<MaterialDensityPair44> > > mesh(new Mesh<MarchingCubesVertex<MaterialDensityPair44> >);
|
||||
MarchingCubesSurfaceExtractor< LargeVolume<MaterialDensityPair44> > 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<MarchingCubesVertex<MaterialDensityPair44> > > 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<float>(width()) / static_cast<float>(height());
|
||||
|
||||
glOrtho(frustumSize*aspect, -frustumSize*aspect, frustumSize, -frustumSize, 1.0, 5000);
|
||||
}
|
@ -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 <QGLWidget>
|
||||
#include <QTimer>
|
||||
|
||||
#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<PolyVox::MaterialDensityPair44>* 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<PolyVox::MaterialDensityPair44>* m_volData;
|
||||
|
||||
//Rather than storing one big mesh, the volume is broken into regions and a mesh is stored for each region
|
||||
std::map<PolyVox::Vector3DUint8, OpenGLMesh, Vector3DUint8Compare> m_mapOpenGLMeshes;
|
||||
std::map<PolyVox::Vector3DUint8, std::shared_ptr<PolyVox::Mesh<PolyVox::MarchingCubesVertex<PolyVox::MaterialDensityPair44> > >, Vector3DUint8Compare> m_mapMeshes;
|
||||
|
||||
unsigned int m_uRegionSideLength;
|
||||
unsigned int m_uVolumeWidthInRegions;
|
||||
unsigned int m_uVolumeHeightInRegions;
|
||||
unsigned int m_uVolumeDepthInRegions;
|
||||
};
|
||||
|
||||
#endif //__PolyVox_OpenGLWidget_H__
|
@ -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<MaterialDensityPair44>* compressor = new RLEBlockCompressor<MaterialDensityPair44>();
|
||||
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user