1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
5 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
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 |
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 |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
9 | * option) any later version. |
… | |
… | |
23 | #include "noise.h" |
23 | #include "noise.h" |
24 | |
24 | |
25 | ///////////////////////////////////////////////////////////////////////////// |
25 | ///////////////////////////////////////////////////////////////////////////// |
26 | |
26 | |
27 | // http://www-wales.ch.cam.ac.uk/~wales/CCD/Thomson/table.html, random permutation |
27 | // http://www-wales.ch.cam.ac.uk/~wales/CCD/Thomson/table.html, random permutation |
|
|
28 | // used as gradient vectors for noise3d |
28 | static const float charges[256][3] = { |
29 | static const float charges[256][3] = { |
29 | 6.32096444e-01, -6.11406706e-01, 4.76062943e-01, 5.19172442e-01, 8.08229585e-01, -2.77893708e-01, |
30 | 6.32096444e-01, -6.11406706e-01, 4.76062943e-01, 5.19172442e-01, 8.08229585e-01, -2.77893708e-01, |
30 | -8.81072422e-01, -4.68301230e-01, 6.63727731e-02, -1.92122323e-01, 9.81341519e-01, -7.60505942e-03, |
31 | -8.81072422e-01, -4.68301230e-01, 6.63727731e-02, -1.92122323e-01, 9.81341519e-01, -7.60505942e-03, |
31 | -2.57478265e-01, 9.34157733e-01, -2.47091628e-01, 8.75501090e-01, -1.48087600e-01, -4.59965112e-01, |
32 | -2.57478265e-01, 9.34157733e-01, -2.47091628e-01, 8.75501090e-01, -1.48087600e-01, -4.59965112e-01, |
32 | -6.44086082e-01, 3.21868600e-01, 6.93940720e-01, -4.42053435e-01, -1.94962529e-01, -8.75544615e-01, |
33 | -6.44086082e-01, 3.21868600e-01, 6.93940720e-01, -4.42053435e-01, -1.94962529e-01, -8.75544615e-01, |
… | |
… | |
154 | 4.98855035e-01, -3.98703643e-01, 7.69531714e-01, 2.55373649e-01, -7.56251592e-02, 9.63880249e-01, |
155 | 4.98855035e-01, -3.98703643e-01, 7.69531714e-01, 2.55373649e-01, -7.56251592e-02, 9.63880249e-01, |
155 | -1.51053916e-01, 3.85469253e-01, -9.10272580e-01, 6.78644990e-01, -7.26064181e-01, 1.10778074e-01, |
156 | -1.51053916e-01, 3.85469253e-01, -9.10272580e-01, 6.78644990e-01, -7.26064181e-01, 1.10778074e-01, |
156 | -3.07350590e-01, -9.14677987e-01, 2.62487703e-01, 5.60298616e-01, 1.74821302e-01, 8.09631381e-01, |
157 | -3.07350590e-01, -9.14677987e-01, 2.62487703e-01, 5.60298616e-01, 1.74821302e-01, 8.09631381e-01, |
157 | }; |
158 | }; |
158 | |
159 | |
|
|
160 | ///////////////////////////////////////////////////////////////////////////// |
|
|
161 | |
|
|
162 | template<int N, typename T> |
|
|
163 | template<class random_generator> |
|
|
164 | void |
|
|
165 | permutation<N,T>::seed (random_generator &rng) |
|
|
166 | { |
|
|
167 | for (int i = 0; i < N; ++i) |
|
|
168 | pmap[i] = i; |
|
|
169 | |
|
|
170 | // fisher-yates to randomly perturb |
|
|
171 | for (int i = N; --i; ) |
|
|
172 | ::swap (pmap[i], pmap[rng (i + 1)]); |
|
|
173 | } |
|
|
174 | |
|
|
175 | template class permutation<256, uint8_t>; |
|
|
176 | |
|
|
177 | ///////////////////////////////////////////////////////////////////////////// |
|
|
178 | |
159 | static vec2d |
179 | static vec2d |
160 | floor (vec2d v) |
180 | floor (vec2d v) |
161 | { |
181 | { |
162 | return vec2d (std::floor (v[0]), std::floor (v[1])); |
182 | return vec2d (std::floor (v[0]), std::floor (v[1])); |
163 | } |
183 | } |
… | |
… | |
170 | |
190 | |
171 | noise_gen<vec2d>::noise_gen (uint32_t seed) |
191 | noise_gen<vec2d>::noise_gen (uint32_t seed) |
172 | { |
192 | { |
173 | seedable_rand_gen rng (seed); |
193 | seedable_rand_gen rng (seed); |
174 | |
194 | |
175 | #if 1 |
195 | #if 0 |
176 | for (int i = 0; i < 256; ++i) |
196 | for (int i = 0; i < 256; ++i) |
177 | { |
197 | { |
178 | vec2d rv (rng () - 0.5f, rng () - 0.5f); |
198 | vec2d rv (rng () - 0.5f, rng () - 0.5f); |
179 | |
199 | |
180 | // normalise |
200 | // normalise |
… | |
… | |
185 | |
205 | |
186 | rvec[i] = rv; |
206 | rvec[i] = rv; |
187 | } |
207 | } |
188 | #endif |
208 | #endif |
189 | |
209 | |
190 | #if 0 |
210 | #if 1 |
191 | float offset = rng () * 3.14159265; |
211 | float offset = rng () * M_PI; |
192 | |
212 | |
193 | for (int i = 0; i < 256; ++i) |
213 | for (int i = 0; i < 256; ++i) |
194 | { |
214 | { |
195 | float phi = i * (3.14159265 * 2 / 256) + offset; |
215 | float phi = i * (M_PI * 2 / 256) + offset; |
196 | |
216 | |
197 | rvec[i][0] = sinf (phi); |
217 | rvec[i][0] = sinf (phi); |
198 | rvec[i][1] = -cosf (phi); |
218 | rvec[i][1] = -cosf (phi); |
199 | } |
219 | } |
200 | |
220 | |
… | |
… | |
516 | template class frac_gen<vec2d>; |
536 | template class frac_gen<vec2d>; |
517 | template class frac_gen<vec3d>; |
537 | template class frac_gen<vec3d>; |
518 | |
538 | |
519 | ///////////////////////////////////////////////////////////////////////////// |
539 | ///////////////////////////////////////////////////////////////////////////// |
520 | |
540 | |
521 | #if 0 |
541 | void noise_test (); |
522 | void hack(); void hack() |
542 | void noise_test () |
523 | { |
543 | { |
524 | frac_gen<vec2d> gen (0.5, 2); |
544 | frac_gen<vec2d> gen (0.5, 1.9); |
525 | frac_gen<vec3d> gen3; |
545 | frac_gen<vec3d> gen3; |
526 | |
546 | |
527 | #if 0 |
547 | #if 1 |
528 | int N = 1024; |
548 | int N = 1024; |
529 | |
549 | |
530 | printf ("P5 %d %d 255\n", N, N); |
550 | printf ("P5 %d %d 255\n", N, N); |
531 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
551 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
532 | for (int y = 0; y < N; ++y) |
552 | for (int y = 0; y < N; ++y) |
… | |
… | |
535 | for (int x = 0; x < N; ++x) |
555 | for (int x = 0; x < N; ++x) |
536 | { |
556 | { |
537 | //putc (127 * n.noise (x * 0.1, y * 0.1, 0, 0,0,1) + 128, stdout); |
557 | //putc (127 * n.noise (x * 0.1, y * 0.1, 0, 0,0,1) + 128, stdout); |
538 | //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout); |
558 | //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout); |
539 | //putc (256 * gen.fBm (x * 0.01, y * 0.01, 3), stdout); |
559 | //putc (256 * gen.fBm (x * 0.01, y * 0.01, 3), stdout); |
540 | //putc (256 * gen.turbulence (x * 0.004 - 1, y * 0.004 - 1, 8), stdout); |
560 | putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 8), stdout); |
541 | //putc (256 * gen.heterofractal (x * 0.008, y * 0.008, 8, 0.9), stdout); |
561 | //putc (256 * gen.heterofractal (x * 0.008, y * 0.008, 8, 0.9), stdout); |
542 | |
562 | |
543 | // mountais or somesuch(?) |
563 | // mountais or somesuch(?) |
544 | //putc (256 * gen.hybridfractal (x * 0.01, y * 0.01, 8, -.4, -4), stdout); |
564 | //putc (256 * gen.hybridfractal (x * 0.01, y * 0.01, 8, -.4, -4), stdout); |
545 | |
565 | |
546 | // temperature |
566 | // temperature |
547 | //putc (256 * gen.fBm (x * 0.002, y * 0.002, 2), stdout); |
567 | //putc (256 * gen.fBm (x * 0.002, y * 0.002, 2), stdout); |
548 | // rain |
568 | // rain |
|
|
569 | |
|
|
570 | //cells |
549 | //putc (256 * gen.ridgedmultifractal (x * 0.01, y * 0.01, 3, 1.03, 2), stdout); |
571 | //putc (256 * gen.ridgedmultifractal (vec2d (x * 0.01, y * 0.01), 3, 0.5 + gen.noise(vec2d(x*0.01,y*0.01)), 2), stdout); |
550 | } |
572 | } |
551 | } |
573 | } |
552 | #else |
574 | #else |
553 | int N = 512; |
575 | int N = 512; |
554 | |
576 | |
… | |
… | |
590 | } |
612 | } |
591 | #endif |
613 | #endif |
592 | |
614 | |
593 | exit (0); |
615 | exit (0); |
594 | } |
616 | } |
595 | #endif |
|
|