Erosion 1

From DaveWiki

Jump to: navigation, search

The first step to applying erosion effects on computers generated landscapes is to actually generate the land. For this we'll be using the libnoise library to generate Perlin Noise.

The following program is very simple:

  • Create a 256x256 pixel height map
  • Set the output coordinates of the height map to (0,0)-(1,1)
  • Generate the height map and save as a grayscale bitmap
#include "noise/noise.h"
#include "noiseutils/noiseutils.h"
class CErosionSample1
	utils::NoiseMap		m_HeightMap;
	module::Perlin		m_NoiseSource;
	static const int   	m_Width  = 256;
	static const int   	m_Height = 256;
	virtual void CreateBaseNoise (void)
		utils::NoiseMapBuilderPlane HeightMapBuilder;
		HeightMapBuilder.SetSourceModule (m_NoiseSource);
		HeightMapBuilder.SetDestNoiseMap (m_HeightMap);
		HeightMapBuilder.SetDestSize (m_Width, m_Height);
		HeightMapBuilder.SetBounds (0.0, 1.0, 0.0, 1.0);
		HeightMapBuilder.Build ();
void OutputHeightMapGrayScale (const char* pFilename, utils::NoiseMap &HeightMap)
	utils::RendererImage	Renderer;
	utils::Image		OutputImage;
	utils::WriterBMP	BMPWriter;
	Renderer.SetSourceNoiseMap (HeightMap);
	Renderer.SetDestImage (OutputImage);
	Renderer.Render ();
	BMPWriter.SetSourceImage (OutputImage);
	BMPWriter.SetDestFilename (pFilename);
	BMPWriter.WriteDestFile ();
int _tmain(int argc, _TCHAR* argv[])
	CErosionSample1 ErosionSample;
	OutputHeightMapGrayScale("sample1.bmp", ErosionSample.m_HeightMap);
	return 0;

The resulting image is:

One of the benefits of using Perlin noise as a source is that it allows large amount of panning and zooming without additional effort. For example, zooming out and into the bottom left of our sample image a few times (original image is the far left on the second row):
Image:Erosion1-c.jpg Image:Erosion1-b.jpg Image:Erosion1-a.jpg Image:Erosion1-0.jpg
Image:Erosion1-1.jpg Image:Erosion1-2.jpg Image:Erosion1-3.jpg Image:Erosion1-4.jpg
Image:Erosion1-5.jpg Image:Erosion1-6.jpg Image:Erosion1-7.jpg Image:Erosion1-8.jpg
Note that some of the images were color corrected to make the later zoom levels more distinct. The last zoom level represents 1 pixel in the original 256x256 original sample image and only 0.06 pixels in the smallest zoom level. If we expanded the entire first image (smallest zoom) to the magnification of the last (largest zoom) the resulting image would be over 1 million pixels in size.

Personal tools