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