Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (slightly adapted by Jerome Herr)
- // The Nature of Code
- // Daniel Shiffman
- // http://natureofcode.com
- // Landscape with height values according to Perlin noise
- Landscape land;
- float theta = 0.0;
- int frames = 500;
- void setup() {
- size(540,540,P3D);
- // Create a landscape object
- land = new Landscape(10,800,800);
- }
- void draw() {
- // Ok, visualize the landscape space
- background(255);
- pushMatrix();
- translate(width/2,height/2,-80);
- rotateX(PI/3);
- rotateZ(theta);
- land.render();
- popMatrix();
- land.calculate();
- theta += TWO_PI/frames;
- if (frameCount<=45) saveFrame("image-###.tif");
- }
- // The Nature of Code
- // Daniel Shiffman
- // http://natureofcode.com
- // "Landscape" example
- class Landscape {
- int scl; // size of each cell
- int w, h; // width and height of thingie
- int rows, cols; // number of rows and columns
- float zoff = random(10000); // perlin noise argument
- float[][] z; // using an array to store all the height values
- Landscape(int scl_, int w_, int h_) {
- scl = scl_;
- w = w_;
- h = h_;
- cols = w/scl;
- rows = h/scl;
- z = new float[cols][rows];
- }
- // Calculate height values (based off a neural netork)
- void calculate() {
- float xoff = 0;
- for (int i = 0; i < cols; i++)
- {
- float yoff = 0;
- for (int j = 0; j < rows; j++)
- {
- z[i][j] = map(noise(xoff, yoff,zoff), 0, 1, -120, 120);
- yoff += 0.05;
- }
- xoff += 0.05;
- }
- zoff+=0.005;
- }
- // Render landscape as grid of quads
- void render() {
- // Every cell is an individual quad
- // (could use quad_strip here, but produces funny results, investigate this)
- for (int x = 0; x < z.length-1; x++)
- {
- for (int y = 0; y < z[x].length-1; y++)
- {
- // one quad at a time
- // each quad's color is determined by the height value at each vertex
- // (clean this part up)
- stroke(0,100);
- int f = (int) map(z[x][y],-120,120,0,255);
- fill(f);
- pushMatrix();
- beginShape(QUADS);
- translate(x*scl-w/2, y*scl-h/2, 0);
- vertex(0, 0, z[x][y]);
- vertex(scl, 0, z[x+1][y]);
- vertex(scl, scl, z[x+1][y+1]);
- vertex(0, scl, z[x][y+1]);
- endShape();
- popMatrix();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement