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