ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/noise.h
Revision: 1.1
Committed: Sun Jul 11 04:01:49 2010 UTC (13 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Log Message:
add noise library, as of yet, unused

File Contents

# Content
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 template<int N, typename T = uint8_t>
29 struct permutation
30 {
31 T pmap [N];
32
33 template<class generator>
34 void seed (generator &rng)
35 {
36 for (int i = 0; i < N; ++i)
37 pmap [i] = i;
38
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)
46 {
47 return pmap [v & (N - 1)];
48 }
49 };
50
51 struct noise_gen
52 {
53 permutation<256, uint8_t> rvmap[2];
54 float rvec [256][2]; // random unit vectors
55
56 noise_gen (uint32_t seed);
57 float noise (float x, float y);
58 };
59
60 struct rotate
61 {
62 float s, c;
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 };
79
80 struct frac_gen
81 : noise_gen
82 {
83 enum { MAX_OCTAVES = 64 };
84
85 float h, lac, ex [MAX_OCTAVES];
86 float fbm_mul [MAX_OCTAVES];
87 rotate rot [MAX_OCTAVES];
88
89 frac_gen (float hurst_expo = .5f, float lacunarity = 2.f);
90
91 float fBm (float x, float y, int octaves);
92 float turbulence (float x, float y, int octaves);
93 float multifractal (float x, float y, int octaves, float offset = 1);
94 float heterofractal (float x, float y, int octaves, float offset = 1);
95 float hybridfractal (float x, float y, int octaves, float offset = 1, float gain = 1);
96 float terrain (float x, float y, int octaves);
97 float terrain2 (float x, float y, int octaves);
98 float ridgedmultifractal (float x, float y, int octaves, float offset = 1, float gain = 8);
99 };
100
101 #endif