Advertisement
xeromino

land

Jul 4th, 2015
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. // (slightly adapted by Jerome Herr)
  2. // The Nature of Code
  3. // Daniel Shiffman
  4. // http://natureofcode.com
  5.  
  6. // Landscape with height values according to Perlin noise
  7.  
  8. Landscape land;    
  9. float theta = 0.0;
  10. int frames = 500;
  11.  
  12. void setup() {
  13.  
  14.   size(540,540,P3D);
  15.  
  16.   // Create a landscape object
  17.   land = new Landscape(10,800,800);
  18. }
  19.  
  20. void draw() {
  21.  
  22.   // Ok, visualize the landscape space
  23.   background(255);
  24.   pushMatrix();
  25.   translate(width/2,height/2,-80);
  26.   rotateX(PI/3);
  27.   rotateZ(theta);
  28.   land.render();
  29.   popMatrix();
  30.  
  31.   land.calculate();
  32.  
  33.   theta += TWO_PI/frames;
  34.   if (frameCount<=45) saveFrame("image-###.tif");
  35. }
  36.  
  37. // The Nature of Code
  38. // Daniel Shiffman
  39. // http://natureofcode.com
  40.  
  41. // "Landscape" example
  42.  
  43. class Landscape {
  44.  
  45.   int scl;           // size of each cell
  46.   int w, h;           // width and height of thingie
  47.   int rows, cols;    // number of rows and columns
  48.   float zoff = random(10000);  // perlin noise argument
  49.   float[][] z;       // using an array to store all the height values
  50.  
  51.   Landscape(int scl_, int w_, int h_) {
  52.     scl = scl_;
  53.     w = w_;
  54.     h = h_;
  55.     cols = w/scl;
  56.     rows = h/scl;
  57.     z = new float[cols][rows];
  58.   }
  59.  
  60.  
  61.   // Calculate height values (based off a neural netork)
  62.   void calculate() {
  63.     float xoff = 0;
  64.     for (int i = 0; i < cols; i++)
  65.     {
  66.       float yoff = 0;
  67.       for (int j = 0; j < rows; j++)
  68.       {
  69.         z[i][j] = map(noise(xoff, yoff,zoff), 0, 1, -120, 120);
  70.         yoff += 0.05;
  71.       }
  72.       xoff += 0.05;
  73.     }
  74.     zoff+=0.005;
  75.   }
  76.  
  77.   // Render landscape as grid of quads
  78.   void render() {
  79.     // Every cell is an individual quad
  80.     // (could use quad_strip here, but produces funny results, investigate this)
  81.     for (int x = 0; x < z.length-1; x++)
  82.     {
  83.       for (int y = 0; y < z[x].length-1; y++)
  84.       {
  85.         // one quad at a time
  86.         // each quad's color is determined by the height value at each vertex
  87.         // (clean this part up)
  88.         stroke(0,100);
  89.         int f = (int) map(z[x][y],-120,120,0,255);
  90.         fill(f);
  91.         pushMatrix();
  92.         beginShape(QUADS);
  93.         translate(x*scl-w/2, y*scl-h/2, 0);
  94.         vertex(0, 0, z[x][y]);
  95.         vertex(scl, 0, z[x+1][y]);
  96.         vertex(scl, scl, z[x+1][y+1]);
  97.         vertex(0, scl, z[x][y+1]);
  98.         endShape();
  99.         popMatrix();
  100.       }
  101.     }
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement