-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeightMap.pde
142 lines (122 loc) · 3.89 KB
/
HeightMap.pde
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class HeightMap
{
private final int mapSize;
private final float cellSize;
private float initPos[][][];
private float pos[][][];
private float tex[][][];
protected ArrayList<Wave> waves;
private Wave waveArray[];
public HeightMap(int mapSize, float cellSize)
{
this.mapSize = mapSize;
this.cellSize = cellSize;
initGridPositions();
initTextValues();
waves = new ArrayList<Wave>();
waveArray = new Wave[0];
}
private void initGridPositions()
{
float startPos = -mapSize*cellSize/2f;
pos = new float[mapSize][mapSize][3];
initPos = new float[mapSize][mapSize][3];
for(int i = 0; i < mapSize; i++)
{
for(int j = 0; j < mapSize; j++)
{
pos[i][j][0] = startPos + j * cellSize; // coordenada X
pos[i][j][1] = 0; // coordenada Y
pos[i][j][2] = startPos + i * cellSize; // coordenada Z
initPos[i][j][0] = pos[i][j][0];
initPos[i][j][1] = pos[i][j][1];
initPos[i][j][2] = pos[i][j][2];
}
}
}
private void initTextValues()
{
tex = new float[mapSize][mapSize][2];
float mapSizeCasted = (float)mapSize;
int i, j;
for(i = 0; i < mapSize; i++)
{
for(j = 0; j < mapSize; j++)
{
tex[i][j][0] = j / mapSizeCasted * image.width;
tex[i][j][1] = i / mapSizeCasted * image.height;
}
}
}
public void update()
{
//Pass arraylist to array to iterate quicker
waveArray = waves.toArray(waveArray);
//Declarations
int i, j, k, len = waveArray.length;
PVector variation;
float time = millis()/1000f;
//Iterate over arrays
for(i = 0; i < mapSize; i++)
{
for(j = 0; j < mapSize; j++)
{
//Reset positions
pos[i][j][0] = initPos[i][j][0];
pos[i][j][1] = initPos[i][j][1];
pos[i][j][2] = initPos[i][j][2];
//Iterate through waves
for(k = 0; k < len; k++)
{
variation = waveArray[k].getVariation(initPos[i][j][0],initPos[i][j][1],initPos[i][j][2],time);
pos[i][j][0] += variation.x;
pos[i][j][1] += variation.y;
pos[i][j][2] += variation.z;
}
}
}
}
public void present()
{
noStroke();
fill(0xffffffff);
beginShape(TRIANGLES);
texture(image);
for(int i = 0; i < mapSize - 1; i++)
{
for(int j = 0; j < mapSize - 1; j++)
{
vertex(pos[i][j][0], pos[i][j][1], pos[i][j][2], tex[i][j][0],tex[i][j][1]);
vertex(pos[i+1][j][0], pos[i+1][j][1], pos[i+1][j][2], tex[i+1][j][0],tex[i+1][j][1]);
vertex(pos[i+1][j+1][0], pos[i+1][j+1][1], pos[i+1][j+1][2], tex[i+1][j+1][0],tex[i+1][j+1][1]);
vertex(pos[i][j][0], pos[i][j][1], pos[i][j][2], tex[i][j][0],tex[i][j][1]);
vertex(pos[i+1][j+1][0], pos[i+1][j+1][1], pos[i+1][j+1][2],tex[i+1][j+1][0],tex[i+1][j+1][1]);
vertex(pos[i][j+1][0], pos[i][j+1][1], pos[i][j+1][2], tex[i][j+1][0],tex[i][j+1][1]);
}
}
endShape();
}
public void presentWired()
{
stroke(0xff000000);
noFill();
beginShape(TRIANGLES);
for(int i = 0; i < mapSize - 1; i++)
{
for(int j = 0; j < mapSize - 1; j++)
{
vertex(pos[i][j][0], pos[i][j][1], pos[i][j][2], tex[i][j][0],tex[i][j][1]);
vertex(pos[i+1][j][0], pos[i+1][j][1], pos[i+1][j][2], tex[i+1][j][0],tex[i+1][j][1]);
vertex(pos[i+1][j+1][0], pos[i+1][j+1][1], pos[i+1][j+1][2], tex[i+1][j+1][0],tex[i+1][j+1][1]);
vertex(pos[i][j][0], pos[i][j][1], pos[i][j][2], tex[i][j][0],tex[i][j][1]);
vertex(pos[i+1][j+1][0], pos[i+1][j+1][1], pos[i+1][j+1][2], tex[i+1][j+1][0],tex[i+1][j+1][1]);
vertex(pos[i][j+1][0], pos[i][j+1][1], pos[i][j+1][2], tex[i][j+1][0],tex[i][j+1][1]);
}
}
endShape();
}
public void addWave(Wave wave)
{
waves.add(wave);
}
}