Gamedev Framework (gf)  0.9.0
A C++14 framework for 2D games
Heightmap.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_HEIGHTMAP_H
22 #define GF_HEIGHTMAP_H
23 
24 #include <tuple>
25 
26 #include "Array2D.h"
27 #include "ColorRamp.h"
28 #include "Image.h"
29 #include "Noise.h"
30 #include "Portability.h"
31 #include "Rect.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
44  class GF_API Heightmap {
45  public:
51  Heightmap(Vector2i size);
52 
58  Vector2i getSize() const {
59  return m_data.getSize();
60  }
61 
67  void reset();
68 
75  double getValue(Vector2i position) const {
76  return m_data(position);
77  }
78 
85  void setValue(Vector2i position, double value) {
86  m_data(position) = value;
87  }
88 
94  std::tuple<double, double> getMinMax() const;
95 
107  void normalize(double min = 0.0, double max = 1.0);
108 
116  void addHill(Vector2d center, double radius, double height);
117 
125  void digHill(Vector2d center, double radius, double height);
126 
133  void addNoise(Noise2D& noise, double scale = 1.0);
134 
140  void addValue(double value);
141 
147  void scale(double value);
148 
156  void clamp(double min = 0.0, double max = 1.0);
157 
177  double getSlope(Vector2i position) const;
178 
186  void thermalErosion(unsigned iterations, double talus, double fraction);
187 
197  void hydraulicErosion(unsigned iterations, double rainAmount, double solubility, double evaporation, double capacity);
198 
206  void fastErosion(unsigned iterations, double talus, double fraction);
207 
215  double getErosionScore() const;
216 
224  enum class Render {
225  Colored,
226  Shaded,
227  };
228 
239  Heightmap subMap(RectI area) const;
240 
250  Image copyToGrayscaleImage() const;
251 
268  Image copyToColoredImage(const ColorRamp& ramp, double waterLevel = 0.5, Render render = Render::Colored) const;
269 
274  private:
275  Array2D<double, int> m_data;
276  };
277 
278 #ifndef DOXYGEN_SHOULD_SKIP_THIS
279 }
280 #endif
281 }
282 
283 #endif // GF_HEIGHTMAP_H
void setValue(Vector2i position, double value)
Set the value at the specified position.
Definition: Heightmap.h:85
void scale(Matrix3f &mat, Vector2f factor)
Combine the current transform with a scaling.
A color ramp.
Definition: ColorRamp.h:41
A heightmap.
Definition: Heightmap.h:44
2D A noise function
Definition: Noise.h:35
constexpr T clamp(T val, T lo, T hi)
Clamping function.
Definition: Math.h:260
Vector2i getSize() const
Get the size of the heightmap.
Definition: Heightmap.h:58
Render
Rendering mode.
Definition: Heightmap.h:224
Class for loading, manipulating and saving images.
Definition: Image.h:92
The namespace for gf classes.
Definition: Action.h:34
double getValue(Vector2i position) const
Get the value at the specified position.
Definition: Heightmap.h:75