ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/noise.h
Revision: 1.18
Committed: Sun Jun 9 21:19:12 2013 UTC (10 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +7 -0 lines
Log Message:
*** empty log message ***

File Contents

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