1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* |
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 |
#include <blitz/tinyvec.h> |
27 |
#include <blitz/tinyvec-et.h> |
28 |
|
29 |
#include "global.h" |
30 |
|
31 |
typedef blitz::TinyVector<float,2> vec2d; |
32 |
typedef blitz::TinyVector<float,3> vec3d; |
33 |
|
34 |
///////////////////////////////////////////////////////////////////////////// |
35 |
|
36 |
template<int N, typename T = uint8_t> |
37 |
struct permutation |
38 |
{ |
39 |
T pmap[N]; |
40 |
|
41 |
template<class generator> |
42 |
void seed (generator &rng) |
43 |
{ |
44 |
for (int i = 0; i < N; ++i) |
45 |
pmap[i] = i; |
46 |
|
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) |
53 |
{ |
54 |
return pmap[v & (N - 1)]; |
55 |
} |
56 |
}; |
57 |
|
58 |
template<class vec_t> |
59 |
struct noise_gen; |
60 |
|
61 |
// modelled after 2d/3d kensler noise without projection |
62 |
template<> |
63 |
struct noise_gen<vec2d> |
64 |
{ |
65 |
permutation<256, uint8_t> rvmap[2]; |
66 |
vec2d rvec[256]; // random unit vectors |
67 |
|
68 |
noise_gen<vec2d> (uint32_t seed); |
69 |
vec2d::T_numtype operator() (vec2d P); |
70 |
}; |
71 |
|
72 |
template<> |
73 |
struct noise_gen<vec3d> |
74 |
{ |
75 |
permutation<256, uint8_t> rvmap[3]; |
76 |
|
77 |
noise_gen<vec3d> (uint32_t seed); |
78 |
vec2d::T_numtype operator() (vec3d P); |
79 |
|
80 |
// noise projected on a surface with normal n |
81 |
vec2d::T_numtype operator() (vec3d P, vec3d N); |
82 |
}; |
83 |
|
84 |
template<class vec_t, int a, int b> |
85 |
struct rotate_nn |
86 |
{ |
87 |
typename vec_t::T_numtype s, c; |
88 |
|
89 |
void set (typename vec_t::T_numtype angle) |
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 |
|
104 |
template<class vec_t> |
105 |
struct rotate_xy : rotate_nn<vec_t, 0, 1> |
106 |
{ |
107 |
}; |
108 |
|
109 |
template<class vec_t> |
110 |
struct rotate_xz : rotate_nn<vec_t, 0, 2> |
111 |
{ |
112 |
}; |
113 |
|
114 |
template<class vec_t> |
115 |
struct rotate_yz : rotate_nn<vec_t, 1, 2> |
116 |
{ |
117 |
}; |
118 |
|
119 |
template<class vec_t> |
120 |
struct frac_gen |
121 |
: noise_gen<vec_t> |
122 |
{ |
123 |
enum { MAX_OCTAVES = 64 }; |
124 |
|
125 |
typedef typename vec_t::T_numtype value_t; |
126 |
|
127 |
value_t h, lac, ex[MAX_OCTAVES]; |
128 |
value_t fbm_mul[MAX_OCTAVES]; |
129 |
rotate_xy<vec_t> rot[MAX_OCTAVES]; |
130 |
|
131 |
value_t noise (vec_t P) |
132 |
{ |
133 |
return operator() (P); |
134 |
} |
135 |
|
136 |
frac_gen (value_t hurst_expo = .5f, value_t lacunarity = 2.f); |
137 |
|
138 |
value_t fBm (vec_t P, int octaves); |
139 |
value_t turbulence (vec_t P, int octaves); |
140 |
value_t multifractal (vec_t P, int octaves, value_t offset = 1); |
141 |
value_t heterofractal (vec_t P, int octaves, value_t offset = 1); |
142 |
value_t hybridfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 1); |
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); |
146 |
}; |
147 |
|
148 |
#endif |
149 |
|