ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/noise.h
Revision: 1.14
Committed: Tue Jan 3 11:25:32 2012 UTC (12 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.13: +1 -1 lines
Log Message:
update copyrights to 2012

File Contents

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