diff --git a/examples/Paging/main.cpp b/examples/Paging/main.cpp index 20a272c6..ab40f514 100644 --- a/examples/Paging/main.cpp +++ b/examples/Paging/main.cpp @@ -27,6 +27,8 @@ freely, subject to the following restrictions: #include "PolyVoxCore/MaterialDensityPair.h" #include "PolyVoxCore/CubicSurfaceExtractorWithNormals.h" #include "PolyVoxCore/MarchingCubesSurfaceExtractor.h" +#include "PolyVoxCore/Pager.h" +#include "PolyVoxCore/RLECompressor.h" #include "PolyVoxCore/SurfaceMesh.h" #include "PolyVoxCore/LargeVolume.h" @@ -197,6 +199,68 @@ void createSphereInVolume(LargeVolume& volData, Vector3DF } } +/** + * Generates data using Perlin noise. + */ +class PerlinNoisePager : public PolyVox::Pager +{ +public: + /// Constructor + PerlinNoisePager() + :Pager() + { + } + + /// Destructor + virtual ~PerlinNoisePager() {}; + + virtual void dataRequiredHandler(const ConstVolumeProxy& volumeProxy, const Region& region) + { + Perlin perlin(2,2,1,234); + + for(int x = region.getLowerX(); x <= region.getUpperX(); x++) + { + for(int y = region.getLowerY(); y <= region.getUpperY(); y++) + { + float perlinVal = perlin.Get(x / static_cast(255-1), y / static_cast(255-1)); + perlinVal += 1.0f; + perlinVal *= 0.5f; + perlinVal *= 255; + for(int z = region.getLowerZ(); z <= region.getUpperZ(); z++) + { + MaterialDensityPair44 voxel; + if(z < perlinVal) + { + const int xpos = 50; + const int zpos = 100; + if((x-xpos)*(x-xpos) + (z-zpos)*(z-zpos) < 200) { + // tunnel + voxel.setMaterial(0); + voxel.setDensity(MaterialDensityPair44::getMinDensity()); + } else { + // solid + voxel.setMaterial(245); + voxel.setDensity(MaterialDensityPair44::getMaxDensity()); + } + } + else + { + voxel.setMaterial(0); + voxel.setDensity(MaterialDensityPair44::getMinDensity()); + } + + volumeProxy.setVoxelAt(x, y, z, voxel); + } + } + } + } + + virtual void dataOverflowHandler(const ConstVolumeProxy& /*volumeProxy*/, const Region& region) + { + std::cout << "warning unloading region: " << region.getLowerCorner() << " -> " << region.getUpperCorner() << std::endl; + } +}; + void load(const ConstVolumeProxy& volume, const PolyVox::Region& reg) { Perlin perlin(2,2,1,234); @@ -250,10 +314,12 @@ int main(int argc, char *argv[]) OpenGLWidget openGLWidget(0); openGLWidget.show(); + RLECompressor* compressor = new RLECompressor(); + //If these lines don't compile, please try commenting them out and using the two lines after //(you will need Boost for this). If you have to do this then please let us know in the forums as //we rely on community feedback to keep the Boost version running. - LargeVolume volData(&load, &unload, 256); + LargeVolume volData(compressor, &load, &unload, 64); //LargeVolume volData(polyvox_bind(&load, polyvox_placeholder_1, polyvox_placeholder_2), // polyvox_bind(&unload, polyvox_placeholder_1, polyvox_placeholder_2), 256); volData.setMaxNumberOfBlocksInMemory(4096); @@ -269,7 +335,7 @@ int main(int argc, char *argv[]) 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; //volData.setBlockCacheSize(64); - PolyVox::Region reg(Vector3DInt32(-255,0,0), Vector3DInt32(255,255,255)); + PolyVox::Region reg(Vector3DInt32(-63,0,0), Vector3DInt32(63,63,255)); std::cout << "Prefetching region: " << reg.getLowerCorner() << " -> " << reg.getUpperCorner() << std::endl; volData.prefetch(reg); std::cout << "Memory usage: " << (volData.calculateSizeInBytes()/1024.0/1024.0) << "MB" << std::endl; diff --git a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h index 49ac5637..ad259ace 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h @@ -36,6 +36,7 @@ namespace PolyVox /** * Provides an interface for performing paging of data. */ + template class FilePager : public Pager { public: @@ -58,6 +59,7 @@ namespace PolyVox protected: std::string m_strFolderName; + }; } #endif //__PolyVox_FilePager_H__ diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h index a7b7d72f..ae504a20 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h @@ -248,6 +248,7 @@ namespace PolyVox /// Constructor for creating a very large paging volume. LargeVolume ( + Compressor* pCompressor, polyvox_function&, const Region&)> dataRequiredHandler, polyvox_function&, const Region&)> dataOverflowHandler, uint16_t uBlockSideLength = 32 diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index e9967114..4ea2b2af 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -37,11 +37,13 @@ namespace PolyVox template LargeVolume::LargeVolume ( + Compressor* pCompressor, polyvox_function&, const Region&)> dataRequiredHandler, polyvox_function&, const Region&)> dataOverflowHandler, uint16_t uBlockSideLength ) :BaseVolume(Region::MaxRegion) + ,m_pCompressor(pCompressor) { m_funcDataRequiredHandler = dataRequiredHandler; m_funcDataOverflowHandler = dataOverflowHandler; diff --git a/library/PolyVoxCore/include/PolyVoxCore/Pager.h b/library/PolyVoxCore/include/PolyVoxCore/Pager.h index 1281fdba..706640aa 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Pager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Pager.h @@ -33,6 +33,7 @@ namespace PolyVox /** * Provides an interface for performing paging of data. */ + template class Pager { public: @@ -43,6 +44,7 @@ namespace PolyVox virtual void dataRequiredHandler(const ConstVolumeProxy& volumeProxy, const Region& region); virtual void dataOverflowHandler(const ConstVolumeProxy& volumeProxy, const Region& region); + }; } #endif //__PolyVox_Pager_H__