207 lines
6.7 KiB
C++
207 lines
6.7 KiB
C++
#include "OpenGLWidget.h"
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include "GradientEstimators.h"
|
|
#include "SurfaceAdjusters.h"
|
|
|
|
//Some namespaces we need
|
|
using namespace std;
|
|
using namespace PolyVox;
|
|
using namespace std;
|
|
|
|
OpenGLWidget::OpenGLWidget(QWidget *parent)
|
|
:QGLWidget(parent)
|
|
{
|
|
m_xRotation = 0;
|
|
m_yRotation = 0;
|
|
m_uRegionSideLength = 16.0f;
|
|
m_distance = -g_uVolumeSideLength / 2.0f;
|
|
|
|
timer = new QTimer(this);
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
|
|
timer->start(0);
|
|
}
|
|
|
|
void OpenGLWidget::setVolume(PolyVox::Volume<PolyVox::uint8_t>* volData)
|
|
{
|
|
//First we free anything from the previous volume (if there was one).
|
|
m_mapOpenGLSurfacePatches.clear();
|
|
m_mapIndexedSurfacePatches.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(PolyVox::uint16_t uRegionZ = 0; uRegionZ < m_uVolumeDepthInRegions; ++uRegionZ)
|
|
{
|
|
for(PolyVox::uint16_t uRegionY = 0; uRegionY < m_uVolumeHeightInRegions; ++uRegionY)
|
|
{
|
|
for(PolyVox::uint16_t uRegionX = 0; uRegionX < m_uVolumeWidthInRegions; ++uRegionX)
|
|
{
|
|
//Create a new surface patch (which is basiaclly the PolyVox term for a mesh).
|
|
IndexedSurfacePatch* ispCurrent = new IndexedSurfacePatch();
|
|
|
|
//Compute the extents of the current region
|
|
//FIXME - This is a little complex? PolyVox could
|
|
//provide more functions for dealing with regions?
|
|
PolyVox::uint16_t regionStartX = uRegionX * m_uRegionSideLength;
|
|
PolyVox::uint16_t regionStartY = uRegionY * m_uRegionSideLength;
|
|
PolyVox::uint16_t regionStartZ = uRegionZ * m_uRegionSideLength;
|
|
|
|
PolyVox::uint16_t regionEndX = regionStartX + m_uRegionSideLength + 1; //Why do we need the '+1' here?
|
|
PolyVox::uint16_t regionEndY = regionStartY + m_uRegionSideLength + 1; //Why do we need the '+1' here?
|
|
PolyVox::uint16_t regionEndZ = regionStartZ + m_uRegionSideLength + 1; //Why do we need the '+1' here?
|
|
|
|
Vector3DInt32 regLowerCorner(regionStartX, regionStartY, regionStartZ);
|
|
Vector3DInt32 regUpperCorner(regionEndX, regionEndY, regionEndZ);
|
|
|
|
//Extract the surface for this region
|
|
extractReferenceSurface(m_volData, PolyVox::Region(regLowerCorner, regUpperCorner), ispCurrent);
|
|
computeNormalsForVertices(m_volData, *ispCurrent, SOBEL_SMOOTHED);
|
|
|
|
//for(int ct = 0; ct < 100; ct++)
|
|
//{
|
|
*ispCurrent = getSmoothedSurface(*ispCurrent);
|
|
//}
|
|
|
|
|
|
Vector3DUint8 v3dRegPos(uRegionX,uRegionY,uRegionZ);
|
|
if(m_bUseOpenGLVertexBufferObjects)
|
|
{
|
|
OpenGLSurfacePatch openGLSurfacePatch = BuildOpenGLSurfacePatch(*ispCurrent);
|
|
m_mapOpenGLSurfacePatches.insert(make_pair(v3dRegPos, openGLSurfacePatch));
|
|
}
|
|
else
|
|
{
|
|
m_mapIndexedSurfacePatches.insert(make_pair(v3dRegPos, ispCurrent));
|
|
}
|
|
//delete ispCurrent;
|
|
}
|
|
}
|
|
}
|
|
|
|
m_distance = m_volData->getLongestSideLength() / -2.0f;
|
|
}
|
|
}
|
|
|
|
void OpenGLWidget::initializeGL()
|
|
{
|
|
m_bUseOpenGLVertexBufferObjects = true;
|
|
if(m_bUseOpenGLVertexBufferObjects)
|
|
{
|
|
#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);
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
}
|
|
|
|
void OpenGLWidget::resizeGL(int w, int h)
|
|
{
|
|
glViewport ( 0, 0, w, h );
|
|
glMatrixMode ( GL_PROJECTION ); // Select The Projection Matrix
|
|
glLoadIdentity ( ); // Reset The Projection Matrix
|
|
|
|
float aspect = ( float ) w / ( float ) h;
|
|
glOrtho(-m_distance*aspect, m_distance*aspect, -m_distance, m_distance, 1.0, 5000);
|
|
glMatrixMode ( GL_MODELVIEW ); // Select The Model View Matrix
|
|
glLoadIdentity ( ); // Reset The Model View Matrix
|
|
}
|
|
|
|
void OpenGLWidget::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
|
|
|
|
//Moves the camera back so we can see the volume
|
|
glTranslatef(0.0f, 0.0f, m_distance);
|
|
|
|
//Rotate the volume by the required amount
|
|
//glRotatef(g_xRotation, 1.0f, 0.0f, 0.0f);
|
|
//glRotatef(g_yRotation, 0.0f, 1.0f, 0.0f);
|
|
|
|
;
|
|
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(PolyVox::uint16_t uRegionZ = 0; uRegionZ < m_uVolumeDepthInRegions; ++uRegionZ)
|
|
{
|
|
for(PolyVox::uint16_t uRegionY = 0; uRegionY < m_uVolumeHeightInRegions; ++uRegionY)
|
|
{
|
|
for(PolyVox::uint16_t uRegionX = 0; uRegionX < m_uVolumeWidthInRegions; ++uRegionX)
|
|
{
|
|
Vector3DUint8 v3dRegPos(uRegionX,uRegionY,uRegionZ);
|
|
if(m_bUseOpenGLVertexBufferObjects)
|
|
{
|
|
renderRegionVertexBufferObject(m_mapOpenGLSurfacePatches[v3dRegPos]);
|
|
}
|
|
else
|
|
{
|
|
IndexedSurfacePatch* ispCurrent = m_mapIndexedSurfacePatches[v3dRegPos];
|
|
renderRegionImmediateMode(*ispCurrent);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
GLenum errCode;
|
|
const GLubyte *errString;
|
|
|
|
if ((errCode = glGetError()) != GL_NO_ERROR)
|
|
{
|
|
errString = gluErrorString(errCode);
|
|
cout << "OpenGL Error: " << errString << endl;
|
|
} // Reset The Current Modelview Matrix
|
|
}
|
|
|
|
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::wheelEvent(QWheelEvent* event)
|
|
{
|
|
m_distance += event->delta() / 120.0f;
|
|
} |