… | |
… | |
267 | vec2d I = floor (P); |
267 | vec2d I = floor (P); |
268 | vec2d F = P - I; |
268 | vec2d F = P - I; |
269 | |
269 | |
270 | value_t v = 0; |
270 | value_t v = 0; |
271 | |
271 | |
|
|
272 | seed *= 3039177861; |
272 | uint8_t i1 = uint8_t (I[1]) ^ (seed >> 16); |
273 | uint8_t i1 = uint8_t (I[1]) + (seed >> 16); |
273 | uint8_t i0 = uint8_t (I[0]) ^ (seed >> 8); |
274 | uint8_t i0 = uint8_t (I[0]) + (seed >> 8); |
|
|
275 | |
|
|
276 | uint8_t h2 = rvmap[2](seed); |
274 | |
277 | |
275 | for (int j = -1; j <= 2; ++j) |
278 | for (int j = -1; j <= 2; ++j) |
276 | { |
279 | { |
277 | uint8_t h1 = rvmap[1](i1 + j) ^ seed; |
280 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
278 | |
281 | |
279 | for (int i = -1; i <= 2; ++i) |
282 | for (int i = -1; i <= 2; ++i) |
280 | { |
283 | { |
281 | vec2d A = F - vec2d (i, j); |
284 | vec2d A = F - vec2d (i, j); |
282 | value_t d = dot (A, A); |
285 | value_t d = dot (A, A); |
… | |
… | |
307 | noise_gen_base<vec3d>::operator() (vec3d P, uint32_t seed) |
310 | noise_gen_base<vec3d>::operator() (vec3d P, uint32_t seed) |
308 | { |
311 | { |
309 | vec3d I = floor (P); |
312 | vec3d I = floor (P); |
310 | vec3d F = P - I; |
313 | vec3d F = P - I; |
311 | |
314 | |
|
|
315 | seed *= 3039177861; |
312 | uint8_t i2 = uint8_t (I[2]) ^ (seed >> 24); |
316 | uint8_t i2 = uint8_t (I[2]) + (seed >> 24); |
313 | uint8_t i1 = uint8_t (I[1]) ^ (seed >> 16); |
317 | uint8_t i1 = uint8_t (I[1]) + (seed >> 16); |
314 | uint8_t i0 = uint8_t (I[0]) ^ (seed >> 8); |
318 | uint8_t i0 = uint8_t (I[0]) + (seed >> 8); |
|
|
319 | |
|
|
320 | uint8_t h3 = rvmap[3](seed); |
315 | |
321 | |
316 | value_t v = 0; |
322 | value_t v = 0; |
317 | |
323 | |
318 | for (int k = -1; k <= 2; ++k) |
324 | for (int k = -1; k <= 2; ++k) |
319 | { |
325 | { |
320 | uint8_t h2 = rvmap[2](i2 + k) ^ seed; |
326 | uint8_t h2 = rvmap[2](i2 + k) ^ h3; |
321 | |
327 | |
322 | for (int j = -1; j <= 2; ++j) |
328 | for (int j = -1; j <= 2; ++j) |
323 | { |
329 | { |
324 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
330 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
325 | |
331 | |
… | |
… | |
353 | noise_gen<vec3d>::operator() (vec3d P, vec3d N, uint32_t seed) |
359 | noise_gen<vec3d>::operator() (vec3d P, vec3d N, uint32_t seed) |
354 | { |
360 | { |
355 | vec3d I = floor (P); |
361 | vec3d I = floor (P); |
356 | vec3d F = P - I; |
362 | vec3d F = P - I; |
357 | |
363 | |
|
|
364 | seed *= 3039177861; |
358 | uint8_t i2 = uint8_t (I[2]) ^ (seed >> 24); |
365 | uint8_t i2 = uint8_t (I[2]) + (seed >> 24); |
359 | uint8_t i1 = uint8_t (I[1]) ^ (seed >> 16); |
366 | uint8_t i1 = uint8_t (I[1]) + (seed >> 16); |
360 | uint8_t i0 = uint8_t (I[0]) ^ (seed >> 8); |
367 | uint8_t i0 = uint8_t (I[0]) + (seed >> 8); |
|
|
368 | |
|
|
369 | uint8_t h3 = rvmap[3](seed); |
361 | |
370 | |
362 | value_t v = 0; |
371 | value_t v = 0; |
363 | |
372 | |
364 | for (int k = -1; k <= 2; ++k) |
373 | for (int k = -1; k <= 2; ++k) |
365 | { |
374 | { |
366 | uint8_t h2 = rvmap[2](i2 + k) ^ seed; |
375 | uint8_t h2 = rvmap[2](i2 + k) ^ h3; |
367 | |
376 | |
368 | for (int j = -1; j <= 2; ++j) |
377 | for (int j = -1; j <= 2; ++j) |
369 | { |
378 | { |
370 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
379 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
371 | |
380 | |
… | |
… | |
603 | template class frac_gen<vec2d>; |
612 | template class frac_gen<vec2d>; |
604 | template class frac_gen<vec3d>; |
613 | template class frac_gen<vec3d>; |
605 | |
614 | |
606 | ///////////////////////////////////////////////////////////////////////////// |
615 | ///////////////////////////////////////////////////////////////////////////// |
607 | |
616 | |
|
|
617 | template<typename T, typename U> |
|
|
618 | T |
|
|
619 | inline border_blend (T a, T b, vec2d P, U N, U W) |
|
|
620 | { |
|
|
621 | U border = W; // within n places of the border |
|
|
622 | |
|
|
623 | min_it (border, P [0]); |
|
|
624 | min_it (border, N - P [0]); |
|
|
625 | min_it (border, P [1]); |
|
|
626 | min_it (border, N - P [1]); |
|
|
627 | |
|
|
628 | return blend (a, b,border, U(0), W); |
|
|
629 | } |
|
|
630 | |
|
|
631 | // highest mountains, deepest sea == 200 .. -200 |
|
|
632 | |
|
|
633 | #define FANCY_GRAPHICS 1 |
|
|
634 | |
|
|
635 | static void |
|
|
636 | gen_quadspace (int x, int y, int z) |
|
|
637 | { |
|
|
638 | vec2d P = vec2d (x, y); |
|
|
639 | |
|
|
640 | const int deep_sea_z = -200; |
|
|
641 | const int mountain_z = 200; |
|
|
642 | |
|
|
643 | static frac2d gen(13); |
|
|
644 | |
|
|
645 | static frac2d vec_gen1 (6, 2, 0.5, 1); |
|
|
646 | static frac2d vec_gen2 (6, 2, 0.5, 2); |
|
|
647 | |
|
|
648 | const float continent_scale = 0.00008; |
|
|
649 | |
|
|
650 | vec2d perturb_pos = pow (P, 1.4) * 1e-5; |
|
|
651 | |
|
|
652 | vec2d perturb ( |
|
|
653 | vec_gen1.fBm (perturb_pos), |
|
|
654 | vec_gen2.fBm (perturb_pos) |
|
|
655 | ); |
|
|
656 | |
|
|
657 | float perturb_perturb = 1 - (P[1] - P[0]) * (1. / 25000 / 2); |
|
|
658 | perturb_perturb = perturb_perturb * perturb_perturb * 0.4; |
|
|
659 | perturb *= perturb_perturb; |
|
|
660 | |
|
|
661 | vec2d P_continent = P * continent_scale + perturb; |
|
|
662 | |
|
|
663 | static frac2d continent_gen (13, 2.13, 0.5); |
|
|
664 | float continent = continent_gen.fBm (P_continent) + 0.05f; |
|
|
665 | |
|
|
666 | float x_gradient = P[0] * (1. / 25000); |
|
|
667 | float y_gradient = P[1] * (1. / 25000); |
|
|
668 | float xy_gradient = (P[0] + P[1]) * (0.5 / 25000); |
|
|
669 | |
|
|
670 | const float N = (25000 - 1) * continent_scale; |
|
|
671 | |
|
|
672 | // we clip a large border on the perturbed shape, to get irregular coastline |
|
|
673 | // and then clip a smaller border around the real shape |
|
|
674 | continent = border_blend (-1.f, continent, P_continent , N, 400 * continent_scale); |
|
|
675 | continent = border_blend (-1.f, continent, P * continent_scale + perturb * 0.1, N, 100 * continent_scale); |
|
|
676 | |
|
|
677 | enum { |
|
|
678 | T_NONE, |
|
|
679 | T_OCEAN, |
|
|
680 | T_RIVER, |
|
|
681 | T_VALLEY, |
|
|
682 | T_MOUNTAIN, |
|
|
683 | T_UNDERGROUND, |
|
|
684 | T_ACQUIFER, |
|
|
685 | } t = T_NONE; |
|
|
686 | |
|
|
687 | vec3d c; |
|
|
688 | int h = 1000000; // height form heightmap |
|
|
689 | |
|
|
690 | // the continent increases in height from 0 to ~700 levels in the absence of anything else |
|
|
691 | // thats about one step every 7 maps. |
|
|
692 | int base_height = blend (0, 300, xy_gradient, 0.2f, 0.9f); |
|
|
693 | int river_height = base_height * 9 / 10; |
|
|
694 | |
|
|
695 | // add this to rivers to "dry them out" |
|
|
696 | float dry_out = max (0.f, lerp (xy_gradient, 0.7f, 1.f, 0.f, 0.3f)); |
|
|
697 | |
|
|
698 | static frac2d river_gen (2); |
|
|
699 | float river1 = abs (river_gen.fBm (P * 0.001 + perturb * 4)) + dry_out; |
|
|
700 | float river2 = river_gen.ridgedmultifractal (P * 0.04, 0.8, 10) - y_gradient * 0.2 - 0.16 - dry_out; |
|
|
701 | |
|
|
702 | float valley = river1 - 0.2f; |
|
|
703 | |
|
|
704 | static frac2d mountain_gen (8, 2.14, 0.5); |
|
|
705 | float mountain = mountain_gen.ridgedmultifractal (P * 0.004); |
|
|
706 | |
|
|
707 | t = valley < 0 ? T_VALLEY : T_MOUNTAIN; |
|
|
708 | c = blend0 (vec3d (0, 0.8, 0), vec3d (0.8, 0, 0), valley, 0.1f); |
|
|
709 | h = blend0 (base_height + continent * 300, base_height + mountain * xy_gradient * 400, valley, 0.1f); |
|
|
710 | |
|
|
711 | if (river1 < 0.01f) |
|
|
712 | { |
|
|
713 | // main rivers - they cut deeply into the mountains (base_height * 0.9f) |
|
|
714 | t = T_RIVER; |
|
|
715 | c = vec3d (0.2, 0.2, 1); |
|
|
716 | min_it (h, river_height + lerp<float> (river1, 0.f, 0.01f, -20, -1)); |
|
|
717 | } |
|
|
718 | |
|
|
719 | if (river2 > 0) |
|
|
720 | { |
|
|
721 | t = T_RIVER; |
|
|
722 | c = vec3d (0.2, 0.2, 1); |
|
|
723 | min_it (h, river_height + lerp<float> (river1, 0.f, 0.01f, -5, -1)); |
|
|
724 | } |
|
|
725 | |
|
|
726 | if (continent < 0) |
|
|
727 | { |
|
|
728 | t = T_OCEAN; |
|
|
729 | min_it (h, min (continent * 200, -1)); |
|
|
730 | c = vec3d (0, 0, 1); |
|
|
731 | } |
|
|
732 | |
|
|
733 | // now we have the base height, and base terrain |
|
|
734 | |
|
|
735 | #if FANCY_GRAPHICS |
|
|
736 | z = h; // show the surface, not the given z layer |
|
|
737 | #endif |
|
|
738 | |
|
|
739 | // everything below the surface is underground, or a variant |
|
|
740 | if (z < h) |
|
|
741 | { |
|
|
742 | t = T_UNDERGROUND; |
|
|
743 | } |
|
|
744 | |
|
|
745 | // put acquifers a bit below the surface, to reduce them leaking out (will still happen) |
|
|
746 | if (z < h - 3) |
|
|
747 | { |
|
|
748 | static frac3d acquifer_gen (4); |
|
|
749 | float acquifer = acquifer_gen.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.01), 1.003, 2); |
|
|
750 | |
|
|
751 | if (acquifer > 0.48) |
|
|
752 | { |
|
|
753 | t = T_ACQUIFER; |
|
|
754 | c = vec3d (1,1,1); |
|
|
755 | } |
|
|
756 | } |
|
|
757 | |
|
|
758 | // TODO: caves |
|
|
759 | // TODO: chees areas |
|
|
760 | // TODO: minerals |
|
|
761 | // TODO: monsters |
|
|
762 | |
|
|
763 | #if FANCY_GRAPHICS |
|
|
764 | float v = clamp (lerp<float> (h, deep_sea_z, mountain_z*0+800, 0.f, 1.f), 0.f, 1.f); |
|
|
765 | c *= v; |
|
|
766 | |
|
|
767 | putc (clamp<int> (255 * c[0], 0, 255), stdout); |
|
|
768 | putc (clamp<int> (255 * c[1], 0, 255), stdout); |
|
|
769 | putc (clamp<int> (255 * c[2], 0, 255), stdout); |
|
|
770 | #endif |
|
|
771 | } |
|
|
772 | |
608 | void noise_test (); |
773 | void noise_test (); |
609 | void noise_test () |
774 | void noise_test () |
610 | { |
775 | { |
611 | frac2d gen; |
|
|
612 | |
|
|
613 | frac2d vec_gen1 (4, 2, 0.5, 1); |
|
|
614 | frac2d vec_gen2 (4, 2, 0.5, 2); |
|
|
615 | |
|
|
616 | #if 1 |
776 | #if 0 |
617 | int N = 1024; |
777 | int Nw = 700; |
618 | |
778 | |
619 | printf ("P6 %d %d 255\n", N, N); |
779 | printf ("P6 %d %d 255\n", Nw * 3, Nw * 2); |
620 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
780 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
621 | for (int y = 0; y < N; ++y) |
781 | for (int y = 0; y < Nw; ++y) |
622 | { |
782 | { |
623 | if (!(y&63))fprintf (stderr, "y %d\n", y);//D |
783 | if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw);//D |
|
|
784 | |
|
|
785 | for (int x = 0; x < Nw; ++x) gen_quadspace (x * 25000 / Nw, y * 25000 / Nw, 0); |
|
|
786 | |
|
|
787 | for (int x = 0; x < Nw; ++x) gen_quadspace (x + 400, y, 0); |
|
|
788 | for (int x = 0; x < Nw; ++x) gen_quadspace (x + 22000, y + 2000, 0); |
|
|
789 | } |
624 | for (int x = 0; x < N; ++x) |
790 | for (int y = 0; y < Nw; ++y) |
625 | { |
791 | { |
626 | vec2d P = vec2d (x, y) * (25000.f / N); |
792 | if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw+50);//D |
627 | |
793 | |
|
|
794 | for (int x = 0; x < Nw; ++x) gen_quadspace (x + 1000, y + 22000, 0); |
|
|
795 | for (int x = 0; x < Nw; ++x) gen_quadspace (x + 12500, y + 12500, 0); |
|
|
796 | for (int x = 0; x < Nw; ++x) gen_quadspace (x + 22000, y + 22500, 0); |
|
|
797 | } |
|
|
798 | |
628 | //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout); |
799 | //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout); |
629 | //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout); |
800 | //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout); |
630 | //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout); |
801 | //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout); |
631 | //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout); |
802 | //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout); |
632 | //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout); |
803 | //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout); |
633 | |
|
|
634 | // mountais or somesuch(?) |
|
|
635 | //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout); |
804 | //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout); |
636 | |
|
|
637 | // temperature |
|
|
638 | //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout); |
805 | //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout); |
639 | // rain |
|
|
640 | |
|
|
641 | float continent; |
|
|
642 | |
|
|
643 | { |
|
|
644 | const float continent_scale = 0.00008; |
|
|
645 | |
|
|
646 | vec2d perturb ( |
|
|
647 | vec_gen1.fBm (P * 0.0004), |
|
|
648 | vec_gen2.fBm (P * 0.0004) |
|
|
649 | ); |
|
|
650 | |
|
|
651 | const float W = 1000 * continent_scale; |
|
|
652 | const float N = (25000 - 1) * continent_scale; |
|
|
653 | |
|
|
654 | static frac2d perturb_gen (4, 2, 0.5, 3); |
|
|
655 | float perturb_perturb = gen.fBm (P * 0.0004); |
|
|
656 | perturb_perturb = perturb_perturb * perturb_perturb * 0.004; |
|
|
657 | |
|
|
658 | vec2d P_perturb = P * continent_scale + perturb * min (W, gen.noise (P * perturb_perturb) * W * 8); |
|
|
659 | |
|
|
660 | static frac2d continent_gen (13, 2.13, 0.5); |
|
|
661 | continent = continent_gen.fBm (P_perturb) + 0.05f; |
|
|
662 | |
|
|
663 | float border = W; // within n places of the border |
|
|
664 | |
|
|
665 | min_it (border, P_perturb [0]); |
|
|
666 | min_it (border, N - P_perturb [0]); |
|
|
667 | min_it (border, P_perturb [1]); |
|
|
668 | min_it (border, N - P_perturb [1]); |
|
|
669 | |
|
|
670 | continent = blend (-1.f, continent, border, 0.f, W); |
|
|
671 | } |
|
|
672 | |
|
|
673 | vec3d c (1, 1, 1); |
|
|
674 | float v; |
|
|
675 | |
|
|
676 | if (continent < 0) |
|
|
677 | { |
|
|
678 | // ocean |
|
|
679 | |
|
|
680 | v = min (continent * 10, -0.2f); |
|
|
681 | c = vec3d (0, 0, 1); |
|
|
682 | } |
|
|
683 | else |
|
|
684 | { |
|
|
685 | // continent |
|
|
686 | |
|
|
687 | // big rivers |
|
|
688 | static frac2d river_gen (2); |
|
|
689 | float river1 = river_gen.fBm (P * 0.0004);// - (P[0] / 25000) * 0.18 + 0.06; |
|
|
690 | float river2 = river_gen.ridgedmultifractal (P * 0.04 + vec2d (3, 5), 0.8, 10) - (P[1] / 25000) * 0.1; |
|
|
691 | |
|
|
692 | if (river1 > 0.05f) |
|
|
693 | { |
|
|
694 | v = -0.1f; |
|
|
695 | c = vec3d (0.4, 0.4, 1); |
|
|
696 | } |
|
|
697 | else if (river1 > 0.08f && river2 > 0.1f) |
|
|
698 | { |
|
|
699 | v = -0.05f; |
|
|
700 | c = vec3d (0.4, 0.4, 1); |
|
|
701 | } |
|
|
702 | else |
|
|
703 | { |
|
|
704 | river1 -= 0.07f; |
|
|
705 | |
|
|
706 | //c = river1 > 0 ? vec3d (0.8, 0.8, 0) : vec3d (0.8, 0, 0); |
|
|
707 | c = blend0 (vec3d (0.8, 0, 0), vec3d (0.8, 0.8, 0), river1, 0.01f);; |
|
|
708 | |
|
|
709 | static frac2d mountain_gen (8, 2.14, 0.5); |
|
|
710 | float mountain = mountain_gen.ridgedmultifractal (P * 0.004); |
|
|
711 | v = blend0 (mountain * 3 - 1, continent, river1, 0.05f); |
|
|
712 | } |
|
|
713 | |
|
|
714 | v=abs(river1) < 0.1; |
|
|
715 | c=vec3d(1,1,1); |
|
|
716 | } |
|
|
717 | |
|
|
718 | c *= v * 0.5 + 0.5; |
|
|
719 | |
|
|
720 | putc (clamp<int> (255 * c[0], 0, 255), stdout); |
|
|
721 | putc (clamp<int> (255 * c[1], 0, 255), stdout); |
|
|
722 | putc (clamp<int> (255 * c[2], 0, 255), stdout); |
|
|
723 | |
|
|
724 | //cells |
|
|
725 | //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout); |
806 | //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout); |
726 | } |
|
|
727 | } |
|
|
728 | #else |
807 | #else |
729 | int N = 128; |
808 | int N = 200; |
730 | |
809 | |
731 | //printf ("P6 %d %d 255\n", N, N); |
810 | //printf ("P6 %d %d 255\n", N, N); |
732 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
811 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
733 | frac3d gen3 (3);; |
|
|
734 | for (int z = 0; z < N; ++z) |
812 | for (int z = 0; z < N; ++z) |
735 | { |
813 | { |
736 | if (!(z&7))fprintf (stderr, "z %d\n", z);//D |
814 | if (!(z&7))fprintf (stderr, "z %d\n", z);//D |
737 | for (int y = 0; y < N; ++y) |
815 | for (int y = 0; y < N; ++y) |
738 | for (int x = 0; x < N; ++x) |
816 | for (int x = 0; x < N; ++x) |
739 | { |
817 | { |
|
|
818 | #if 0 |
740 | float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2; |
819 | float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2; |
741 | |
820 | |
742 | #if 0 |
|
|
743 | if (z < 64) |
821 | if (z < 64) |
744 | v = v * (z * z) / (64 * 64); |
822 | v = v * (z * z) / (64 * 64); |
745 | #endif |
|
|
746 | |
823 | |
747 | if (v <= 0.9) |
824 | if (v <= 0.9) |
748 | continue; |
825 | continue; |
|
|
826 | #endif |
|
|
827 | static frac3d gen3 (10); |
|
|
828 | //float v = gen3.turbulence (vec3d (x * 0.01, y * 0.01, z * 0.01)); |
|
|
829 | float v = gen3.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.001), 1.003, 2); |
|
|
830 | |
|
|
831 | if (v <= 0.48) continue; |
749 | |
832 | |
750 | float r[4]; |
833 | float r[4]; |
751 | int i[4]; |
834 | int i[4]; |
752 | |
835 | |
753 | r[0] = x; |
836 | r[0] = x; |