diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-07-03 15:31:06 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-07-03 15:31:06 +0200 |
commit | c24d988742997af9e4689e0836a6073cdd302c1f (patch) | |
tree | 1327de6e00fb2dcc4156a85896393b1e7c668714 /terrain_generator.cpp | |
parent | 7add4dd07e17c5a6ba0a2a1d9700d72055b812ae (diff) |
Diffstat (limited to 'terrain_generator.cpp')
-rw-r--r-- | terrain_generator.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/terrain_generator.cpp b/terrain_generator.cpp index a57f5d1..fe9ddd0 100644 --- a/terrain_generator.cpp +++ b/terrain_generator.cpp @@ -2,6 +2,8 @@ #include <noise/noise.h> #include "noiseutils/noiseutils.h" +#include <boost/random.hpp> +#include <boost/random/variate_generator.hpp> using namespace noise; @@ -46,3 +48,55 @@ float *TerrainGenerator::get_chunk(int64_t x, int64_t y, unsigned int width, uns h = generate_heights(x, y, width, height); return h; } + +// TODO: merge this into terrain height generation above somehow +std::list<Vector3> TerrainGenerator::generate_objects(int64_t x, int64_t y, unsigned int width, unsigned int height) { + std::list<Vector3> trees; + + const double scale_factor = 0.004; + // "base" needs to match the terrain heightmap noise + module::Perlin base; + base.SetSeed(seed); + + module::Const low; + low.SetConstValue(0); + + module::Select final; + final.SetSourceModule(0, low); + final.SetSourceModule(1, base); + final.SetControlModule(base); + const float lower = -.24; + const float upper = .19; + final.SetBounds(lower, upper); + + utils::NoiseMap heightmap; + utils::NoiseMapBuilderPlane heightmap_builder; + + heightmap_builder.SetSourceModule(final); + heightmap_builder.SetDestNoiseMap(heightmap); + + heightmap_builder.SetDestSize(width, height); + heightmap_builder.SetBounds((double)x * scale_factor, (double)(x+33) * scale_factor, + (double)y * scale_factor, (double)(y+33) * scale_factor); + heightmap_builder.Build(); + + boost::mt19937 rng(seed); + boost::normal_distribution<float> nd(-2, .7); + boost::variate_generator<boost::mt19937&, boost::normal_distribution<float> > r(rng, nd); + + for(unsigned int i = 0; i < 32; i++) { + for(unsigned int j = 0; j < 32; j++) { + if(heightmap.GetValue(i, j) > 0 && r() > 0) + trees.push_back(Vector3(x + i, 0, y + j)); + } + } + + return trees; +} + +std::list<Vector3> TerrainGenerator::get_objects(int64_t x, int64_t y, unsigned int width, unsigned int height) { + if(has_objects(x, y)) + return load_objects(x, y); + else + return generate_objects(x, y, width, height); +} |