ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/noise.h
Revision: 1.3
Committed: Fri Apr 22 02:03:11 2011 UTC (13 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.2: +140 -3 lines
Log Message:
move gridmap to arch, refactor cf.pm a bit

File Contents

# User Rev Content
1 root 1.1 /*
2     * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3     *
4     * Copyright (©) 2010 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 "global.h"
27    
28 root 1.3 template<int N, typename T = float>
29     struct gen_vec_base
30     {
31     enum { D = N };
32     T v[N];
33    
34     T &operator [](int n)
35     {
36     return v[n];
37     }
38    
39     template<typename U>
40     const gen_vec_base<N,T> &operator =(gen_vec_base<N,U> v)
41     {
42     for (int i = 0; i < N; ++i)
43     this->v[i] = v.v[i];
44    
45     return *this;
46     }
47     };
48    
49     template<int N, typename T>
50     gen_vec_base<N,T> floor (gen_vec_base<N,T> v)
51     {
52     for (int i = 0; i < N; ++i)
53     v[i] = floor (v[i]);
54    
55     return v;
56     }
57    
58     template<int N, typename T, typename U>
59     gen_vec_base<N,T> operator +(gen_vec_base<N,T> a, gen_vec_base<N,U> b)
60     {
61     gen_vec_base<N,T> c;
62    
63     for (int i = 0; i < N; ++i)
64     c[i] = a[i] + b[i];
65    
66     return c;
67     }
68    
69     template<int N, typename T, typename U>
70     gen_vec_base<N,T> operator -(gen_vec_base<N,T> a, gen_vec_base<N,U> b)
71     {
72     gen_vec_base<N,T> c;
73    
74     for (int i = 0; i < N; ++i)
75     c[i] = a[i] - b[i];
76    
77     return c;
78     }
79    
80     template<int N, typename T, typename U>
81     T operator *(gen_vec_base<N,T> a, gen_vec_base<N,U> b)
82     {
83     T v = 0;
84    
85     for (int i = 0; i < N; ++i)
86     v += a[i] * b[i];
87    
88     return v;
89     }
90    
91     template<int N, typename T = float>
92     struct gen_vec;
93    
94     template<typename T>
95     struct gen_vec<2,T> : gen_vec_base<2,T>
96     {
97     using gen_vec_base<2,T>::operator =;
98    
99     gen_vec ()
100     {
101     }
102    
103     gen_vec (T x, T y)
104     {
105     this->v[0] = x;
106     this->v[1] = y;
107     }
108    
109     template<typename U>
110     gen_vec (gen_vec_base<2,U> v)
111     {
112     *this = v;
113     }
114     };
115    
116     template<typename T>
117     struct gen_vec<3,T> : gen_vec_base<3,T>
118     {
119     gen_vec ()
120     {
121     }
122    
123     gen_vec (T x, T y, T z)
124     {
125     this->v[0] = x;
126     this->v[1] = y;
127     this->v[2] = z;
128     }
129    
130     template<typename U>
131     gen_vec (gen_vec_base<3,U> v)
132     {
133     *this = v;
134     }
135     };
136    
137     typedef gen_vec<2> vec2d;
138     typedef gen_vec<3> vec3d;
139    
140     template<int N, typename T = float>
141     struct space
142     {
143     enum { D = N };
144     typedef gen_vec<N,T> vec;
145     };
146    
147     typedef space<2> space2d;
148     typedef space<3> space3d;
149    
150     /////////////////////////////////////////////////////////////////////////////
151    
152 root 1.1 template<int N, typename T = uint8_t>
153     struct permutation
154     {
155     T pmap [N];
156    
157     template<class generator>
158     void seed (generator &rng)
159     {
160     for (int i = 0; i < N; ++i)
161     pmap [i] = i;
162    
163     // fisher-yates to randomly perturb
164     for (int i = N; --i; )
165     ::swap (pmap [i], pmap [::rmg_rndm (i + 1)]);
166     }
167    
168     T operator ()(T v)
169     {
170     return pmap [v & (N - 1)];
171     }
172     };
173    
174 root 1.3 // modelled after 2d/3d kensler noise without projection
175     struct noise2d
176 root 1.1 {
177     permutation<256, uint8_t> rvmap[2];
178     float rvec [256][2]; // random unit vectors
179    
180 root 1.3 noise2d (uint32_t seed);
181 root 1.1 float noise (float x, float y);
182     };
183    
184 root 1.3 struct noise3d
185     {
186     permutation<256, uint8_t> rvmap[3];
187     float rvec [256][3]; // random unit vectors
188    
189     noise3d (uint32_t seed);
190     float noise (float x, float y, float z);
191    
192     // noise projected on a surface with normal n
193     float noise (float x, float y, float z, float nx, float ny, float nz);
194     };
195    
196 root 1.1 struct rotate
197     {
198     float s, c;
199    
200     void set (float angle)
201     {
202     s = sinf (angle);
203     c = cosf (angle);
204     }
205    
206     void operator ()(float &x, float &y) const
207     {
208     float xr = x * c - y * s;
209     float yr = x * s + y * c;
210    
211     x = xr;
212     y = yr;
213     }
214     };
215    
216     struct frac_gen
217 root 1.3 : noise2d
218 root 1.1 {
219     enum { MAX_OCTAVES = 64 };
220    
221     float h, lac, ex [MAX_OCTAVES];
222     float fbm_mul [MAX_OCTAVES];
223     rotate rot [MAX_OCTAVES];
224    
225     frac_gen (float hurst_expo = .5f, float lacunarity = 2.f);
226    
227     float fBm (float x, float y, int octaves);
228     float turbulence (float x, float y, int octaves);
229     float multifractal (float x, float y, int octaves, float offset = 1);
230     float heterofractal (float x, float y, int octaves, float offset = 1);
231     float hybridfractal (float x, float y, int octaves, float offset = 1, float gain = 1);
232     float terrain (float x, float y, int octaves);
233     float terrain2 (float x, float y, int octaves);
234     float ridgedmultifractal (float x, float y, int octaves, float offset = 1, float gain = 8);
235     };
236    
237     #endif