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.1 by root, Sun Jul 11 04:01:49 2010 UTC vs.
Revision 1.14 by root, Tue Jan 3 11:25:32 2012 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.
21 */ 21 */
22 22
23#ifndef NOISE_H_ 23#ifndef NOISE_H_
24#define NOISE_H_ 24#define NOISE_H_
25 25
26#include <blitz/tinyvec.h>
27#include <blitz/tinyvec-et.h>
28
26#include "global.h" 29#include "global.h"
27 30
31typedef blitz::TinyVector<float,2> vec2d;
32typedef blitz::TinyVector<float,3> vec3d;
33
34vec2d
35inline floor (vec2d v)
36{
37 return vec2d (fastfloor (v[0]), fastfloor (v[1]));
38}
39
40vec3d
41inline floor (vec3d v)
42{
43 return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2]));
44}
45
46vec2d
47inline 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
58template<typename T>
59inline T
60sigmoid0 (T x)
61{
62 return x;
63}
64
65// 3x²-2x³
66template<typename T>
67inline T
68sigmoid1 (T x)
69{
70 return (3 - 2 * x) * x * x;
71}
72
73// 6x⁵ - 15x⁴ + 10x³
74template<typename T>
75inline T
76sigmoid2 (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
92template<typename T, typename U>
93inline T
94blend (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.
106template<typename T, typename U>
107inline T
108blend0 (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
113template<class vec_t, int a, int b>
114struct 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
133template<class vec_t>
134struct rotate_xy : rotate_nn<vec_t, 0, 1>
135{
136};
137
138template<class vec_t>
139struct rotate_xz : rotate_nn<vec_t, 0, 2>
140{
141};
142
143template<class vec_t>
144struct rotate_yz : rotate_nn<vec_t, 1, 2>
145{
146};
147
148/////////////////////////////////////////////////////////////////////////////
149
28template<int N, typename T = uint8_t> 150template< int N, typename T>
29struct permutation 151struct permutation
30{ 152{
31 T pmap [N]; 153 T pmap[N];
32 154
33 template<class generator> 155 void seed (seedable_rand_gen &rng);
34 void seed (generator &rng)
35 {
36 for (int i = 0; i < N; ++i)
37 pmap [i] = i;
38 156
39 // fisher-yates to randomly perturb
40 for (int i = N; --i; )
41 //::swap (pmap [i], pmap [rng (i + 1)]);
42 ::swap (pmap [i], pmap [::rmg_rndm (i + 1)]);
43 }
44
45 T operator ()(T v) 157 T operator ()(T v) func_pure
46 { 158 {
47 return pmap [v & (N - 1)]; 159 return pmap[v & T(N - 1)];
48 } 160 }
49}; 161};
50 162
163/////////////////////////////////////////////////////////////////////////////
164
165template<class vec_t>
166struct noise_gen_base
167{
168 permutation<256, uint8_t> rvmap[vec_t::numElements + 1];
169
170 typedef typename vec_t::T_numtype value_t;
171
172 void seed (seedable_rand_gen &rng);
173 void seed (seed_t seed);
174
175 value_t operator ()(vec_t P, uint32_t seed = 0) func_pure;
176};
177
178template<class vec_t>
51struct noise_gen 179struct noise_gen;
52{
53 permutation<256, uint8_t> rvmap[2];
54 float rvec [256][2]; // random unit vectors
55 180
56 noise_gen (uint32_t seed); 181// modelled after 2d/3d kensler noise without projection
57 float noise (float x, float y); 182template<>
58}; 183struct noise_gen<vec2d>
59 184: noise_gen_base<vec2d>
60struct rotate
61{ 185{
62 float s, c; 186 static value_t abs_avg() { return 0.2231; } // avg(abs(noise))
63
64 void set (float angle)
65 {
66 s = sinf (angle);
67 c = cosf (angle);
68 }
69
70 void operator ()(float &x, float &y) const
71 {
72 float xr = x * c - y * s;
73 float yr = x * s + y * c;
74
75 x = xr;
76 y = yr;
77 }
78}; 187};
79 188
189template<>
190struct noise_gen<vec3d>
191: noise_gen_base<vec3d>
192{
193 static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise))
194
195 using noise_gen_base<vec3d>::operator ();
196
197 // noise projected on a surface with normal n
198 vec2d::T_numtype operator() (vec3d P, vec3d N, uint32_t seed = 0) func_pure;
199};
200
201typedef noise_gen<vec2d> noise2d;
202typedef noise_gen<vec3d> noise3d;
203
204/////////////////////////////////////////////////////////////////////////////
205
206template<class vec_t>
80struct frac_gen 207struct frac_gen
81: noise_gen 208: noise_gen<vec_t>
82{ 209{
83 enum { MAX_OCTAVES = 64 }; 210 enum { MAX_OCTAVES = 32 };
84 211
212 typedef typename vec_t::T_numtype value_t;
213
214 int octaves;
85 float h, lac, ex [MAX_OCTAVES]; 215 value_t h, lac, ex[MAX_OCTAVES];
86 float fbm_mul [MAX_OCTAVES]; 216 value_t fbm_mul[MAX_OCTAVES];
87 rotate rot [MAX_OCTAVES]; 217 rotate_xy<vec_t> rot[MAX_OCTAVES];
88 218
89 frac_gen (float hurst_expo = .5f, float lacunarity = 2.f); 219 frac_gen (int octaves = 3, value_t lacunarity = 2, value_t hurst_expo = .5, uint32_t seed = 0);
90 220
91 float fBm (float x, float y, int octaves); 221 value_t noise (vec_t P, uint32_t seed = 0) func_pure
92 float turbulence (float x, float y, int octaves); 222 {
93 float multifractal (float x, float y, int octaves, float offset = 1); 223 return operator() (P, seed);
94 float heterofractal (float x, float y, int octaves, float offset = 1); 224 }
95 float hybridfractal (float x, float y, int octaves, float offset = 1, float gain = 1); 225
96 float terrain (float x, float y, int octaves); 226 value_t fBm (vec_t P) func_pure;
97 float terrain2 (float x, float y, int octaves); 227 value_t turbulence (vec_t P) func_pure;
98 float ridgedmultifractal (float x, float y, int octaves, float offset = 1, float gain = 8); 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;
99}; 235};
236
237typedef frac_gen<vec2d> frac2d;
238typedef frac_gen<vec3d> frac3d;
239
240/////////////////////////////////////////////////////////////////////////////
241
242template<typename T, typename U>
243inline T
244border_blend (T a, T b, vec2d P, U N, U W)
245{
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 return blend (a, b, border, U(0), W);
254}
100 255
101#endif 256#endif
257

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines