void createHeightmapInVolume(Volume<MaterialDensityPair44>* volData, float* heightmap, int width, int height) { //This vector hold the position of the center of the volume Vector3DFloat v3dVolCenter(volData->getWidth() / 2, volData->getHeight() / 2, volData->getDepth() / 2); //This three-level for loop iterates over every voxel in the volume for (int z = 0; z < volData->getWidth(); z++) { for (int x = 0; x < volData->getDepth(); x++) { float height = volData->getHeight()-1; height *= heightmap[x+z*width]; int h = int(height); if(h < 1) h = 1; for (int y = 0; y < h; y++) { Vector3DFloat v3dCurrentPos(x,y,z); uint8_t uDensity = MaterialDensityPair44::getMaxDensity(); MaterialDensityPair44 voxel = volData->getVoxelAt(x,y,z); voxel.setDensity(uDensity); volData->setVoxelAt(x, y, z, voxel); } } } }
virtual void pageIn(const PolyVox::Region& region, PagedVolume<MaterialDensityPair44>::Chunk* pChunk) { 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<float>(255 - 1), y / static_cast<float>(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()); } // Voxel position within a chunk always start from zero. So if a chunk represents region (4, 8, 12) to (11, 19, 15) // then the valid chunk voxels are from (0, 0, 0) to (7, 11, 3). Hence we subtract the lower corner position of the // region from the volume space position in order to get the chunk space position. pChunk->setVoxel(x - region.getLowerX(), y - region.getLowerY(), z - region.getLowerZ(), voxel); } } } }