#include "terrain_loader.h" #include #include "noiseutils/noiseutils.h" #include #include #include using namespace noise; TerrainLoader::TerrainLoader(int seed, fs::path root) { this->seed = seed; this->root = root; } TerrainLoader::~TerrainLoader() { } float *TerrainLoader::generate_heights(int x, int y, int width, int height) { const double scale_factor = 0.004; module::Perlin mod; mod.SetSeed(seed); utils::NoiseMap heightmap; utils::NoiseMapBuilderPlane heightmap_builder; heightmap_builder.SetSourceModule(mod); heightmap_builder.SetDestNoiseMap(heightmap); heightmap_builder.SetDestSize(width, height); heightmap_builder.SetBounds((double)x * scale_factor, (double)(x+width) * scale_factor, (double)y * scale_factor, (double)(y+height) * scale_factor); heightmap_builder.Build(); float *heights = new float[width*height]; for(int i = 0; i < width; i++) { for(int j = 0; j < height; j++) { heights[i*height + j] = 50*(1+heightmap.GetValue(i, j)); } } save_chunk(heights, x, y, width, height); return heights; } float *TerrainLoader::get_chunk(int x, int y, int width, int height) { float *h; if(has_chunk(x, y)) h = load_chunk(x, y, width, height); else h = generate_heights(x, y, width, height); return h; } bool TerrainLoader::has_chunk(int x, int y) { return fs::exists(root / (boost::format("%d.%d.chunk") % x % y).str()); } // TODO: Handle big-endian platforms void TerrainLoader::save_chunk(float *chunk, int x, int y, int width, int height) { fs::path p = root / (boost::format("%d.%d.chunk") % x % y).str(); fs::ofstream os(p, std::ios::out | std::ios::binary); uint64_t w = width; uint64_t h = height; os.write((const char*)&w, sizeof(w)); os.write((const char*)&h, sizeof(h)); os.write((const char*)chunk, width*height * sizeof(float)); os.close(); } // TODO: Handle big-endian platforms // NOTE: assumes width <= chunk_size+1, likewise for height float *TerrainLoader::load_chunk(int x, int y, int width, int height) { fs::path p = root / (boost::format("%d.%d.chunk") % x % y).str(); fs::ifstream is(p, std::ios::in | std::ios::binary); uint64_t w, h; w = h = 0; is.read((char*)&w, sizeof(w)); is.read((char*)&h, sizeof(h)); // TODO: should return w and h, fix calling code if(w != width || h != height) throw std::runtime_error("width or height doesn't match"); float *chunk = new float[width*height]; is.read((char*)chunk, width*height*sizeof(float)); is.close(); return chunk; }