Advertisement
Guest User

Rewrite

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