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 |
root |
1.9 |
template<class vec_t, int a, int b> |
35 |
|
|
struct rotate_nn |
36 |
|
|
{ |
37 |
|
|
typename vec_t::T_numtype s, c; |
38 |
|
|
|
39 |
|
|
void set (typename vec_t::T_numtype angle) |
40 |
|
|
{ |
41 |
|
|
s = sin (angle); |
42 |
|
|
c = cos (angle); |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
void operator ()(vec_t &P) const |
46 |
|
|
{ |
47 |
|
|
vec_t o = P; |
48 |
|
|
|
49 |
|
|
P[a] = o[a] * c - o[b] * s; |
50 |
|
|
P[b] = o[a] * s + o[b] * c; |
51 |
|
|
} |
52 |
|
|
}; |
53 |
|
|
|
54 |
|
|
template<class vec_t> |
55 |
|
|
struct rotate_xy : rotate_nn<vec_t, 0, 1> |
56 |
|
|
{ |
57 |
|
|
}; |
58 |
|
|
|
59 |
|
|
template<class vec_t> |
60 |
|
|
struct rotate_xz : rotate_nn<vec_t, 0, 2> |
61 |
|
|
{ |
62 |
|
|
}; |
63 |
|
|
|
64 |
|
|
template<class vec_t> |
65 |
|
|
struct rotate_yz : rotate_nn<vec_t, 1, 2> |
66 |
|
|
{ |
67 |
|
|
}; |
68 |
|
|
|
69 |
root |
1.3 |
///////////////////////////////////////////////////////////////////////////// |
70 |
|
|
|
71 |
root |
1.1 |
template<int N, typename T = uint8_t> |
72 |
|
|
struct permutation |
73 |
|
|
{ |
74 |
root |
1.5 |
T pmap[N]; |
75 |
root |
1.1 |
|
76 |
root |
1.9 |
void seed (seedable_rand_gen &rng); |
77 |
root |
1.1 |
|
78 |
root |
1.9 |
T operator ()(T v) func_pure |
79 |
root |
1.1 |
{ |
80 |
root |
1.5 |
return pmap[v & (N - 1)]; |
81 |
root |
1.1 |
} |
82 |
|
|
}; |
83 |
|
|
|
84 |
root |
1.9 |
///////////////////////////////////////////////////////////////////////////// |
85 |
|
|
|
86 |
|
|
template<class vec_t> |
87 |
|
|
struct noise_gen_base |
88 |
|
|
{ |
89 |
|
|
permutation<256, uint8_t> rvmap[vec_t::numElements]; |
90 |
|
|
|
91 |
|
|
void seed (seedable_rand_gen &rng); |
92 |
|
|
void seed (seed_t seed); |
93 |
|
|
|
94 |
|
|
typename vec_t::T_numtype operator ()(vec_t P) func_pure; |
95 |
|
|
}; |
96 |
|
|
|
97 |
root |
1.5 |
template<class vec_t> |
98 |
|
|
struct noise_gen; |
99 |
|
|
|
100 |
root |
1.3 |
// modelled after 2d/3d kensler noise without projection |
101 |
root |
1.5 |
template<> |
102 |
|
|
struct noise_gen<vec2d> |
103 |
root |
1.9 |
: noise_gen_base<vec2d> |
104 |
root |
1.1 |
{ |
105 |
root |
1.8 |
static vec2d::T_numtype abs_avg() { return 0.2231; } // avg(abs(noise)) |
106 |
root |
1.1 |
}; |
107 |
|
|
|
108 |
root |
1.5 |
template<> |
109 |
|
|
struct noise_gen<vec3d> |
110 |
root |
1.9 |
: noise_gen_base<vec3d> |
111 |
root |
1.3 |
{ |
112 |
root |
1.8 |
static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise)) |
113 |
|
|
|
114 |
root |
1.9 |
using noise_gen_base<vec3d>::operator (); |
115 |
root |
1.3 |
|
116 |
|
|
// noise projected on a surface with normal n |
117 |
root |
1.9 |
vec2d::T_numtype operator() (vec3d P, vec3d N) func_pure; |
118 |
root |
1.5 |
}; |
119 |
|
|
|
120 |
root |
1.9 |
template<class vec_T> |
121 |
|
|
struct perturb_gen |
122 |
root |
1.5 |
{ |
123 |
root |
1.9 |
//TODO |
124 |
root |
1.5 |
}; |
125 |
|
|
|
126 |
|
|
template<class vec_t> |
127 |
root |
1.1 |
struct frac_gen |
128 |
root |
1.5 |
: noise_gen<vec_t> |
129 |
root |
1.1 |
{ |
130 |
root |
1.8 |
enum { MAX_OCTAVES = 32 }; |
131 |
root |
1.1 |
|
132 |
root |
1.5 |
typedef typename vec_t::T_numtype value_t; |
133 |
|
|
|
134 |
|
|
value_t h, lac, ex[MAX_OCTAVES]; |
135 |
|
|
value_t fbm_mul[MAX_OCTAVES]; |
136 |
|
|
rotate_xy<vec_t> rot[MAX_OCTAVES]; |
137 |
|
|
|
138 |
root |
1.9 |
frac_gen (value_t hurst_expo = .5, value_t lacunarity = 2, uint32_t seed = 0); |
139 |
|
|
|
140 |
|
|
value_t noise (vec_t P) func_pure |
141 |
root |
1.5 |
{ |
142 |
|
|
return operator() (P); |
143 |
|
|
} |
144 |
|
|
|
145 |
root |
1.9 |
value_t fBm (vec_t P, int octaves) func_pure; |
146 |
|
|
value_t turbulence (vec_t P, int octaves) func_pure; |
147 |
|
|
value_t multifractal (vec_t P, int octaves, value_t offset = 1) func_pure; |
148 |
|
|
value_t heterofractal (vec_t P, int octaves, value_t offset = 1) func_pure; |
149 |
|
|
value_t hybridfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 1) func_pure; |
150 |
|
|
value_t ridgedmultifractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 8) func_pure; |
151 |
|
|
value_t billowfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 2) func_pure; |
152 |
|
|
value_t terrain (vec_t P, int octaves) func_pure; |
153 |
|
|
value_t terrain2 (vec_t P, int octaves) func_pure; |
154 |
root |
1.1 |
}; |
155 |
|
|
|
156 |
|
|
#endif |
157 |
root |
1.5 |
|