From 9fa6460f22be5482239d0b903af985a519f77166 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 1 Jun 2011 19:28:24 +0200 Subject: Initial commit. --- terrain_generator.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 terrain_generator.cpp (limited to 'terrain_generator.cpp') diff --git a/terrain_generator.cpp b/terrain_generator.cpp new file mode 100644 index 0000000..a57f5d1 --- /dev/null +++ b/terrain_generator.cpp @@ -0,0 +1,48 @@ +#include "terrain_generator.h" + +#include +#include "noiseutils/noiseutils.h" + +using namespace noise; + +TerrainGenerator::TerrainGenerator(int seed, fs::path root) : TerrainLoader(root) { + this->seed = seed; +} + +float *TerrainGenerator::generate_heights(int64_t x, int64_t y, unsigned int width, unsigned 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); + // subtract by one due to chunk overlapping + heightmap_builder.SetBounds((double)(x-1) * scale_factor, (double)(x-1+width) * scale_factor, + (double)(y-1) * scale_factor, (double)(y-1+height) * scale_factor); + heightmap_builder.Build(); + + float *heights = new float[width*height]; + for(unsigned int i = 0; i < width; i++) { + for(unsigned 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 *TerrainGenerator::get_chunk(int64_t x, int64_t y, unsigned int width, unsigned 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; +} -- cgit v1.2.3