Advertisement
Willium_Bob_Cole

Perlin Noise Stand Alone Test

Dec 14th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. double Noise(int x, int y);
  8. double SmoothNoise_1(float x, float y);
  9. double Cosine_Interpolate(float a, float b, float x);
  10. double InterpolatedNoise_1(float x, float y);
  11. double PerlinNoise_2D(float persistence, int Number_Of_Octaves, float x, float y);
  12.  
  13. const int MAPWIDTH = 4;
  14. const int MAPHEIGHT = 16;
  15.  
  16. int SEED;
  17.  
  18. double mapHeight[MAPWIDTH][MAPHEIGHT];
  19.  
  20. int main()
  21. {
  22.     srand(time(NULL));
  23.  
  24.     SEED = rand();
  25.  
  26.     for(int y = 0; y < MAPHEIGHT; y++)
  27.     {
  28.         for(int x = 0; x < MAPWIDTH; x++)
  29.         {
  30.             mapHeight[x][y] = PerlinNoise_2D(0.5, 6, SEED + x, SEED + y);
  31.             //mapHeight[x][y] = InterpolatedNoise_1(SEED + x, SEED + y);
  32.  
  33.         }
  34.     }
  35.  
  36.     for(int y = 0; y < MAPHEIGHT; y++)
  37.     {
  38.         for(int x = 0; x < MAPWIDTH; x++)
  39.         {
  40.             cout << mapHeight[x][y] << "\t";
  41.         }
  42.         cout << endl;
  43.     }
  44.  
  45.     cin.get();
  46.  
  47.     return 0;
  48. }
  49.  
  50.  
  51. double Noise_1(int x, int y)
  52. {
  53.     int n = x + y * 57;
  54.     n = (n << 13) ^ n;
  55.     return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
  56. }
  57. /*
  58. double Noise_2(int x, int y)
  59. {
  60.     int n = x + y * 57;
  61.     n = (n << 13) ^ n;
  62.     return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
  63. }
  64.  
  65. double Noise_3(int x, int y)
  66. {
  67.     int n = x + y * 57;
  68.     n = (n << 13) ^ n;
  69.     return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
  70. }
  71.  
  72. double Noise_4(int x, int y)
  73. {
  74.     int n = x + y * 57;
  75.     n = (n << 13) ^ n;
  76.     return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
  77. }
  78. */
  79.  
  80. double SmoothNoise_1(float x, float y)
  81. {
  82.     float corners = (Noise_1(x - 1, y - 1) + Noise_1(x + 1, y - 1) + Noise_1(x - 1, y + 1) + Noise_1(x + 1, y + 1)) / 16;
  83.     float sides = (Noise_1(x - 1, y) + Noise_1(x + 1, y) + Noise_1(x, y - 1) + Noise_1(x, y + 1)) / 8;
  84.     float center = Noise_1(x, y) / 4;
  85.     return corners + sides + center;
  86. }
  87. /*
  88. double SmoothNoise_2(float x, float y)
  89. {
  90.     float corners = (Noise_2(x - 1, y - 1) + Noise_2(x + 1, y - 1) + Noise_2(x - 1, y + 1) + Noise_2(x + 1, y + 1)) / 16;
  91.     float sides = (Noise_2(x - 1, y) + Noise_2(x + 1, y) + Noise_2(x, y - 1) + Noise_2(x, y + 1)) / 8;
  92.     float center = Noise_2(x, y) / 4;
  93.     return corners + sides + center;
  94. }
  95.  
  96. double SmoothNoise_3(float x, float y)
  97. {
  98.     float corners = (Noise_3(x - 1, y - 1) + Noise_3(x + 1, y - 1) + Noise_3(x - 1, y + 1) + Noise_3(x + 1, y + 1)) / 16;
  99.     float sides = (Noise_3(x - 1, y) + Noise_3(x + 1, y) + Noise_3(x, y - 1) + Noise_3(x, y + 1)) / 8;
  100.     float center = Noise_3(x, y) / 4;
  101.     return corners + sides + center;
  102. }
  103.  
  104. double SmoothNoise_4(float x, float y)
  105. {
  106.     float corners = (Noise_4(x - 1, y - 1) + Noise_4(x + 1, y - 1) + Noise_4(x - 1, y + 1) + Noise_4(x + 1, y + 1)) / 16;
  107.     float sides = (Noise_4(x - 1, y) + Noise_4(x + 1, y) + Noise_4(x, y - 1) + Noise_4(x, y + 1)) / 8;
  108.     float center = Noise_4(x, y) / 4;
  109.     return corners + sides + center;
  110. }
  111. */
  112.  
  113. double Cosine_Interpolate(float a, float b, float x)
  114. {
  115.     float ft = x * 3.1415927;
  116.     float f = (1 - cos(ft)) * .5;
  117.  
  118.     return  a*(1 - f) + b*f;
  119. }
  120.  
  121.  
  122. double InterpolatedNoise_1(float x, float y)
  123. {
  124.  
  125.     int integer_X = int(x);
  126.     float fractional_X = x - integer_X;
  127.  
  128.     int integer_Y = int(y);
  129.     float fractional_Y = y - integer_Y;
  130.  
  131.     double v1 = SmoothNoise_1(integer_X, integer_Y);
  132.     double v2 = SmoothNoise_1(integer_X + 1, integer_Y);
  133.     double v3 = SmoothNoise_1(integer_X, integer_Y + 1);
  134.     double v4 = SmoothNoise_1(integer_X + 1, integer_Y + 1);
  135.     /*
  136.     double v2 = SmoothNoise_2(integer_X + 1, integer_Y);
  137.     double v3 = SmoothNoise_3(integer_X, integer_Y + 1);
  138.     double v4 = SmoothNoise_4(integer_X + 1, integer_Y + 1);*/
  139.  
  140.     double i1 = Cosine_Interpolate(v1, v2, fractional_X);
  141.     double i2 = Cosine_Interpolate(v3, v4, fractional_X);
  142.  
  143.     return Cosine_Interpolate(i1, i2, fractional_Y);
  144. }
  145.  
  146.  
  147. double PerlinNoise_2D(float persistence, int Number_Of_Octaves, float x, float y)
  148. {
  149.  
  150.     float total = 0;
  151.     int p = persistence;
  152.     int n = Number_Of_Octaves - 1;
  153.  
  154.     for (int i = 0; i < n; i++)
  155.     {
  156.  
  157.         float frequency = 2^i;
  158.         float amplitude = p^i;
  159.  
  160.         total = total + InterpolatedNoise_1(x * frequency, y * frequency) * amplitude;
  161.     }
  162.  
  163.     return total;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement