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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines