From 3f849e19a45642b8be2fe899c65582aa11650f16 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 15 Feb 2015 12:03:31 +0100 Subject: [PATCH] Started refactoring examples and got DecodeOnGPUExample working. --- CMakeLists.txt | 2 +- examples/Basic/main.cpp | 28 ++++++++--- examples/DecodeOnGPU/main.cpp | 85 +++++++++++++++++++------------- examples/common/OpenGLWidget.cpp | 2 + examples/common/OpenGLWidget.h | 4 ++ 5 files changed, 78 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05c35799..6194898e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,7 @@ IF(ENABLE_EXAMPLES AND Qt5OpenGL_FOUND) ADD_SUBDIRECTORY(examples/Paging) ADD_SUBDIRECTORY(examples/OpenGL) ADD_SUBDIRECTORY(examples/SmoothLOD) - #ADD_SUBDIRECTORY(examples/DecodeOnGPU) + ADD_SUBDIRECTORY(examples/DecodeOnGPU) ADD_SUBDIRECTORY(examples/Python) SET(BUILD_EXAMPLES ON) ELSE() diff --git a/examples/Basic/main.cpp b/examples/Basic/main.cpp index 3fba8964..f8207e4c 100644 --- a/examples/Basic/main.cpp +++ b/examples/Basic/main.cpp @@ -66,13 +66,18 @@ void createSphereInVolume(PagedVolume& volData, float fRadius) } } -int main(int argc, char *argv[]) +class BasicExample : public OpenGLWidget { - //Create and show the Qt OpenGL window - QApplication app(argc, argv); - OpenGLWidget openGLWidget(0); - openGLWidget.show(); +public: + BasicExample(QWidget *parent) + :OpenGLWidget(parent) + { + } + +protected: + void initialize() override + { //Create an empty volume and then place a sphere in it PagedVolume volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(63, 63, 63))); createSphereInVolume(volData, 30); @@ -86,9 +91,18 @@ int main(int argc, char *argv[]) auto decodedMesh = decodeMesh(mesh); //Pass the surface to the OpenGL window - openGLWidget.addMesh(decodedMesh); + addMesh(decodedMesh); //openGLWidget.addMesh(mesh2); - openGLWidget.setViewableRegion(volData.getEnclosingRegion()); + setViewableRegion(volData.getEnclosingRegion()); + } +}; + +int main(int argc, char *argv[]) +{ + //Create and show the Qt OpenGL window + QApplication app(argc, argv); + BasicExample openGLWidget(0); + openGLWidget.show(); //Run the message pump. return app.exec(); diff --git a/examples/DecodeOnGPU/main.cpp b/examples/DecodeOnGPU/main.cpp index 52091119..0ce9a2ac 100644 --- a/examples/DecodeOnGPU/main.cpp +++ b/examples/DecodeOnGPU/main.cpp @@ -66,6 +66,54 @@ void createSphereInVolume(PagedVolume& volData, float fRadius) } } +class DecodeOnGPUExample : public OpenGLWidget +{ +public: + DecodeOnGPUExample(QWidget *parent) + :OpenGLWidget(parent) + { + + } + +protected: + void initialize() override + { + QSharedPointer shader(new QGLShaderProgram); + + if (!shader->addShaderFromSourceFile(QGLShader::Vertex, ":/decode.vert")) + { + std::cerr << shader->log().toStdString() << std::endl; + exit(EXIT_FAILURE); + } + + if (!shader->addShaderFromSourceFile(QGLShader::Fragment, ":/decode.frag")) + { + std::cerr << shader->log().toStdString() << std::endl; + exit(EXIT_FAILURE); + } + + setShader(shader); + + //Create an empty volume and then place a sphere in it + PagedVolume volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(63, 63, 63))); + createSphereInVolume(volData, 30); + + // Extract the surface for the specified region of the volume. Uncomment the line for the kind of surface extraction you want to see. + //auto mesh = extractCubicMesh(&volData, volData.getEnclosingRegion()); + auto mesh = extractMarchingCubesMesh(&volData, volData.getEnclosingRegion()); + + // The surface extractor outputs the mesh in an efficient compressed format which is not directly suitable for rendering. The easiest approach is to + // decode this on the CPU as shown below, though more advanced applications can upload the compressed mesh to the GPU and decompress in shader code. + //auto decodedMesh = decodeMesh(mesh); + + //Pass the surface to the OpenGL window + OpenGLMeshData meshData = buildOpenGLMeshData(mesh); + addMeshData(meshData); + + setViewableRegion(volData.getEnclosingRegion()); + } + +private: OpenGLMeshData buildOpenGLMeshData(const PolyVox::Mesh< PolyVox::MarchingCubesVertex< uint8_t > >& surfaceMesh, const PolyVox::Vector3DInt32& translation = PolyVox::Vector3DInt32(0, 0, 0), float scale = 1.0f) { // Convienient access to the vertices and indices @@ -120,48 +168,15 @@ OpenGLMeshData buildOpenGLMeshData(const PolyVox::Mesh< PolyVox::MarchingCubesVe return meshData; } +}; int main(int argc, char *argv[]) { //Create and show the Qt OpenGL window QApplication app(argc, argv); - OpenGLWidget openGLWidget(0); + DecodeOnGPUExample openGLWidget(0); openGLWidget.show(); - QSharedPointer shader(new QGLShaderProgram); - - if (!shader->addShaderFromSourceFile(QGLShader::Vertex, ":/decode.vert")) - { - std::cerr << shader->log().toStdString() << std::endl; - exit(EXIT_FAILURE); - } - - if (!shader->addShaderFromSourceFile(QGLShader::Fragment, ":/decode.frag")) - { - std::cerr << shader->log().toStdString() << std::endl; - exit(EXIT_FAILURE); - } - - openGLWidget.setShader(shader); - - //Create an empty volume and then place a sphere in it - PagedVolume volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(63, 63, 63))); - createSphereInVolume(volData, 30); - - // Extract the surface for the specified region of the volume. Uncomment the line for the kind of surface extraction you want to see. - //auto mesh = extractCubicMesh(&volData, volData.getEnclosingRegion()); - auto mesh = extractMarchingCubesMesh(&volData, volData.getEnclosingRegion()); - - // The surface extractor outputs the mesh in an efficient compressed format which is not directly suitable for rendering. The easiest approach is to - // decode this on the CPU as shown below, though more advanced applications can upload the compressed mesh to the GPU and decompress in shader code. - //auto decodedMesh = decodeMesh(mesh); - - //Pass the surface to the OpenGL window - OpenGLMeshData meshData = buildOpenGLMeshData(mesh); - openGLWidget.addMeshData(meshData); - - openGLWidget.setViewableRegion(volData.getEnclosingRegion()); - //Run the message pump. return app.exec(); } \ No newline at end of file diff --git a/examples/common/OpenGLWidget.cpp b/examples/common/OpenGLWidget.cpp index 61d23c57..ce774e30 100644 --- a/examples/common/OpenGLWidget.cpp +++ b/examples/common/OpenGLWidget.cpp @@ -121,6 +121,8 @@ void OpenGLWidget::initializeGL() // Initial setup of camera. setupWorldToCameraMatrix(); + + initialize(); } void OpenGLWidget::resizeGL(int w, int h) diff --git a/examples/common/OpenGLWidget.h b/examples/common/OpenGLWidget.h index 35472003..c411c8b6 100644 --- a/examples/common/OpenGLWidget.h +++ b/examples/common/OpenGLWidget.h @@ -136,6 +136,10 @@ protected: void resizeGL(int w, int h); void paintGL(); + virtual void initialize() + { + } + private: void setupWorldToCameraMatrix();