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.8 by root, Tue Apr 26 03:18:07 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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / 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
26#include <blitz/tinyvec.h> 30#include <blitz/tinyvec.h>
27#include <blitz/tinyvec-et.h> 31#include <blitz/tinyvec-et.h>
28
29#include "global.h"
30
31typedef blitz::TinyVector<float,2> vec2d; 32typedef blitz::TinyVector<float,2> vec2d;
32typedef blitz::TinyVector<float,3> vec3d; 33typedef blitz::TinyVector<float,3> vec3d;
34#else
33 35
34///////////////////////////////////////////////////////////////////////////// 36// blitz++ 0.10 - not working
37#include <blitz/array.h>
38#include <blitz/tinyvec2.h>
39#include <blitz/tinyvec2.cc>
35 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
54#include "global.h"
55
56vec2d
57inline floor (vec2d v)
58{
59 return vec2d (fastfloor (v[0]), fastfloor (v[1]));
60}
61
62vec3d
63inline floor (vec3d v)
64{
65 return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2]));
66}
67
68vec2d
69inline 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
80template<typename T>
81inline T
82sigmoid0 (T x)
83{
84 return x;
85}
86
87// 3x²-2x³
88template<typename T>
89inline T
90sigmoid1 (T x)
91{
92 return (3 - 2 * x) * x * x;
93}
94
95// 6x⁵ - 15x⁴ + 10x³
96template<typename T>
97inline T
98sigmoid2 (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
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
36template<int N, typename T = uint8_t> 172template< int N, typename T>
37struct permutation 173struct permutation
38{ 174{
39 T pmap[N]; 175 T pmap[N];
40 176
41 template<class random_generator>
42 void seed (random_generator &rng); 177 void seed (seedable_rand_gen &rng);
43 178
44 T operator ()(T v) 179 ecb_pure T operator ()(T v)
45 { 180 {
46 return pmap[v & (N - 1)]; 181 return pmap[v & T(N - 1)];
47 } 182 }
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);
48}; 198};
49 199
50template<class vec_t> 200template<class vec_t>
51struct noise_gen; 201struct noise_gen;
52 202
53// modelled after 2d/3d kensler noise without projection 203// modelled after 2d/3d kensler noise without projection
54template<> 204template<>
55struct noise_gen<vec2d> 205struct noise_gen<vec2d>
206: noise_gen_base<vec2d>
56{ 207{
57 static vec2d::T_numtype abs_avg() { return 0.2231; } // avg(abs(noise)) 208 static value_t abs_avg() { return 0.2231; } // avg(abs(noise))
58
59 permutation<256, uint8_t> rvmap[2];
60
61 noise_gen<vec2d> (uint32_t seed);
62 vec2d::T_numtype operator() (vec2d P);
63}; 209};
64 210
65template<> 211template<>
66struct noise_gen<vec3d> 212struct noise_gen<vec3d>
213: noise_gen_base<vec3d>
67{ 214{
68 static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise)) 215 static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise))
69 216
70 permutation<256, uint8_t> rvmap[3]; 217 using noise_gen_base<vec3d>::operator ();
71
72 noise_gen<vec3d> (uint32_t seed);
73 vec2d::T_numtype operator() (vec3d P);
74 218
75 // noise projected on a surface with normal n 219 // noise projected on a surface with normal n
76 vec2d::T_numtype operator() (vec3d P, vec3d N); 220 ecb_pure vec2d::T_numtype operator() (vec3d P, vec3d N, uint32_t seed = 0);
77}; 221};
78 222
79template<class vec_t, int a, int b> 223typedef noise_gen<vec2d> noise2d;
80struct rotate_nn 224typedef noise_gen<vec3d> noise3d;
81{
82 typename vec_t::T_numtype s, c;
83 225
84 void set (typename vec_t::T_numtype angle) 226/////////////////////////////////////////////////////////////////////////////
85 {
86 s = sin (angle);
87 c = cos (angle);
88 }
89
90 void operator ()(vec_t &P) const
91 {
92 vec_t o = P;
93
94 P[a] = o[a] * c - o[b] * s;
95 P[b] = o[a] * s + o[b] * c;
96 }
97};
98
99template<class vec_t>
100struct rotate_xy : rotate_nn<vec_t, 0, 1>
101{
102};
103
104template<class vec_t>
105struct rotate_xz : rotate_nn<vec_t, 0, 2>
106{
107};
108
109template<class vec_t>
110struct rotate_yz : rotate_nn<vec_t, 1, 2>
111{
112};
113 227
114template<class vec_t> 228template<class vec_t>
115struct frac_gen 229struct frac_gen
116: noise_gen<vec_t> 230: noise_gen<vec_t>
117{ 231{
118 enum { MAX_OCTAVES = 32 }; 232 enum { MAX_OCTAVES = 32 };
119 233
120 typedef typename vec_t::T_numtype value_t; 234 typedef typename vec_t::T_numtype value_t;
121 235
236 int octaves;
122 value_t h, lac, ex[MAX_OCTAVES]; 237 value_t h, lac, ex[MAX_OCTAVES];
123 value_t fbm_mul[MAX_OCTAVES]; 238 value_t fbm_mul[MAX_OCTAVES];
124 rotate_xy<vec_t> rot[MAX_OCTAVES]; 239 rotate_xy<vec_t> rot[MAX_OCTAVES];
125 240
126 value_t noise (vec_t P) 241 frac_gen (int octaves = 3, value_t lacunarity = 2, value_t hurst_expo = .5, uint32_t seed = 0);
242
243 ecb_pure value_t noise (vec_t P, uint32_t seed = 0)
127 { 244 {
128 return operator() (P); 245 return this->operator() (P, seed);
129 } 246 }
130 247
131 frac_gen (value_t hurst_expo = .5, value_t lacunarity = 2); 248 ecb_pure value_t fBm (vec_t P);
132 249 ecb_pure value_t turbulence (vec_t P);
133 value_t fBm (vec_t P, int octaves);
134 value_t turbulence (vec_t P, int octaves);
135 value_t multifractal (vec_t P, int octaves, value_t offset = 1); 250 ecb_pure value_t multifractal (vec_t P, value_t offset = 1);
136 value_t heterofractal (vec_t P, int octaves, value_t offset = 1); 251 ecb_pure value_t heterofractal (vec_t P, value_t offset = 1);
137 value_t hybridfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 1); 252 ecb_pure value_t hybridfractal (vec_t P, value_t offset = 1, value_t gain = 1);
138 value_t ridgedmultifractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 8); 253 ecb_pure value_t ridgedmultifractal (vec_t P, value_t offset = 1, value_t gain = 8);
139 value_t billowfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 2); 254 ecb_pure value_t billowfractal (vec_t P, value_t offset = 1, value_t gain = 2);
140 value_t terrain (vec_t P, int octaves); 255 ecb_pure value_t terrain (vec_t P);
141 value_t terrain2 (vec_t P, int octaves); 256 ecb_pure value_t terrain2 (vec_t P);
142}; 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}
143 277
144#endif 278#endif
145 279

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines