1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* |
6 |
* Deliantra is free software: you can redistribute it and/or modify it under |
7 |
* the terms of the Affero GNU General Public License as published by the |
8 |
* Free Software Foundation, either version 3 of the License, or (at your |
9 |
* option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the Affero GNU General Public License |
17 |
* and the GNU General Public License along with this program. If not, see |
18 |
* <http://www.gnu.org/licenses/>. |
19 |
* |
20 |
* The authors can be reached via e-mail to <support@deliantra.net> |
21 |
*/ |
22 |
|
23 |
#ifndef NOISE_H_ |
24 |
#define NOISE_H_ |
25 |
|
26 |
#include "global.h" |
27 |
|
28 |
template<int N, typename T = uint8_t> |
29 |
struct permutation |
30 |
{ |
31 |
T pmap [N]; |
32 |
|
33 |
template<class generator> |
34 |
void seed (generator &rng) |
35 |
{ |
36 |
for (int i = 0; i < N; ++i) |
37 |
pmap [i] = i; |
38 |
|
39 |
// fisher-yates to randomly perturb |
40 |
for (int i = N; --i; ) |
41 |
::swap (pmap [i], pmap [::rmg_rndm (i + 1)]); |
42 |
} |
43 |
|
44 |
T operator ()(T v) |
45 |
{ |
46 |
return pmap [v & (N - 1)]; |
47 |
} |
48 |
}; |
49 |
|
50 |
struct noise_gen |
51 |
{ |
52 |
permutation<256, uint8_t> rvmap[2]; |
53 |
float rvec [256][2]; // random unit vectors |
54 |
|
55 |
noise_gen (uint32_t seed); |
56 |
float noise (float x, float y); |
57 |
}; |
58 |
|
59 |
struct rotate |
60 |
{ |
61 |
float s, c; |
62 |
|
63 |
void set (float angle) |
64 |
{ |
65 |
s = sinf (angle); |
66 |
c = cosf (angle); |
67 |
} |
68 |
|
69 |
void operator ()(float &x, float &y) const |
70 |
{ |
71 |
float xr = x * c - y * s; |
72 |
float yr = x * s + y * c; |
73 |
|
74 |
x = xr; |
75 |
y = yr; |
76 |
} |
77 |
}; |
78 |
|
79 |
struct frac_gen |
80 |
: noise_gen |
81 |
{ |
82 |
enum { MAX_OCTAVES = 64 }; |
83 |
|
84 |
float h, lac, ex [MAX_OCTAVES]; |
85 |
float fbm_mul [MAX_OCTAVES]; |
86 |
rotate rot [MAX_OCTAVES]; |
87 |
|
88 |
frac_gen (float hurst_expo = .5f, float lacunarity = 2.f); |
89 |
|
90 |
float fBm (float x, float y, int octaves); |
91 |
float turbulence (float x, float y, int octaves); |
92 |
float multifractal (float x, float y, int octaves, float offset = 1); |
93 |
float heterofractal (float x, float y, int octaves, float offset = 1); |
94 |
float hybridfractal (float x, float y, int octaves, float offset = 1, float gain = 1); |
95 |
float terrain (float x, float y, int octaves); |
96 |
float terrain2 (float x, float y, int octaves); |
97 |
float ridgedmultifractal (float x, float y, int octaves, float offset = 1, float gain = 8); |
98 |
}; |
99 |
|
100 |
#endif |