-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSketchPerlinNoiseWaterSurface.java
executable file
·78 lines (65 loc) · 2.5 KB
/
SketchPerlinNoiseWaterSurface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package de.hfkbremen.algorithmiccliches.examples;
import de.hfkbremen.algorithmiccliches.util.ACArcBall;
import processing.core.PApplet;
public class SketchPerlinNoiseWaterSurface extends PApplet {
private final int mGridScale = 16;
private float[][] mWaterSurface;
private float mWaveOffset;
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
new ACArcBall(this);
final int GRID_WIDTH = width / mGridScale;
final int GRID_HEIGHT = height / mGridScale;
mWaterSurface = new float[GRID_WIDTH][GRID_HEIGHT];
}
public void draw() {
final float mFrameRate = 1.0f / frameRate;
mWaveOffset += mFrameRate;
for (int x = 0; x < mWaterSurface.length; x++) {
for (int y = 0; y < mWaterSurface[x].length; y++) {
final float mWaveScale = 5.0f;
final float mNormalizedX = (float) x / (float) mWaterSurface.length * mWaveScale;
final float mNormalizedY = (float) y / (float) mWaterSurface[x].length * mWaveScale;
final float mWaveOffsetScale = 0.5f;
final float mX = mNormalizedX + mWaveOffset * mWaveOffsetScale;
final float mY = mNormalizedY;
final float mHeightScale = 100.0f;
mWaterSurface[x][y] = noise(mX, mY) * mHeightScale;
}
}
/* draw */
background(255);
directionalLight(126, 126, 126, 0, 0, -1);
ambientLight(102, 102, 102);
pushMatrix();
translate(0, 0, -height / 2.0f);
noStroke();
fill(255, 127, 0);
rect(0, 0, width, height);
stroke(15, 143, 255);
fill(0, 127, 255);
pushMatrix();
scale(mGridScale, mGridScale, 1);
strokeWeight(1.0f / mGridScale);
for (int x = 0; x < mWaterSurface.length - 1; x++) {
for (int y = 0; y < mWaterSurface[x].length - 1; y++) {
pushMatrix();
translate(x, y);
beginShape(QUADS);
vertex(0, 0, mWaterSurface[x][y]);
vertex(1, 0, mWaterSurface[x + 1][y]);
vertex(1, 1, mWaterSurface[x + 1][y + 1]);
vertex(0, 1, mWaterSurface[x][y + 1]);
endShape();
popMatrix();
}
}
popMatrix();
popMatrix();
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchPerlinNoiseWaterSurface.class.getName()});
}
}