ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/noise.h
(Generate patch)

Comparing deliantra/server/include/noise.h (file contents):
Revision 1.3 by root, Fri Apr 22 02:03:11 2011 UTC vs.
Revision 1.22 by root, Sat Nov 17 23:40:01 2018 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines