vivienneanthony

Olsen Noise Algorithm C++ WIP .010

Feb 18th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.83 KB | None | 0 0
  1. /*
  2.  * @author Tat
  3.  * c++ rewrite vivienne (WIP)
  4.  * verion .01
  5.  */
  6.  
  7. #include <iostream>
  8. #include <vector>
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <png++/png.hpp>
  13.  
  14. using namespace std;
  15.  
  16. void SaveTerrFile(const int * image, int size, char * filename);
  17.  
  18.  
  19. class OlsenNoise2D
  20. {
  21.  
  22. public:
  23.     int * olsennoise(int x, int y, int width, int height);
  24.  
  25. private:
  26.     int hashrandom(std::vector<long long int> elements);
  27.     long long hash(long long v);
  28.  
  29. };
  30.  
  31. int * OlsenNoise2D::olsennoise(int x, int y, int width, int height)
  32. {
  33.     int maxiterations =6;
  34.     int cx, cy;
  35.     int cxh, cyh;
  36.     int cwidth, cheight;
  37.     int xoff, yoff;
  38.     int nwidth, nheight;
  39.     int nx, ny;
  40.     int nxh, nyh;
  41.     int m=0;
  42.     int n=0;
  43.     int * field = NULL;
  44.  
  45.     for (int iteration = 0; iteration < maxiterations; iteration++)
  46.     {
  47.         nx = x;
  48.         ny = y;
  49.  
  50.         nxh = x + width;
  51.         nyh = y + width;
  52.  
  53.         n = maxiterations - iteration;
  54.  
  55.         for (int i = 1; i < n; i++)
  56.         {
  57.             nx = (nx / 2) - 1;
  58.             ny = (ny / 2) - 1;
  59.             nxh = 1 -(-nxh/2);
  60.             nyh = 1 -(-nyh/2);
  61.         }
  62.  
  63.         xoff = -2*((nx/2)) + nx + 1;
  64.         yoff = -2*((ny/2)) + ny + 1;
  65.  
  66.         cx = (nx / 2) - 1;
  67.         cy = (ny / 2) - 1;
  68.         cxh = 1 -(-nxh/2);
  69.         cyh = 1 -(-nyh/2);
  70.  
  71.         nwidth = nxh - nx;
  72.         nheight = nyh - ny;
  73.  
  74.         cwidth = cxh - cx;
  75.         cheight = cyh - cy;
  76.  
  77.         int fieldwidth=0;
  78.         int fieldheight=0;
  79.  
  80.         /// Only happens once
  81.         if (field==NULL)
  82.         {
  83.             /// allocate memory
  84.             field = new int[height * width];
  85.  
  86.             /// blank value
  87.             for (int x = 0; x < width; x++)
  88.             {
  89.                 for (int y = 0; y < height; y++)
  90.                 {
  91.                     field[x+(y*width)]=0;
  92.                 }
  93.             }
  94.         }
  95.  
  96.         /// rest
  97.         fieldwidth=cwidth;
  98.         fieldheight=cheight;
  99.  
  100.         /// First loop
  101.         for (int j = 0, m=cwidth; j < m; j++)
  102.         {
  103.             for (int k = 0, n=cheight; k < n; k++)
  104.             {
  105.                 field[j+(k*m)] += (hashrandom( {cx + j, ((cy + k)*fieldwidth), iteration}) & (1 << (7 - iteration)));
  106.             }
  107.         }
  108.  
  109.         /// Up sampled
  110.         int * upsampled = new int[(fieldwidth*2)*(fieldheight*2)];
  111.         long int upsampledsize=(fieldwidth*2)*(fieldheight*2);
  112.  
  113.         for (int j = 0, m=fieldwidth*2; j < m; j++)
  114.         {
  115.             for (int k = 0,n=fieldheight*2; k < n; k++)
  116.             {
  117.                 upsampled[j+(k*m)] = field[(j / 2)+((k / 2)*fieldwidth)];
  118.             }
  119.         }
  120.  
  121.         memcpy((void *)field,(void *) upsampled,upsampledsize*sizeof(int));
  122.         delete upsampled;
  123.  
  124.         /// rest
  125.         fieldwidth=fieldwidth*2;
  126.         fieldheight=fieldheight*2;
  127.  
  128.         /// Blur field
  129.         int * blurfield =new int[(fieldwidth-2)*(fieldheight-2)];
  130.         long int blurfieldsize = (fieldwidth-2)*(fieldheight-2);
  131.  
  132.         for (int j = 0,m=fieldwidth-2; j < m; j++)
  133.         {
  134.             for (int k = 0, n=fieldheight-2;  k < n; k++)
  135.             {
  136.                 for (int h = 0; h < 9; h++)
  137.                 {
  138.  
  139.                     blurfield[j+(k*m)] += field[(j + (h % 3))+((k+(h/ 3))*fieldwidth)];
  140.                 }
  141.                 blurfield[j+(k*m)] /= 9;
  142.             }
  143.         }
  144.  
  145.         memcpy((void *)field,(void *)blurfield,blurfieldsize*sizeof(int));
  146.         delete blurfield;
  147.  
  148.         /// rest
  149.         fieldwidth=fieldwidth-2;
  150.         fieldheight=fieldheight-2;
  151.  
  152.         /// Trim field
  153.         int * trimfield = new int[nwidth*nheight];
  154.         long int trimfieldsize = nwidth*nheight;
  155.  
  156.         for (int j = 0, m=nwidth; j < m; j++)
  157.         {
  158.             for (int k = 0, n=nheight; k < n; k++)
  159.             {
  160.                 trimfield[j+(k*m)] = field[(j + xoff)+((k + yoff)*fieldwidth)];
  161.  
  162.             }
  163.         }
  164.         memcpy((void *)field,(void *)trimfield,trimfieldsize*sizeof(int));
  165.         delete trimfield;
  166.  
  167.     }
  168.  
  169.     SaveTerrFile(field, width, "rgbOlsena.png");
  170.  
  171.     return field;
  172. }
  173.  
  174. int OlsenNoise2D::hashrandom(std::vector<long long int> elements)
  175. {
  176.     long long hashcalc = 0;
  177.  
  178.  
  179.     for (int i = 0; i < elements.size(); i++)
  180.     {
  181.         hashcalc ^= elements[i];
  182.         hashcalc = hash(hashcalc);
  183.     }
  184.     return (int) hashcalc;
  185. };
  186.  
  187. long long OlsenNoise2D::hash(long long v)
  188. {
  189.     long long hash = v;
  190.     long long h = hash;
  191.  
  192.     switch ((int) hash & 3)
  193.     {
  194.     case 3:
  195.         hash += h;
  196.         hash ^= hash << 32;
  197.         hash ^= h << 36;
  198.         hash += hash >> 22;
  199.         break;
  200.     case 2:
  201.         hash += h;
  202.         hash ^= hash << 22;
  203.         hash += hash >> 34;
  204.         break;
  205.     case 1:
  206.         hash += h;
  207.         hash ^= hash << 20;
  208.         hash += hash >> 2;
  209.     }
  210.     hash ^= hash << 6;
  211.     hash += hash >> 10;
  212.     hash ^= hash << 8;
  213.     hash += hash >> 34;
  214.     hash ^= hash << 50;
  215.     hash += hash >> 12;
  216.     return hash;
  217. };
  218.  
  219.  
  220. int main()
  221. {
  222.     /// Test
  223.     int ImageSize=2048;
  224.  
  225.     int * imageInput = new int[ImageSize*ImageSize];
  226.  
  227.     /// Image
  228.     OlsenNoise2D testingolsen;
  229.     imageInput=testingolsen.olsennoise(0,0,ImageSize,ImageSize);
  230.  
  231.     // SaveTerrFile(imageInput, ImageSize, "rgbOlsen.png");
  232.  
  233.     delete imageInput;
  234.  
  235.     return 1;
  236. }
  237.  
  238.  
  239. void SaveTerrFile(const int * image, int size, char * filename)
  240. {
  241.     png::image< png::rgb_pixel > newimage(size, size);
  242.  
  243.     for (unsigned int y = 0; y < newimage.get_width(); ++y)
  244.     {
  245.         for (unsigned int x = 0; x < newimage.get_height(); ++x)
  246.         {
  247.             int col = int(image[x+(y*newimage.get_width())]);
  248.             newimage[y][x] = png::rgb_pixel(col,col,col);
  249.             // non-checking equivalent of image.set_pixel(x, y, ...);
  250.         }
  251.     }
  252.  
  253.     newimage.write(filename);
  254. }
Advertisement
Add Comment
Please, Sign In to add comment