ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/noise.C
(Generate patch)

Comparing deliantra/server/common/noise.C (file contents):
Revision 1.16 by root, Sat Apr 30 19:04:50 2011 UTC vs.
Revision 1.18 by root, Sun May 1 13:18:23 2011 UTC

610} 610}
611 611
612template class frac_gen<vec2d>; 612template class frac_gen<vec2d>;
613template class frac_gen<vec3d>; 613template class frac_gen<vec3d>;
614 614
615/////////////////////////////////////////////////////////////////////////////
616
617template<typename T, typename U>
618T
619inline 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
633static void
634gen_height (int x, int y)
635{
636 vec2d P = vec2d (x, y);
637
638 const int deep_sea_z = -200;
639 const int mountain_z = 200;
640
641 static frac2d gen(13);
642
643 static frac2d vec_gen1 (6, 2, 0.5, 1);
644 static frac2d vec_gen2 (6, 2, 0.5, 2);
645
646 const float continent_scale = 0.00008;
647
648 vec2d perturb_pos = pow (P, 1.4) * 1e-5;
649
650 vec2d perturb (
651 vec_gen1.fBm (perturb_pos),
652 vec_gen2.fBm (perturb_pos)
653 );
654
655 float perturb_perturb = 1 - (P[1] - P[0]) * (1. / 25000 / 2);
656 perturb_perturb = perturb_perturb * perturb_perturb * 0.4;
657 perturb *= perturb_perturb;
658
659 vec2d P_continent = P * continent_scale + perturb;
660
661 static frac2d continent_gen (13, 2.13, 0.5);
662 float continent = continent_gen.fBm (P_continent) + 0.05f;
663
664 float x_gradient = P[0] * (1. / 25000);
665 float y_gradient = P[1] * (1. / 25000);
666 float xy_gradient = (P[0] + P[1]) * (0.5 / 25000);
667
668 const float N = (25000 - 1) * continent_scale;
669
670 // we clip a large border on the perturbed shape, to get irregular coastline
671 // and then clip a smaller border around the real shape
672 continent = border_blend (-1.f, continent, P_continent , N, 400 * continent_scale);
673 continent = border_blend (-1.f, continent, P * continent_scale + perturb * 0.1, N, 100 * continent_scale);
674
675 enum {
676 T_NONE,
677 T_OCEAN,
678 T_RIVER,
679 T_VALLEY,
680 T_MOUNTAIN,
681 } t = T_NONE;
682
683 vec3d c;
684 int z = 1000000;
685
686 // the continent increases in height from 0 to ~700 levels in the absence of anything else
687 // thats about one step every 7 maps.
688 int base_height = blend (0, 300, xy_gradient, 0.2f, 0.9f);
689 int river_height = base_height * 9 / 10;
690
691 // add this to rivers to "dry them out"
692 float dry_out = max (0.f, lerp (xy_gradient, 0.7f, 1.f, 0.f, 0.3f));
693
694 static frac2d river_gen (2);
695 float river1 = abs (river_gen.fBm (P * 0.001 + perturb * 4)) + dry_out;
696 float river2 = river_gen.ridgedmultifractal (P * 0.04, 0.8, 10) - y_gradient * 0.2 - 0.16 - dry_out;
697
698 float valley = river1 - 0.2f;
699
700 static frac2d mountain_gen (8, 2.14, 0.5);
701 float mountain = mountain_gen.ridgedmultifractal (P * 0.004);
702
703 t = valley < 0 ? T_VALLEY : T_MOUNTAIN;
704 c = blend0 (vec3d (0.8, 0.8, 0), vec3d (0.8, 0, 0), valley, 0.1f);
705 z = blend0 (base_height + continent * 300, base_height + mountain * xy_gradient * 400, valley, 0.1f);
706
707 if (river1 < 0.01f)
708 {
709 // main rivers - they cut deeply into the mountains (base_height * 0.9f)
710 t = T_RIVER;
711 c = vec3d (0.2, 0.2, 1);
712 min_it (z, river_height + lerp<float> (river1, 0.f, 0.01f, -20, -1));
713 }
714
715 if (river2 > 0)
716 {
717 t = T_RIVER;
718 c = vec3d (0.2, 0.2, 1);
719 min_it (z, river_height + lerp<float> (river1, 0.f, 0.01f, -5, -1));
720 }
721
722 if (continent < 0)
723 {
724 t = T_OCEAN;
725 min_it (z, min (continent * 200, -1));
726 c = vec3d (0, 0, 1);
727 }
728
729 float v = clamp (lerp<float> (z, deep_sea_z, mountain_z*0+800, 0.f, 1.f), 0.f, 1.f);
730 c *= v;
731
732 putc (clamp<int> (255 * c[0], 0, 255), stdout);
733 putc (clamp<int> (255 * c[1], 0, 255), stdout);
734 putc (clamp<int> (255 * c[2], 0, 255), stdout);
735}
736
737void noise_test ();
738void noise_test ()
739{
740#if 1
741 int Nw = 700;
742
743 printf ("P6 %d %d 255\n", Nw * 3, Nw * 2);
744 // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
745 for (int y = 0; y < Nw; ++y)
746 {
747 if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw);//D
748
749 for (int x = 0; x < Nw; ++x) gen_height (x * 25000 / Nw, y * 25000 / Nw);
750
751 for (int x = 0; x < Nw; ++x) gen_height (x + 400, y);
752 for (int x = 0; x < Nw; ++x) gen_height (x + 22000, y + 2000);
753 }
754 for (int y = 0; y < Nw; ++y)
755 {
756 if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw+50);//D
757
758 for (int x = 0; x < Nw; ++x) gen_height (x + 1000, y + 22000);
759 for (int x = 0; x < Nw; ++x) gen_height (x + 12500, y + 12500);
760 for (int x = 0; x < Nw; ++x) gen_height (x + 22000, y + 22000);
761 }
762
763 //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout);
764 //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout);
765 //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout);
766 //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout);
767 //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout);
768 //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout);
769 //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout);
770 //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout);
771#else
772 int N = 128;
773
774 //printf ("P6 %d %d 255\n", N, N);
775 // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
776 frac3d gen3 (3);;
777 for (int z = 0; z < N; ++z)
778 {
779 if (!(z&7))fprintf (stderr, "z %d\n", z);//D
780 for (int y = 0; y < N; ++y)
781 for (int x = 0; x < N; ++x)
782 {
783 float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2;
784
785#if 0
786 if (z < 64)
787 v = v * (z * z) / (64 * 64);
788#endif
789
790 if (v <= 0.9)
791 continue;
792
793 float r[4];
794 int i[4];
795
796 r[0] = x;
797 r[1] = y;
798 r[2] = z;
799 r[3] = v;
800
801 memcpy (i, r, 16);
802
803 i[0] = htonl (i[0]);
804 i[1] = htonl (i[1]);
805 i[2] = htonl (i[2]);
806 i[3] = htonl (i[3]);
807
808 fwrite (i, 4*4, 1, stdout);
809 }
810 }
811#endif
812
813 exit (0);
814}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines