libnoise logo

A portable, open-source, coherent noise-generating library for C++


Tutorial 4: Modifying the parameters of the noise module

In this tutorial, you'll modify the output of a Perlin-noise module to generate terrain height maps with different terrain features.

Before you begin

Before you begin this tutorial, open the source file you created in the previous tutorial. The code in that file should look like this:

#include <noise/noise.h>
#include "noiseutils.h"

using namespace noise;

int main (int argc, char** argv)
{
  module::Perlin myModule;

  utils::NoiseMap heightMap;
  utils::NoiseMapBuilderPlane heightMapBuilder;
  heightMapBuilder.SetSourceModule (myModule);
  heightMapBuilder.SetDestNoiseMap (heightMap);
  heightMapBuilder.SetDestSize (256, 256);
  heightMapBuilder.SetBounds (6.0, 10.0, 1.0, 5.0);
  heightMapBuilder.Build ();

  utils::RendererImage renderer;
  utils::Image image;
  renderer.SetSourceNoiseMap (heightMap);
  renderer.SetDestImage (image);
  renderer.ClearGradient ();
  renderer.AddGradientPoint (-1.0000, utils::Color (  0,   0, 128, 255)); // deeps
  renderer.AddGradientPoint (-0.2500, utils::Color (  0,   0, 255, 255)); // shallow
  renderer.AddGradientPoint ( 0.0000, utils::Color (  0, 128, 255, 255)); // shore
  renderer.AddGradientPoint ( 0.0625, utils::Color (240, 240,  64, 255)); // sand
  renderer.AddGradientPoint ( 0.1250, utils::Color ( 32, 160,   0, 255)); // grass
  renderer.AddGradientPoint ( 0.3750, utils::Color (224, 224,   0, 255)); // dirt
  renderer.AddGradientPoint ( 0.7500, utils::Color (128, 128, 128, 255)); // rock
  renderer.AddGradientPoint ( 1.0000, utils::Color (255, 255, 255, 255)); // snow
  renderer.EnableLight ();
  renderer.SetLightContrast (3.0); // Triple the contrast
  renderer.SetLightBrightness (2.0); // Double the brightness
  renderer.Render ();

  utils::WriterBMP writer;
  writer.SetSourceImage (image);
  writer.SetDestFilename ("tutorial.bmp");
  writer.WriteDestFile ();

  return 0;
}

Modifying the number of octaves that generate Perlin noise

So far in these tutorials, you've been using a Perlin-noise module to generate the coherent-noise values for a terrain height map. For this tutorial, it is helpful to understand what exactly Perlin noise is — it's the sum of several coherent-noise functions with ever-increasing frequencies and ever-decreasing amplitudes. Each of these functions is called an octave, because each octave has double the frequency of the previous octave. Musical tones have this property as well.

The following images show how six octaves combine to form Perlin noise:

Octaves combine to form Perlin noise

If you examine the above images, you'll notice a couple of things:

  • The higher the octave number, the "busier" its associated image becomes. This is because higher octaves have higher frequencies than lower octaves.
  • The higher the octave number, the grayer its associated image becomes. This is because higher octaves have lower amplitudes than lower octaves. (In the above diagram, zero is represented by a neutral gray.)

You can modify the number of octaves for a Perlin-noise module by calling its SetOctaveCount() method. Add the following highlighted code:

  module::Perlin myModule;
  myModule.SetOctaveCount (6);

The above code generates Perlin noise with six octaves, which is the default. Change this to any value between 1 and 6. When you've changed this value, compile and run the program again, then open the tutorial.bmp image file.

Here are some renderings of terrain height maps that were generated by Perlin-noise modules with one, two, three, and four octaves:

Terrain height map generated by one-octave Perlin noise

Terrain height map generated by two-octave Perlin noise

Terrain height map generated by three-octave Perlin noise

Terrain height map generated by four-octave Perlin noise

Notice that the amount of detail increases when the number of octaves increases. Beyond a certain number of octaves (determined by the resolution of the terrain height map), the frequency of the Perlin noise is greater than the resolution of the height map and you are doing more work than needed, for no effect.

Modifying the frequency of the Perlin noise

Next, you'll modify the frequency of the Perlin-noise module. The frequency determines how many changes occur along a unit length. Increasing the frequency will increase the number of terrain features (and also decrease the size of these features) in a terrain height map.

To modify the frequency of the Perlin-noise module, call its SetFrequency() method. Add the following highlighted code:

  module::Perlin myModule;
  myModule.SetOctaveCount (6);
  myModule.SetFrequency (1.0);

This will generate Perlin noise with a frequency of 1, which is the default. Change this to any value between 1 and 16. When you've changed this value, compile and run the program again, then open the tutorial.bmp image file.

Here are some renderings of terrain height maps that were generated by Perlin-noise modules with frequencies of 1, 2, 4, and 8:

Terrain height map generated by Perlin noise with a frequency of 1

Terrain height map generated by Perlin noise with a frequency of 2

Terrain height map generated by Perlin noise with a frequency of 4

Terrain height map generated by Perlin noise with a frequency of 8

If you use an extremely high frequency (300+), your height map won't look much different than non-coherent noise.

Modifying the persistence value of the Perlin noise

Finally, you'll modify the persistence value of the Perlin-noise module. The persistence value determines how quickly the amplitudes fall for each successive octave. Increasing the persistence value will create a rougher terrain height map, while decreasing the persistence value will create a smoother height map.

To modify the persistence value of the Perlin-noise module, call its SetPersistence() method. Add the following highlighted code:

  module::Perlin myModule;
  myModule.SetOctaveCount (6);
  myModule.SetFrequency (1.0);
  myModule.SetPersistence (0.5);

This will generate Perlin noise with a persistence value of 0.5, which is the default. Change this to any value between 0 and 1. When you've changed this value, compile and run the program again, then open the tutorial.bmp image file.

Here are some renderings of terrain height maps that were generated by Perlin-noise modules with persistence values of 1/4, 1/2, and 3/4:

Terrain height map generated by Perlin noise with a persistence value of 1/4

Terrain height map generated by Perlin noise with a persistence value of 1/2

Terrain height map generated by Perlin noise with a persistence value of 3/4

Conclusion

In this tutorial, you've modified the output of the Perlin-noise module to modify the terrain features of your terrain height map. By now, you should understand how each of these methods change the height map.

Before doing the next tutorial, you may want to generate terrain height maps with different combinations of octave counts, frequencies, and persistence values to see what kinds of interesting height maps you can create.