1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2010,2011,2012 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 <string.h> // workaround for tinyvec using memcpy without including string.h |
27 |
#include <blitz/tinyvec.h> |
28 |
#include <blitz/tinyvec-et.h> |
29 |
|
30 |
#include "global.h" |
31 |
|
32 |
typedef blitz::TinyVector<float,2> vec2d; |
33 |
typedef blitz::TinyVector<float,3> vec3d; |
34 |
|
35 |
vec2d |
36 |
inline floor (vec2d v) |
37 |
{ |
38 |
return vec2d (fastfloor (v[0]), fastfloor (v[1])); |
39 |
} |
40 |
|
41 |
vec3d |
42 |
inline floor (vec3d v) |
43 |
{ |
44 |
return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2])); |
45 |
} |
46 |
|
47 |
vec2d |
48 |
inline pow (vec2d v, vec2d p) |
49 |
{ |
50 |
return vec2d (pow (v[0], p[0]), pow (v[1], p[1])); |
51 |
} |
52 |
|
53 |
///////////////////////////////////////////////////////////////////////////// |
54 |
|
55 |
// various s-shaped curves, smooth to, first, or second derivative |
56 |
// used for smooth interpolation from 0..1 |
57 |
|
58 |
// linear |
59 |
template<typename T> |
60 |
inline T |
61 |
sigmoid0 (T x) |
62 |
{ |
63 |
return x; |
64 |
} |
65 |
|
66 |
// 3x²-2x³ |
67 |
template<typename T> |
68 |
inline T |
69 |
sigmoid1 (T x) |
70 |
{ |
71 |
return (3 - 2 * x) * x * x; |
72 |
} |
73 |
|
74 |
// 6x⁵ - 15x⁴ + 10x³ |
75 |
template<typename T> |
76 |
inline T |
77 |
sigmoid2 (T x) |
78 |
{ |
79 |
#ifdef MORE_PARALLELITY |
80 |
float x2 = x * x; |
81 |
float x4 = x2 * x2; |
82 |
|
83 |
return (6 * x4 + 10 * x2) * x - 15 * x4; |
84 |
#endif |
85 |
|
86 |
// simple horner |
87 |
return ((6 * x - 15) * x + 10) * x * x * x; |
88 |
} |
89 |
|
90 |
// blend between a and b |
91 |
// c is the control function - if lower than ca |
92 |
// then return a, if higher than cb, return b |
93 |
template<typename T, typename U> |
94 |
inline T |
95 |
blend (T a, T b, U c, U ca, U cb, U weight (U) = sigmoid1) |
96 |
{ |
97 |
if (c <= ca) return a; |
98 |
if (c >= cb) return b; |
99 |
|
100 |
U w = weight ((c - ca) / (cb - ca)); |
101 |
return (U(1) - w) * a + w * b; |
102 |
} |
103 |
|
104 |
// blend between a and b |
105 |
// c is the control function - if lower than -c_w |
106 |
// then return a, if higher than +c_w then b. |
107 |
template<typename T, typename U> |
108 |
inline T |
109 |
blend0 (T a, T b, U c, U c_w, U weight (U) = sigmoid1) |
110 |
{ |
111 |
return blend<T,U> (a, b, c, -c_w, c_w, weight); |
112 |
} |
113 |
|
114 |
template<class vec_t, int a, int b> |
115 |
struct rotate_nn |
116 |
{ |
117 |
typename vec_t::T_numtype s, c; |
118 |
|
119 |
void set (typename vec_t::T_numtype angle) |
120 |
{ |
121 |
s = sin (angle); |
122 |
c = cos (angle); |
123 |
} |
124 |
|
125 |
void operator ()(vec_t &P) const |
126 |
{ |
127 |
vec_t o = P; |
128 |
|
129 |
P[a] = o[a] * c - o[b] * s; |
130 |
P[b] = o[a] * s + o[b] * c; |
131 |
} |
132 |
}; |
133 |
|
134 |
template<class vec_t> |
135 |
struct rotate_xy : rotate_nn<vec_t, 0, 1> |
136 |
{ |
137 |
}; |
138 |
|
139 |
template<class vec_t> |
140 |
struct rotate_xz : rotate_nn<vec_t, 0, 2> |
141 |
{ |
142 |
}; |
143 |
|
144 |
template<class vec_t> |
145 |
struct rotate_yz : rotate_nn<vec_t, 1, 2> |
146 |
{ |
147 |
}; |
148 |
|
149 |
///////////////////////////////////////////////////////////////////////////// |
150 |
|
151 |
template< int N, typename T> |
152 |
struct permutation |
153 |
{ |
154 |
T pmap[N]; |
155 |
|
156 |
void seed (seedable_rand_gen &rng); |
157 |
|
158 |
T operator ()(T v) func_pure |
159 |
{ |
160 |
return pmap[v & T(N - 1)]; |
161 |
} |
162 |
}; |
163 |
|
164 |
///////////////////////////////////////////////////////////////////////////// |
165 |
|
166 |
template<class vec_t> |
167 |
struct noise_gen_base |
168 |
{ |
169 |
permutation<256, uint8_t> rvmap[vec_t::numElements + 1]; |
170 |
|
171 |
typedef typename vec_t::T_numtype value_t; |
172 |
|
173 |
void seed (seedable_rand_gen &rng); |
174 |
void seed (seed_t seed); |
175 |
|
176 |
value_t operator ()(vec_t P, uint32_t seed = 0) func_pure; |
177 |
}; |
178 |
|
179 |
template<class vec_t> |
180 |
struct noise_gen; |
181 |
|
182 |
// modelled after 2d/3d kensler noise without projection |
183 |
template<> |
184 |
struct noise_gen<vec2d> |
185 |
: noise_gen_base<vec2d> |
186 |
{ |
187 |
static value_t abs_avg() { return 0.2231; } // avg(abs(noise)) |
188 |
}; |
189 |
|
190 |
template<> |
191 |
struct noise_gen<vec3d> |
192 |
: noise_gen_base<vec3d> |
193 |
{ |
194 |
static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise)) |
195 |
|
196 |
using noise_gen_base<vec3d>::operator (); |
197 |
|
198 |
// noise projected on a surface with normal n |
199 |
vec2d::T_numtype operator() (vec3d P, vec3d N, uint32_t seed = 0) func_pure; |
200 |
}; |
201 |
|
202 |
typedef noise_gen<vec2d> noise2d; |
203 |
typedef noise_gen<vec3d> noise3d; |
204 |
|
205 |
///////////////////////////////////////////////////////////////////////////// |
206 |
|
207 |
template<class vec_t> |
208 |
struct frac_gen |
209 |
: noise_gen<vec_t> |
210 |
{ |
211 |
enum { MAX_OCTAVES = 32 }; |
212 |
|
213 |
typedef typename vec_t::T_numtype value_t; |
214 |
|
215 |
int octaves; |
216 |
value_t h, lac, ex[MAX_OCTAVES]; |
217 |
value_t fbm_mul[MAX_OCTAVES]; |
218 |
rotate_xy<vec_t> rot[MAX_OCTAVES]; |
219 |
|
220 |
frac_gen (int octaves = 3, value_t lacunarity = 2, value_t hurst_expo = .5, uint32_t seed = 0); |
221 |
|
222 |
value_t noise (vec_t P, uint32_t seed = 0) func_pure |
223 |
{ |
224 |
return this->operator() (P, seed); |
225 |
} |
226 |
|
227 |
value_t fBm (vec_t P) func_pure; |
228 |
value_t turbulence (vec_t P) func_pure; |
229 |
value_t multifractal (vec_t P, value_t offset = 1) func_pure; |
230 |
value_t heterofractal (vec_t P, value_t offset = 1) func_pure; |
231 |
value_t hybridfractal (vec_t P, value_t offset = 1, value_t gain = 1) func_pure; |
232 |
value_t ridgedmultifractal (vec_t P, value_t offset = 1, value_t gain = 8) func_pure; |
233 |
value_t billowfractal (vec_t P, value_t offset = 1, value_t gain = 2) func_pure; |
234 |
value_t terrain (vec_t P) func_pure; |
235 |
value_t terrain2 (vec_t P) func_pure; |
236 |
}; |
237 |
|
238 |
typedef frac_gen<vec2d> frac2d; |
239 |
typedef frac_gen<vec3d> frac3d; |
240 |
|
241 |
///////////////////////////////////////////////////////////////////////////// |
242 |
|
243 |
template<typename T, typename U> |
244 |
inline T |
245 |
border_blend (T a, T b, vec2d P, U N, U W) |
246 |
{ |
247 |
U border = W; // within n places of the border |
248 |
|
249 |
min_it (border, P [0]); |
250 |
min_it (border, N - P [0]); |
251 |
min_it (border, P [1]); |
252 |
min_it (border, N - P [1]); |
253 |
|
254 |
return blend (a, b, border, U(0), W); |
255 |
} |
256 |
|
257 |
#endif |
258 |
|