1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
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 |
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 |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
10 | * |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
14 | * GNU General Public License for more details. |
15 | * |
15 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
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 |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
19 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
21 | */ |
22 | |
22 | |
23 | #ifndef NOISE_H_ |
23 | #ifndef NOISE_H_ |
24 | #define NOISE_H_ |
24 | #define NOISE_H_ |
25 | |
25 | |
|
|
26 | #if 0 // blitz++0.09 |
|
|
27 | #include <string.h> // workaround for tinyvec using memcpy without including string.h |
|
|
28 | #include <cstdlib> // workaround for tinyvec using labs without including cstdlib |
|
|
29 | #include <blitz/tinyvec.h> |
|
|
30 | #include <blitz/tinyvec-et.h> |
|
|
31 | typedef blitz::TinyVector<float,2> vec2d; |
|
|
32 | typedef blitz::TinyVector<float,3> vec3d; |
|
|
33 | #else |
|
|
34 | |
|
|
35 | // blitz++ 0.10 - not working |
|
|
36 | #include <blitz/array.h> |
|
|
37 | #include <blitz/tinyvec2.h> |
|
|
38 | #include <blitz/tinyvec2.cc> |
|
|
39 | |
|
|
40 | template<typename T, int length> |
|
|
41 | struct vecnd |
|
|
42 | : blitz::TinyVector<T, length> |
|
|
43 | { |
|
|
44 | static const int numElements = length; |
|
|
45 | using blitz::TinyVector<T, length>::TinyVector; |
|
|
46 | }; |
|
|
47 | |
|
|
48 | typedef vecnd<float,2> vec2d; |
|
|
49 | typedef vecnd<float,3> vec3d; |
|
|
50 | |
|
|
51 | #endif |
|
|
52 | |
26 | #include "global.h" |
53 | #include "global.h" |
27 | |
54 | |
|
|
55 | vec2d |
|
|
56 | inline floor (vec2d v) |
|
|
57 | { |
|
|
58 | return vec2d (fastfloor (v[0]), fastfloor (v[1])); |
|
|
59 | } |
|
|
60 | |
|
|
61 | vec3d |
|
|
62 | inline floor (vec3d v) |
|
|
63 | { |
|
|
64 | return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2])); |
|
|
65 | } |
|
|
66 | |
|
|
67 | vec2d |
|
|
68 | inline pow (vec2d v, vec2d p) |
|
|
69 | { |
|
|
70 | return vec2d (pow (v[0], p[0]), pow (v[1], p[1])); |
|
|
71 | } |
|
|
72 | |
|
|
73 | ///////////////////////////////////////////////////////////////////////////// |
|
|
74 | |
|
|
75 | // various s-shaped curves, smooth to, first, or second derivative |
|
|
76 | // used for smooth interpolation from 0..1 |
|
|
77 | |
|
|
78 | // linear |
|
|
79 | template<typename T> |
|
|
80 | inline T |
|
|
81 | sigmoid0 (T x) |
|
|
82 | { |
|
|
83 | return x; |
|
|
84 | } |
|
|
85 | |
|
|
86 | // 3x²-2x³ |
|
|
87 | template<typename T> |
|
|
88 | inline T |
|
|
89 | sigmoid1 (T x) |
|
|
90 | { |
|
|
91 | return (3 - 2 * x) * x * x; |
|
|
92 | } |
|
|
93 | |
|
|
94 | // 6x⁵ - 15x⁴ + 10x³ |
|
|
95 | template<typename T> |
|
|
96 | inline T |
|
|
97 | sigmoid2 (T x) |
|
|
98 | { |
|
|
99 | #ifdef MORE_PARALLELITY |
|
|
100 | float x2 = x * x; |
|
|
101 | float x4 = x2 * x2; |
|
|
102 | |
|
|
103 | return (6 * x4 + 10 * x2) * x - 15 * x4; |
|
|
104 | #endif |
|
|
105 | |
|
|
106 | // simple horner |
|
|
107 | return ((6 * x - 15) * x + 10) * x * x * x; |
|
|
108 | } |
|
|
109 | |
|
|
110 | // blend between a and b |
|
|
111 | // c is the control function - if lower than ca |
|
|
112 | // then return a, if higher than cb, return b |
|
|
113 | template<typename T, typename U> |
|
|
114 | inline T |
|
|
115 | blend (T a, T b, U c, U ca, U cb, U weight (U) = sigmoid1) |
|
|
116 | { |
|
|
117 | if (c <= ca) return a; |
|
|
118 | if (c >= cb) return b; |
|
|
119 | |
|
|
120 | U w = weight ((c - ca) / (cb - ca)); |
|
|
121 | return (U(1) - w) * a + w * b; |
|
|
122 | } |
|
|
123 | |
|
|
124 | // blend between a and b |
|
|
125 | // c is the control function - if lower than -c_w |
|
|
126 | // then return a, if higher than +c_w then b. |
|
|
127 | template<typename T, typename U> |
|
|
128 | inline T |
|
|
129 | blend0 (T a, T b, U c, U c_w, U weight (U) = sigmoid1) |
|
|
130 | { |
|
|
131 | return blend<T,U> (a, b, c, -c_w, c_w, weight); |
|
|
132 | } |
|
|
133 | |
|
|
134 | template<class vec_t, int a, int b> |
|
|
135 | struct rotate_nn |
|
|
136 | { |
|
|
137 | typename vec_t::T_numtype s, c; |
|
|
138 | |
|
|
139 | void set (typename vec_t::T_numtype angle) |
|
|
140 | { |
|
|
141 | s = sin (angle); |
|
|
142 | c = cos (angle); |
|
|
143 | } |
|
|
144 | |
|
|
145 | void operator ()(vec_t &P) const |
|
|
146 | { |
|
|
147 | vec_t o = P; |
|
|
148 | |
|
|
149 | P[a] = o[a] * c - o[b] * s; |
|
|
150 | P[b] = o[a] * s + o[b] * c; |
|
|
151 | } |
|
|
152 | }; |
|
|
153 | |
|
|
154 | template<class vec_t> |
|
|
155 | struct rotate_xy : rotate_nn<vec_t, 0, 1> |
|
|
156 | { |
|
|
157 | }; |
|
|
158 | |
|
|
159 | template<class vec_t> |
|
|
160 | struct rotate_xz : rotate_nn<vec_t, 0, 2> |
|
|
161 | { |
|
|
162 | }; |
|
|
163 | |
|
|
164 | template<class vec_t> |
|
|
165 | struct rotate_yz : rotate_nn<vec_t, 1, 2> |
|
|
166 | { |
|
|
167 | }; |
|
|
168 | |
|
|
169 | ///////////////////////////////////////////////////////////////////////////// |
|
|
170 | |
28 | template<int N, typename T = uint8_t> |
171 | template< int N, typename T> |
29 | struct permutation |
172 | struct permutation |
30 | { |
173 | { |
31 | T pmap [N]; |
174 | T pmap[N]; |
32 | |
175 | |
33 | template<class generator> |
176 | void seed (seedable_rand_gen &rng); |
34 | void seed (generator &rng) |
|
|
35 | { |
|
|
36 | for (int i = 0; i < N; ++i) |
|
|
37 | pmap [i] = i; |
|
|
38 | |
177 | |
39 | // fisher-yates to randomly perturb |
|
|
40 | for (int i = N; --i; ) |
|
|
41 | //::swap (pmap [i], pmap [rng (i + 1)]); |
|
|
42 | ::swap (pmap [i], pmap [::rmg_rndm (i + 1)]); |
|
|
43 | } |
|
|
44 | |
|
|
45 | T operator ()(T v) |
178 | T operator ()(T v) func_pure |
46 | { |
179 | { |
47 | return pmap [v & (N - 1)]; |
180 | return pmap[v & T(N - 1)]; |
48 | } |
181 | } |
49 | }; |
182 | }; |
50 | |
183 | |
|
|
184 | ///////////////////////////////////////////////////////////////////////////// |
|
|
185 | |
|
|
186 | template<class vec_t> |
|
|
187 | struct noise_gen_base |
|
|
188 | { |
|
|
189 | permutation<256, uint8_t> rvmap[vec_t::numElements + 1]; |
|
|
190 | |
|
|
191 | typedef typename vec_t::T_numtype value_t; |
|
|
192 | |
|
|
193 | void seed (seedable_rand_gen &rng); |
|
|
194 | void seed (seed_t seed); |
|
|
195 | |
|
|
196 | value_t operator ()(vec_t P, uint32_t seed = 0) func_pure; |
|
|
197 | }; |
|
|
198 | |
|
|
199 | template<class vec_t> |
51 | struct noise_gen |
200 | struct noise_gen; |
52 | { |
|
|
53 | permutation<256, uint8_t> rvmap[2]; |
|
|
54 | float rvec [256][2]; // random unit vectors |
|
|
55 | |
201 | |
56 | noise_gen (uint32_t seed); |
202 | // modelled after 2d/3d kensler noise without projection |
57 | float noise (float x, float y); |
203 | template<> |
58 | }; |
204 | struct noise_gen<vec2d> |
59 | |
205 | : noise_gen_base<vec2d> |
60 | struct rotate |
|
|
61 | { |
206 | { |
62 | float s, c; |
207 | static value_t abs_avg() { return 0.2231; } // avg(abs(noise)) |
63 | |
|
|
64 | void set (float angle) |
|
|
65 | { |
|
|
66 | s = sinf (angle); |
|
|
67 | c = cosf (angle); |
|
|
68 | } |
|
|
69 | |
|
|
70 | void operator ()(float &x, float &y) const |
|
|
71 | { |
|
|
72 | float xr = x * c - y * s; |
|
|
73 | float yr = x * s + y * c; |
|
|
74 | |
|
|
75 | x = xr; |
|
|
76 | y = yr; |
|
|
77 | } |
|
|
78 | }; |
208 | }; |
79 | |
209 | |
|
|
210 | template<> |
|
|
211 | struct noise_gen<vec3d> |
|
|
212 | : noise_gen_base<vec3d> |
|
|
213 | { |
|
|
214 | static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise)) |
|
|
215 | |
|
|
216 | using noise_gen_base<vec3d>::operator (); |
|
|
217 | |
|
|
218 | // noise projected on a surface with normal n |
|
|
219 | vec2d::T_numtype operator() (vec3d P, vec3d N, uint32_t seed = 0) func_pure; |
|
|
220 | }; |
|
|
221 | |
|
|
222 | typedef noise_gen<vec2d> noise2d; |
|
|
223 | typedef noise_gen<vec3d> noise3d; |
|
|
224 | |
|
|
225 | ///////////////////////////////////////////////////////////////////////////// |
|
|
226 | |
|
|
227 | template<class vec_t> |
80 | struct frac_gen |
228 | struct frac_gen |
81 | : noise_gen |
229 | : noise_gen<vec_t> |
82 | { |
230 | { |
83 | enum { MAX_OCTAVES = 64 }; |
231 | enum { MAX_OCTAVES = 32 }; |
84 | |
232 | |
|
|
233 | typedef typename vec_t::T_numtype value_t; |
|
|
234 | |
|
|
235 | int octaves; |
85 | float h, lac, ex [MAX_OCTAVES]; |
236 | value_t h, lac, ex[MAX_OCTAVES]; |
86 | float fbm_mul [MAX_OCTAVES]; |
237 | value_t fbm_mul[MAX_OCTAVES]; |
87 | rotate rot [MAX_OCTAVES]; |
238 | rotate_xy<vec_t> rot[MAX_OCTAVES]; |
88 | |
239 | |
89 | frac_gen (float hurst_expo = .5f, float lacunarity = 2.f); |
240 | frac_gen (int octaves = 3, value_t lacunarity = 2, value_t hurst_expo = .5, uint32_t seed = 0); |
90 | |
241 | |
91 | float fBm (float x, float y, int octaves); |
242 | value_t noise (vec_t P, uint32_t seed = 0) func_pure |
92 | float turbulence (float x, float y, int octaves); |
243 | { |
93 | float multifractal (float x, float y, int octaves, float offset = 1); |
244 | return this->operator() (P, seed); |
94 | float heterofractal (float x, float y, int octaves, float offset = 1); |
245 | } |
95 | float hybridfractal (float x, float y, int octaves, float offset = 1, float gain = 1); |
246 | |
96 | float terrain (float x, float y, int octaves); |
247 | value_t fBm (vec_t P) func_pure; |
97 | float terrain2 (float x, float y, int octaves); |
248 | value_t turbulence (vec_t P) func_pure; |
98 | float ridgedmultifractal (float x, float y, int octaves, float offset = 1, float gain = 8); |
249 | value_t multifractal (vec_t P, value_t offset = 1) func_pure; |
|
|
250 | value_t heterofractal (vec_t P, value_t offset = 1) func_pure; |
|
|
251 | value_t hybridfractal (vec_t P, value_t offset = 1, value_t gain = 1) func_pure; |
|
|
252 | value_t ridgedmultifractal (vec_t P, value_t offset = 1, value_t gain = 8) func_pure; |
|
|
253 | value_t billowfractal (vec_t P, value_t offset = 1, value_t gain = 2) func_pure; |
|
|
254 | value_t terrain (vec_t P) func_pure; |
|
|
255 | value_t terrain2 (vec_t P) func_pure; |
99 | }; |
256 | }; |
|
|
257 | |
|
|
258 | typedef frac_gen<vec2d> frac2d; |
|
|
259 | typedef frac_gen<vec3d> frac3d; |
|
|
260 | |
|
|
261 | ///////////////////////////////////////////////////////////////////////////// |
|
|
262 | |
|
|
263 | template<typename T, typename U> |
|
|
264 | inline T |
|
|
265 | border_blend (T a, T b, vec2d P, U N, U W) |
|
|
266 | { |
|
|
267 | U border = W; // within n places of the border |
|
|
268 | |
|
|
269 | min_it (border, P [0]); |
|
|
270 | min_it (border, N - P [0]); |
|
|
271 | min_it (border, P [1]); |
|
|
272 | min_it (border, N - P [1]); |
|
|
273 | |
|
|
274 | return blend (a, b, border, U(0), W); |
|
|
275 | } |
100 | |
276 | |
101 | #endif |
277 | #endif |
|
|
278 | |