From f0eb6a7b101afba52f33c5286e15a862ee68c64e Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 7 May 2011 16:06:08 +0200 Subject: Replace static heightmap with perlin noise using libnoise and noiseutils. --- terrain.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 terrain.cpp (limited to 'terrain.cpp') diff --git a/terrain.cpp b/terrain.cpp new file mode 100644 index 0000000..a44d0d3 --- /dev/null +++ b/terrain.cpp @@ -0,0 +1,32 @@ +#include "terrain.h" + +#include +#include "noiseutils/noiseutils.h" + +#include + +using namespace noise; + +float *Terrain::generate_heights(int x, int y, int width, int height) { + module::Perlin mod; + mod.SetSeed(0); + + utils::NoiseMap heightmap; + utils::NoiseMapBuilderPlane heightmap_builder; + + heightmap_builder.SetSourceModule(mod); + heightmap_builder.SetDestNoiseMap(heightmap); + + heightmap_builder.SetDestSize(width, height); + heightmap_builder.SetBounds(x / 100, (x+width) / 100, y / 100, (y+height) / 100); + 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] = 10*(1+heightmap.GetValue(i, j)); + } + } + + return heights; +} -- cgit v1.2.3