Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
Public Types | Public Member Functions | List of all members
gf::Heightmap Class Reference

A heightmap. More...

#include <gf/Heightmap.h>

Public Types

enum  Render {
  Render::Colored,
  Render::Shaded
}
 Rendering mode. More...
 

Public Member Functions

 Heightmap (Vector2i size)
 Constructor. More...
 
Vector2i getSize () const
 Get the size of the heightmap. More...
 
void reset ()
 Reset the heightmap. More...
 
double getValue (Vector2i position) const
 Get the value at the specified position. More...
 
void setValue (Vector2i position, double value)
 Set the value at the specified position. More...
 
std::tuple< double, double > getMinMax () const
 Get the minimum and maximum of the heightmap. More...
 
Shaping
void normalize (double min=0.0, double max=1.0)
 Normalize the heightmap. More...
 
void addHill (Vector2d center, double radius, double height)
 Add a hill to the heightmap. More...
 
void digHill (Vector2d center, double radius, double height)
 Dig a hill in the heightmap. More...
 
void addNoise (Noise2D &noise, double scale=1.0)
 Add a noise to the heightmap. More...
 
void addValue (double value)
 Add a constant to the heightmap. More...
 
void scale (double value)
 Scale the values of the heightmap. More...
 
void clamp (double min=0.0, double max=1.0)
 Clamp the values of the heightmap. More...
 
Erosion
double getSlope (Vector2i position) const
 Compute the slope at a position. More...
 
void thermalErosion (unsigned iterations, double talus, double fraction)
 Apply thermal erosion to the heightmap. More...
 
void hydraulicErosion (unsigned iterations, double rainAmount, double solubility, double evaporation, double capacity)
 Apply hydraulic erosision to the heightmap. More...
 
void fastErosion (unsigned iterations, double talus, double fraction)
 Apply fast erosion to the heightmap. More...
 
double getErosionScore () const
 Compute the erosion score for the heightmap. More...
 
Export
Heightmap subMap (RectI area) const
 Get a sub-map of the heightmap. More...
 
Image copyToGrayscaleImage () const
 Export to a grayscale image. More...
 
Image copyToColoredImage (const ColorRamp &ramp, double waterLevel=0.5, Render render=Render::Colored) const
 Export to a colored image. More...
 

Detailed Description

A heightmap.

See also
gf::midpointDisplacement2D(), gf::diamondSquare2D()

Member Enumeration Documentation

◆ Render

enum gf::Heightmap::Render
strong

Rendering mode.

Enumerator
Colored 

Export to a simply colored image.

Shaded 

Export to a shaded image.

Constructor & Destructor Documentation

◆ Heightmap()

gf::Heightmap::Heightmap ( Vector2i  size)

Constructor.

Parameters
sizeThe size of the heightmap

Member Function Documentation

◆ addHill()

void gf::Heightmap::addHill ( Vector2d  center,
double  radius,
double  height 
)

Add a hill to the heightmap.

Parameters
centerThe center of the hill
radiusThe radius of the hill
heightThe height of the hill

◆ addNoise()

void gf::Heightmap::addNoise ( Noise2D noise,
double  scale = 1.0 
)

Add a noise to the heightmap.

Parameters
noiseThe noise to use
scaleThe scale of the noise

◆ addValue()

void gf::Heightmap::addValue ( double  value)

Add a constant to the heightmap.

Parameters
valueThe value of the constant

◆ clamp()

void gf::Heightmap::clamp ( double  min = 0.0,
double  max = 1.0 
)

Clamp the values of the heightmap.

Parameters
minThe minimum value (defaults to \( 0.0 \))
maxThe maximum value (defaults to \( 1.0 \))
See also
normalize()

◆ copyToColoredImage()

Image gf::Heightmap::copyToColoredImage ( const ColorRamp ramp,
double  waterLevel = 0.5,
Render  render = Render::Colored 
) const

Export to a colored image.

The heightmap is assumed to be normalized.

The color ramp assumes that the water level is at \( 0.5 \). The actual water level can be specified and the ramp is automatically adapted on the fly.

Parameters
rampA color ramp
waterLevelThe actual water level (defaults to \( 0.5 \))
renderThe rendering mode
Returns
A colored image representing the heightmap
See also
copyToGrayscaleImage()

◆ copyToGrayscaleImage()

Image gf::Heightmap::copyToGrayscaleImage ( ) const

Export to a grayscale image.

The heightmap is assumed to be normalized.

Returns
A grayscale image representing the heightmap
See also
copyToColoredImage()

◆ digHill()

void gf::Heightmap::digHill ( Vector2d  center,
double  radius,
double  height 
)

Dig a hill in the heightmap.

Parameters
centerThe center of the hill
radiusThe radius of the hill
heightThe height of the hill

◆ fastErosion()

void gf::Heightmap::fastErosion ( unsigned  iterations,
double  talus,
double  fraction 
)

Apply fast erosion to the heightmap.

Parameters
iterationsThe number of iterations
talusThe maximum difference for a move of material (typically \( \frac{8}{N} \) or more)
fractionThe fraction of material that moves (typically \( 0.5 \))

◆ getErosionScore()

double gf::Heightmap::getErosionScore ( ) const

Compute the erosion score for the heightmap.

The erosion score is the average slope divided by the standard deviation of slopes. A high erosion score means that the terrain has large flat areas and has some steep areas.

◆ getMinMax()

std::tuple<double, double> gf::Heightmap::getMinMax ( ) const

Get the minimum and maximum of the heightmap.

Returns
A pair with the first element being the minimum and the second element the maximum

◆ getSize()

Vector2i gf::Heightmap::getSize ( ) const
inline

Get the size of the heightmap.

Returns
The current size of the heightmap

◆ getSlope()

double gf::Heightmap::getSlope ( Vector2i  position) const

Compute the slope at a position.

The slope is defined as "the greatest of the height differences between the cell and its four neighbours in a Von Neumann neighbourhood".

\[ \text{slope}(x, y) = \max(|h(x,y) - h(x - 1, y)|, |h(x,y) - h(x + 1, y)|, |h(x,y) - h(x, y - 1)|, |h(x,y) - h(x, y + 1)|) \]

Parameters
positionThe given position

◆ getValue()

double gf::Heightmap::getValue ( Vector2i  position) const
inline

Get the value at the specified position.

Parameters
positionA position
Returns
The value at the given position

◆ hydraulicErosion()

void gf::Heightmap::hydraulicErosion ( unsigned  iterations,
double  rainAmount,
double  solubility,
double  evaporation,
double  capacity 
)

Apply hydraulic erosision to the heightmap.

Parameters
iterationsThe number of iterations
rainAmountThe amount of rain each cell receives at each iteration (typically \( 0.01 \))
solubilityThe solubility of the terrain (typically \( 0.01 \))
evaporationThe proportion of evaporated water at each iteration (typically \( 0.5 \))
capacityThe maximum proportion of material that can be carried by water (typically \( 0.01 \))

◆ normalize()

void gf::Heightmap::normalize ( double  min = 0.0,
double  max = 1.0 
)

Normalize the heightmap.

Parameters
minThe new minimum value (defaults to \( 0.0 \))
maxThe new maximum value (defaults to \( 1.0 \))

◆ reset()

void gf::Heightmap::reset ( )

Reset the heightmap.

The values are all set to \( 0.0 \)

◆ scale()

void gf::Heightmap::scale ( double  value)

Scale the values of the heightmap.

Parameters
valueThe factor of scaling

◆ setValue()

void gf::Heightmap::setValue ( Vector2i  position,
double  value 
)
inline

Set the value at the specified position.

Parameters
positionA position
valueThe new value

◆ subMap()

Heightmap gf::Heightmap::subMap ( RectI  area) const

Get a sub-map of the heightmap.

Parameters
areaThe area of the sub-map in the heightmap

◆ thermalErosion()

void gf::Heightmap::thermalErosion ( unsigned  iterations,
double  talus,
double  fraction 
)

Apply thermal erosion to the heightmap.

Parameters
iterationsThe number of iterations
talusThe minimum difference for a move of material (typically \( \frac{4}{N} \))
fractionThe fraction of material that moves (typically \( 0.5 \))