Refactoring example code.

This commit is contained in:
David Williams 2015-02-20 11:23:17 +01:00
parent 3f849e19a4
commit 565aa21799
5 changed files with 232 additions and 198 deletions

View File

@ -72,7 +72,6 @@ public:
BasicExample(QWidget *parent) BasicExample(QWidget *parent)
:OpenGLWidget(parent) :OpenGLWidget(parent)
{ {
} }
protected: protected:

View File

@ -72,7 +72,6 @@ public:
DecodeOnGPUExample(QWidget *parent) DecodeOnGPUExample(QWidget *parent)
:OpenGLWidget(parent) :OpenGLWidget(parent)
{ {
} }
protected: protected:
@ -114,8 +113,8 @@ protected:
} }
private: private:
OpenGLMeshData buildOpenGLMeshData(const PolyVox::Mesh< PolyVox::MarchingCubesVertex< uint8_t > >& surfaceMesh, const PolyVox::Vector3DInt32& translation = PolyVox::Vector3DInt32(0, 0, 0), float scale = 1.0f) 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 // Convienient access to the vertices and indices
const auto& vecIndices = surfaceMesh.getIndices(); const auto& vecIndices = surfaceMesh.getIndices();
const auto& vecVertices = surfaceMesh.getVertices(); const auto& vecVertices = surfaceMesh.getVertices();
@ -167,7 +166,7 @@ OpenGLMeshData buildOpenGLMeshData(const PolyVox::Mesh< PolyVox::MarchingCubesVe
meshData.indexType = sizeof(PolyVox::Mesh< PolyVox::MarchingCubesVertex< uint8_t > >::IndexType) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT; meshData.indexType = sizeof(PolyVox::Mesh< PolyVox::MarchingCubesVertex< uint8_t > >::IndexType) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
return meshData; return meshData;
} }
}; };
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -47,8 +47,17 @@ using namespace std;
const int32_t g_uVolumeSideLength = 128; const int32_t g_uVolumeSideLength = 128;
int main(int argc, char *argv[]) class OpenGLExample : public OpenGLWidget
{ {
public:
OpenGLExample(QWidget *parent)
:OpenGLWidget(parent)
{
}
protected:
void initialize() override
{
FilePager<MaterialDensityPair88>* pager = new FilePager<MaterialDensityPair88>("."); FilePager<MaterialDensityPair88>* pager = new FilePager<MaterialDensityPair88>(".");
PagedVolume<MaterialDensityPair88> volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(g_uVolumeSideLength - 1, g_uVolumeSideLength - 1, g_uVolumeSideLength - 1)), pager); PagedVolume<MaterialDensityPair88> volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(g_uVolumeSideLength - 1, g_uVolumeSideLength - 1, g_uVolumeSideLength - 1)), pager);
@ -69,21 +78,14 @@ int main(int argc, char *argv[])
createSphereInVolume(volData, 20.0f, 1); createSphereInVolume(volData, 20.0f, 1);
cout << "Creating cubes" << std::endl; cout << "Creating cubes" << std::endl;
createCubeInVolume(volData, Vector3DInt32(minPos, minPos, minPos), Vector3DInt32(midPos-1, midPos-1, midPos-1), 0); createCubeInVolume(volData, Vector3DInt32(minPos, minPos, minPos), Vector3DInt32(midPos - 1, midPos - 1, midPos - 1), 0);
createCubeInVolume(volData, Vector3DInt32(midPos+1, midPos+1, minPos), Vector3DInt32(maxPos, maxPos, midPos-1), 0); createCubeInVolume(volData, Vector3DInt32(midPos + 1, midPos + 1, minPos), Vector3DInt32(maxPos, maxPos, midPos - 1), 0);
createCubeInVolume(volData, Vector3DInt32(midPos+1, minPos, midPos+1), Vector3DInt32(maxPos, midPos-1, maxPos), 0); createCubeInVolume(volData, Vector3DInt32(midPos + 1, minPos, midPos + 1), Vector3DInt32(maxPos, midPos - 1, maxPos), 0);
createCubeInVolume(volData, Vector3DInt32(minPos, midPos+1, midPos+1), Vector3DInt32(midPos-1, maxPos, maxPos), 0); createCubeInVolume(volData, Vector3DInt32(minPos, midPos + 1, midPos + 1), Vector3DInt32(midPos - 1, maxPos, maxPos), 0);
createCubeInVolume(volData, Vector3DInt32(1, midPos-10, midPos-10), Vector3DInt32(maxPos-1, midPos+10, midPos+10), MaterialDensityPair44::getMaxDensity()); createCubeInVolume(volData, Vector3DInt32(1, midPos - 10, midPos - 10), Vector3DInt32(maxPos - 1, midPos + 10, midPos + 10), MaterialDensityPair44::getMaxDensity());
createCubeInVolume(volData, Vector3DInt32(midPos-10, 1, midPos-10), Vector3DInt32(midPos+10, maxPos-1, midPos+10), MaterialDensityPair44::getMaxDensity()); createCubeInVolume(volData, Vector3DInt32(midPos - 10, 1, midPos - 10), Vector3DInt32(midPos + 10, maxPos - 1, midPos + 10), MaterialDensityPair44::getMaxDensity());
createCubeInVolume(volData, Vector3DInt32(midPos-10, midPos-10 ,1), Vector3DInt32(midPos+10, midPos+10, maxPos-1), MaterialDensityPair44::getMaxDensity()); createCubeInVolume(volData, Vector3DInt32(midPos - 10, midPos - 10, 1), Vector3DInt32(midPos + 10, midPos + 10, maxPos - 1), MaterialDensityPair44::getMaxDensity());
QApplication app(argc, argv);
OpenGLWidget openGLWidget(0);
openGLWidget.show();
QSharedPointer<QGLShaderProgram> shader(new QGLShaderProgram); QSharedPointer<QGLShaderProgram> shader(new QGLShaderProgram);
@ -99,7 +101,7 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
openGLWidget.setShader(shader); setShader(shader);
QTime time; QTime time;
time.start(); time.start();
@ -129,7 +131,7 @@ int main(int argc, char *argv[])
// Pass the surface to the OpenGL window. Note that we are also passing an offset in this multi-mesh example. This is because // Pass the surface to the OpenGL window. Note that we are also passing an offset in this multi-mesh example. This is because
// the surface extractors return a mesh with 'local space' positions to reduce storage requirements and precision problems. // the surface extractors return a mesh with 'local space' positions to reduce storage requirements and precision problems.
openGLWidget.addMesh(decodedMesh, decodedMesh.getOffset()); addMesh(decodedMesh, decodedMesh.getOffset());
meshCounter++; meshCounter++;
} }
@ -138,9 +140,17 @@ int main(int argc, char *argv[])
cout << "Rendering volume as " << meshCounter << " seperate meshes" << endl; cout << "Rendering volume as " << meshCounter << " seperate meshes" << endl;
setViewableRegion(volData.getEnclosingRegion());
}
};
openGLWidget.setViewableRegion(volData.getEnclosingRegion()); int main(int argc, char *argv[])
{
//Create and show the Qt OpenGL window
QApplication app(argc, argv);
OpenGLExample openGLWidget(0);
openGLWidget.show();
//Run the message pump.
return app.exec(); return app.exec();
} }

View File

@ -139,13 +139,17 @@ public:
} }
}; };
int main(int argc, char *argv[]) class PagingExample : public OpenGLWidget
{ {
//Create and show the Qt OpenGL window public:
QApplication app(argc, argv); PagingExample(QWidget *parent)
OpenGLWidget openGLWidget(0); :OpenGLWidget(parent)
openGLWidget.show(); {
}
protected:
void initialize() override
{
PerlinNoisePager* pager = new PerlinNoisePager(); PerlinNoisePager* pager = new PerlinNoisePager();
PagedVolume<MaterialDensityPair44> volData(PolyVox::Region::MaxRegion(), pager, 64); PagedVolume<MaterialDensityPair44> volData(PolyVox::Region::MaxRegion(), pager, 64);
volData.setMemoryUsageLimit(8 * 1024 * 1024); // 8Mb volData.setMemoryUsageLimit(8 * 1024 * 1024); // 8Mb
@ -153,21 +157,21 @@ int main(int argc, char *argv[])
//createSphereInVolume(volData, 30); //createSphereInVolume(volData, 30);
//createPerlinTerrain(volData); //createPerlinTerrain(volData);
//createPerlinVolumeSlow(volData); //createPerlinVolumeSlow(volData);
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl; std::cout << "Memory usage: " << (volData.calculateSizeInBytes() / 1024.0 / 1024.0) << "MB" << std::endl;
//std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl; //std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl;
PolyVox::Region reg(Vector3DInt32(-255,0,0), Vector3DInt32(255,255,255)); PolyVox::Region reg(Vector3DInt32(-255, 0, 0), Vector3DInt32(255, 255, 255));
std::cout << "Prefetching region: " << reg.getLowerCorner() << " -> " << reg.getUpperCorner() << std::endl; std::cout << "Prefetching region: " << reg.getLowerCorner() << " -> " << reg.getUpperCorner() << std::endl;
volData.prefetch(reg); volData.prefetch(reg);
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl; std::cout << "Memory usage: " << (volData.calculateSizeInBytes() / 1024.0 / 1024.0) << "MB" << std::endl;
//std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl; //std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl;
PolyVox::Region reg2(Vector3DInt32(0,0,0), Vector3DInt32(255,255,255)); PolyVox::Region reg2(Vector3DInt32(0, 0, 0), Vector3DInt32(255, 255, 255));
std::cout << "Flushing region: " << reg2.getLowerCorner() << " -> " << reg2.getUpperCorner() << std::endl; std::cout << "Flushing region: " << reg2.getLowerCorner() << " -> " << reg2.getUpperCorner() << std::endl;
volData.flush(reg2); volData.flush(reg2);
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl; std::cout << "Memory usage: " << (volData.calculateSizeInBytes() / 1024.0 / 1024.0) << "MB" << std::endl;
//std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl; //std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl;
std::cout << "Flushing entire volume" << std::endl; std::cout << "Flushing entire volume" << std::endl;
volData.flushAll(); volData.flushAll();
std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl; std::cout << "Memory usage: " << (volData.calculateSizeInBytes() / 1024.0 / 1024.0) << "MB" << std::endl;
//std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl; //std::cout << "Compression ratio: 1 to " << (1.0/(volData.calculateCompressionRatio())) << std::endl;
//Extract the surface //Extract the surface
@ -177,9 +181,18 @@ int main(int argc, char *argv[])
auto decodedMesh = decodeMesh(mesh); auto decodedMesh = decodeMesh(mesh);
//Pass the surface to the OpenGL window //Pass the surface to the OpenGL window
openGLWidget.addMesh(decodedMesh); addMesh(decodedMesh);
openGLWidget.setViewableRegion(reg2); setViewableRegion(reg2);
}
};
int main(int argc, char *argv[])
{
//Create and show the Qt OpenGL window
QApplication app(argc, argv);
PagingExample openGLWidget(0);
openGLWidget.show();
//Run the message pump. //Run the message pump.
return app.exec(); return app.exec();

View File

@ -68,13 +68,17 @@ void createSphereInVolume(PagedVolume<uint8_t>& volData, float fRadius)
} }
} }
int main(int argc, char *argv[]) class SmoothLODExample : public OpenGLWidget
{ {
//Create and show the Qt OpenGL window public:
QApplication app(argc, argv); SmoothLODExample(QWidget *parent)
OpenGLWidget openGLWidget(0); :OpenGLWidget(parent)
openGLWidget.show(); {
}
protected:
void initialize() override
{
//Create an empty volume and then place a sphere in it //Create an empty volume and then place a sphere in it
PagedVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(63, 63, 63))); PagedVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(63, 63, 63)));
createSphereInVolume(volData, 28); createSphereInVolume(volData, 28);
@ -84,7 +88,7 @@ int main(int argc, char *argv[])
//smoothRegion<PagedVolume, Density8>(volData, volData.getEnclosingRegion()); //smoothRegion<PagedVolume, Density8>(volData, volData.getEnclosingRegion());
//smoothRegion<PagedVolume, Density8>(volData, volData.getEnclosingRegion()); //smoothRegion<PagedVolume, Density8>(volData, volData.getEnclosingRegion());
RawVolume<uint8_t> volDataLowLOD(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(15, 31, 31))); RawVolume<uint8_t> volDataLowLOD(PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(15, 31, 31)));
VolumeResampler< PagedVolume<uint8_t>, RawVolume<uint8_t> > volumeResampler(&volData, PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(31, 63, 63)), &volDataLowLOD, volDataLowLOD.getEnclosingRegion()); VolumeResampler< PagedVolume<uint8_t>, RawVolume<uint8_t> > volumeResampler(&volData, PolyVox::Region(Vector3DInt32(0, 0, 0), Vector3DInt32(31, 63, 63)), &volDataLowLOD, volDataLowLOD.getEnclosingRegion());
volumeResampler.execute(); volumeResampler.execute();
@ -100,10 +104,19 @@ int main(int argc, char *argv[])
auto decodedMeshHighLOD = decodeMesh(meshHighLOD); auto decodedMeshHighLOD = decodeMesh(meshHighLOD);
//Pass the surface to the OpenGL window //Pass the surface to the OpenGL window
openGLWidget.addMesh(decodedMeshHighLOD, Vector3DInt32(30, 0, 0)); addMesh(decodedMeshHighLOD, Vector3DInt32(30, 0, 0));
openGLWidget.addMesh(decodedMeshLowLOD, Vector3DInt32(0, 0, 0), 63.0f / 31.0f); addMesh(decodedMeshLowLOD, Vector3DInt32(0, 0, 0), 63.0f / 31.0f);
openGLWidget.setViewableRegion(volData.getEnclosingRegion()); setViewableRegion(volData.getEnclosingRegion());
}
};
int main(int argc, char *argv[])
{
//Create and show the Qt OpenGL window
QApplication app(argc, argv);
SmoothLODExample openGLWidget(0);
openGLWidget.show();
//Run the message pump. //Run the message pump.
return app.exec(); return app.exec();