java - Issues while generating Perlin Noise -


i've created function generate 2-dimension array of perlin noise data, starting base noise array of random values between 0 , 1.

the base white noise array looks this:random values 0-1 in 2d array

from there, use smoothnoise() function interpolate values create looking image (which don't have because doesn't work)

the smooth noise function called once every octave. images of data smooth noise function returns follows (octaves lowest highest)

octave 0 octave 1 octave 2 octave 3 octave 4 octave 5

after smoothing done image outputted turns black. upload image black there no need.

my code is:

    import java.util.random;   public class noise2d {      int width;     int height;     public noise2d(int width, int height){         this.width = width;         this.height = height;     }      public double[][] generatewhitenoise(int width,int height){         random r = new random(0);         double[][] whitenoise = new double[width][height];          for(int = 0; i<whitenoise.length; i++){             for(int j = 0; j<whitenoise[0].length;j++){                 double rnum =   r.nextdouble()%1;                 whitenoise[i][j] = (double)rnum;             }         }         imagewriter.writeimage(whitenoise, "whitenoise");         return whitenoise;     }      public double interpolate(double x0, double x1, double alpha){         return x0 * (1 - alpha) + alpha * x1;     }      public double[][] generatesmoothnoise(double[][] basenoise, int octave){         int width = basenoise.length;         int height = basenoise[0].length;          double[][] smoothnoise = new double[width][height];          int period = 1<< octave; //2^i         double frequency = 1.0/period;          for(int x = 0; x<width; x++){             int x0 = (x/period)*period; //7/3 = 2 *3 = 6             int x1 = (x0+period)%width;              double hblend = (x-x0)*frequency;              for(int y = 0; y<height; y++){                  int y0 = (y/period)*period;                 int y1 = (y0 + period)%height;                  double vblend = (y - y0)*frequency;                  double top = interpolate(basenoise[x0][y0],basenoise[x1][y1],hblend);                 double bottom = interpolate(basenoise[x0][y1],basenoise[x1][y0],hblend);                  smoothnoise[x][y] = interpolate(top,bottom,vblend);             }         }         imagewriter.writeimage(smoothnoise,"smooth"+integer.tostring(octave));         return smoothnoise;     }      public double[][] generateperlinnoise(double basenoise[][], int octaves){         int width = basenoise.length;         int height = basenoise[0].length;          double persistence = 0.5;          double[][][] smoothnoise = new double[octaves][][];          for(int = 0; i<octaves; i++){             smoothnoise[i] = generatesmoothnoise(basenoise,i);         }          double[][] perlinnoise = new double[width][height];         double amplitude = 1;         double totalamplitude = 0;          for(int octave = octaves-1; octave>=0; octave--){             amplitude*=persistence;             totalamplitude+=amplitude;             for(int x = 0; x<width;x++){                 for(int y = 0; y<height; y++){                     perlinnoise[x][y] = smoothnoise[octave][x][y] * amplitude;                 }             }         }         imagewriter.writeimage(perlinnoise,"files");         for(int = 0; i<width; i++){             for(int j = 0; j<height; j++){                 perlinnoise[i][j] /= totalamplitude;             }         }         return perlinnoise;     }  } 

the class defined , methods called by:

perlin p = new perlin(256,256); writeimage(p.smoothnoise(p.makenoise(256,256), 1, 16), "perlin"); 

1 frequency, 16 octave count data written in function writeimage(2d array, "name") using following code (i don't think there problem part i'll post anyway):

public static void writeimage(double[][] data,string name){         bufferedimage img = new bufferedimage(data.length,data[0].length,bufferedimage.type_int_rgb);          for(int y = 0; y<data.length; y++){             for(int x = 0; x<data[y].length; x++){                 if(data[y][x]>1){                     data[y][x] = 1;                 }                 if(data[y][x]<0){                     data[y][x] = 0;                 }                  color c = new color((float)data[y][x],(float)data[y][x],(float)data[y][x]);                 img.setrgb(x,y,c.getrgb());             }         }          try{             file file = new file(name+".png");             file.createnewfile();              imageio.write(img,"png",file);         }catch(ioexception e){             e.printstacktrace();         }     }      public static void writeexistingimage(bufferedimage img){         try{             file file = new file("noise2.png");             file.createnewfile();              imageio.write(img,"png",file);         }catch(ioexception e){             e.printstacktrace();         }     } 

to conclude, believe problem lies within smoothnoise method, although quite possibly wrong because knowledge of perlin noise not extensive. if problem not lie here, assume lies in generateperlinnoise() method. appreciated. happy suggestion, have been trying solve problem extremely long time.

to clarify: problem generateperlinnoise method returns set of data(2d) array makes black image (rather cool noise image) think smooth noise images not supposed split squares


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -