Gamedev Framework (gf)  0.1.0
A C++11 framework for 2D games
Noise.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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_NOISE_H
22 #define GF_NOISE_H
23 
24 #include <cstddef>
25 #include <cstdint>
26 #include <array>
27 #include <functional>
28 
29 #include "Math.h"
30 #include "Portability.h"
31 #include "Vector.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
38  class Random;
39 
40  /**
41  * @ingroup core
42  * @brief Gradient noise
43  *
44  * [Gradient noise](https://en.wikipedia.org/wiki/Gradient_noise) is a
45  * lattice-based noise based on gradients.
46  */
48  public:
49  /**
50  * @brief Constructor
51  *
52  * @param random A random engine
53  * @param step A step
54  *
55  * @sa gf::Step
56  */
57  GradientNoise(Random& random, Step<double> step);
58 
59  /**
60  * @brief Take a noise value
61  *
62  * @param x The x coordinate of the noise value
63  * @param y The y coordinate of the noise value
64  * @return The noise value
65  */
66  double operator()(double x, double y) const;
67 
68  private:
69  Step<double> m_step;
70  std::array<Vector2d, 256> m_gradients;
71  std::array<uint8_t, 256> m_perm;
72 
73  const Vector2d& at(uint8_t i, uint8_t j) const;
74  };
75 
76 
77  /**
78  * @ingroup core
79  * @brief Fractal noise
80  *
81  * Fractal noise is based of fractional Brownian motion (fBm). It consists
82  * in adding several octaves of a basic noise at different amplitudes.
83  *
84  */
86  public:
87  /**
88  * @brief Constructor
89  *
90  * @param noise The basic noise function
91  * @param scale The scale factor
92  * @param octaves The number of octaves
93  * @param lacunarity The factor applied to frequency
94  * @param persistence The factor applied to amplitude
95  * @param dimension The contrast between the layers
96  */
97  FractalNoise(std::function<double(double,double)> noise, double scale, std::size_t octaves = 8, double lacunarity = 2.0, double persistence = 0.5, double dimension = 1.0)
98  : m_noise(noise)
99  , m_scale(scale)
100  , m_octaves(octaves)
101  , m_lacunarity(lacunarity)
102  , m_persistence(persistence)
103  , m_dimension(dimension)
104  {
105 
106  }
107 
108  /**
109  * @brief Take a noise value
110  *
111  * @param x The x coordinate of the noise value
112  * @param y The y coordinate of the noise value
113  * @return The noise value
114  */
115  double operator()(double x, double y) const;
116 
117  private:
118  std::function<double(double,double)> m_noise;
119  double m_scale;
120  std::size_t m_octaves;
121  double m_lacunarity;
122  double m_persistence;
123  double m_dimension;
124  };
125 
126 
127 
128  /**
129  * @ingroup core
130  * @brief Perlin noise
131  *
132  * [Perlin noise](https://en.wikipedia.org/wiki/Perlin_noise) is the
133  * combination of a fractal noise and a gradient noise.
134  *
135  * @sa gf::GradientNoise, gf::FractalNoise
136  */
138  public:
139  /**
140  * @brief Constructor
141  *
142  * @param random A random engine
143  * @param scale The scale factor
144  * @param octaves The number of octaves
145  */
146  PerlinNoise(Random& random, double scale, std::size_t octaves = 8);
147 
148  /**
149  * @brief Take a noise value
150  *
151  * @param x The x coordinate of the noise value
152  * @param y The y coordinate of the noise value
153  * @return The noise value
154  */
155  double operator()(double x, double y) const;
156 
157  private:
158  GradientNoise m_gradient;
159  FractalNoise m_fractal;
160  };
161 
162  /**
163  * @ingroup core
164  * @brief Simplex noise
165  *
166  * [Simplex noise](https://en.wikipedia.org/wiki/Simplex_noise) is a lattice
167  * noise based on gradients put on a simplex.
168  *
169  * This implementation is limited to 2D noise and is *not* submitted to the
170  * patent that covers simplex noise.
171  *
172  * @sa gf::GradientNoise
173  */
175  public:
176  /**
177  * @brief Constructor
178  *
179  * @param random A random engine
180  */
181  SimplexNoise(Random& random);
182 
183  /**
184  * @brief Take a noise value
185  *
186  * @param x The x coordinate of the noise value
187  * @param y The y coordinate of the noise value
188  * @return The noise value
189  */
190  double operator()(double x, double y) const;
191 
192 
193  private:
194  std::array<uint8_t, 256> m_perm;
195 
196  const Vector2d& at(uint8_t i, uint8_t j) const;
197  };
198 
199  /**
200  * @ingroup core
201  * @brief OpenSimplex noise
202  *
203  * [OpenSimplex noise](https://en.wikipedia.org/wiki/OpenSimplex_noise) is a lattice
204  * noise very similar to simplex noise.
205  *
206  * @sa gf::SimplexNoise
207  */
209  public:
210  /**
211  * @brief Constructor
212  *
213  * @param random A random engine
214  */
215  OpenSimplexNoise(Random& random);
216 
217  /**
218  * @brief Take a noise value
219  *
220  * @param x The x coordinate of the noise value
221  * @param y The y coordinate of the noise value
222  * @return The noise value
223  */
224  double operator()(double x, double y) const;
225 
226 
227  private:
228  std::array<uint8_t, 256> m_perm;
229 
230  const Vector2d& at(uint8_t i, uint8_t j) const;
231  };
232 
233 
234 #ifndef DOXYGEN_SHOULD_SKIP_THIS
235 }
236 #endif
237 }
238 
239 #endif // GF_NOISE_H
A random engine.
Definition: Random.h:43
Gradient noise.
Definition: Noise.h:47
Perlin noise.
Definition: Noise.h:137
SimplexNoise(Random &random)
Constructor.
double operator()(double x, double y) const
Take a noise value.
double operator()(double x, double y) const
Take a noise value.
double operator()(double x, double y) const
Take a noise value.
GradientNoise(Random &random, Step< double > step)
Constructor.
double operator()(double x, double y) const
Take a noise value.
PerlinNoise(Random &random, double scale, std::size_t octaves=8)
Constructor.
Definition: Action.h:34
Fractal noise.
Definition: Noise.h:85
double operator()(double x, double y) const
Take a noise value.
FractalNoise(std::function< double(double, double)> noise, double scale, std::size_t octaves=8, double lacunarity=2.0, double persistence=0.5, double dimension=1.0)
Constructor.
Definition: Noise.h:97
OpenSimplexNoise(Random &random)
Constructor.
Vector< double, 2 > Vector2d
A double vector with 2 components.
Definition: Vector.h:759
#define GF_API
Definition: Portability.h:35
Simplex noise.
Definition: Noise.h:174
OpenSimplex noise.
Definition: Noise.h:208