-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.cpp
297 lines (241 loc) · 7.33 KB
/
backup.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "ofApp.h"
#include <math.h>
#include <iostream>
#include <algorithm>
#include <tgmath.h>
using std::cout;
using std::endl;
float lerp(float x1, float x2, float w)
{
return x1 + w * (x2 - x1);
}
//--------------------------------------------------------------
void ofApp::setup(){
counter = 0;
for(int i = 0; i < SIZE; i++)
{
heights_rects[i] = ofRectangle(0, 0, 0, 0);
water_rects[i] = ofRectangle(0, 0, 0, 0);
sed_rects[i] = ofRectangle(0, 0, 0, 0);
heights[i] = 8 + 4 * sin((float)i/6.0);
//heights[i] = 12;
sed[i] = 0;
}
}
//--------------------------------------------------------------
void ofApp::update(){
for(int i = 0; i < SIZE; i++)
{
heights_rects[i].setY(window_height);
heights_rects[i].setX(i * WIDTH);
heights_rects[i].setHeight(-WIDTH * heights[i]);
heights_rects[i].setWidth(WIDTH);
sed_rects[i].setY(heights_rects[i].getMinY());
sed_rects[i].setX(heights_rects[i].getMinX());
sed_rects[i].setHeight(-WIDTH * sed[i]);
sed_rects[i].setWidth(WIDTH);
water_rects[i].setY(sed_rects[i].getMinY());
water_rects[i].setX(sed_rects[i].getMinX());
water_rects[i].setHeight(-WIDTH * water[i]);
water_rects[i].setWidth(WIDTH);
}
//We use the model described in
//www-ljk.imag.fr/Publications/Basilic/com.lmc.publi.PUBLI_Inproceedings@117681e94b6_fff75c/FastErosion_PG07.pdf
float gravity = 1.0;
float distance = 1.0;
float time_step = 0.1;
float sediment_capacity_constant = 0.1;
float dissolving_constant = 1;
float sedimentation_constant = 1;
float evaporation_constant = 0.0001;
// rain
for(int i = 0; i < SIZE; i++)
{
//water[i] += 0.05;
}
//water[(int)ofRandom(SIZE)] = 0.1;
// water movement
for(int i = 0; i < SIZE; i++)
{
float left_height_diff = (heights[i] + water[i]) - (heights[i-1] + water[i-1]);
pipe_flux_left[i] = max(0.0f, pipe_flux_left[i] + time_step * gravity * left_height_diff);
float right_height_diff = (heights[i] + water[i]) - (heights[i+1] + water[i+1]);
pipe_flux_right[i] = max(0.0f, pipe_flux_right[i] + time_step * gravity * right_height_diff);
//don't output water to the left if on the left edge or to the right if
//on the right edge
if(i == 0)
{
pipe_flux_left[i] = 0;
}
if(i == SIZE - 1)
{
pipe_flux_right[i] = 0;
}
float scaling_factor = min(1.0f, (water[i] * distance) / (pipe_flux_left[i] + pipe_flux_right[i]));
pipe_flux_left[i] *= scaling_factor;
pipe_flux_right[i] *= scaling_factor;
}
for(int i = 0; i < SIZE; i++)
{
float flux_in;
//only take water input from the left if on the right edge and right if
//on the left edge
if(i == 0)
{
flux_in = pipe_flux_left[i+1];
}
else if(i == SIZE - 1)
{
flux_in = pipe_flux_right[i-1];
}
else
{
flux_in = pipe_flux_right[i-1] + pipe_flux_left[i+1];
}
float flux_out = pipe_flux_right[i] + pipe_flux_left[i];
float water_area_diff = time_step * (flux_in - flux_out);
float prev_water = water[i];
water[i] += water_area_diff / distance;
float passed_water = (pipe_flux_right[i-1] - pipe_flux_left[i] + pipe_flux_right[i] - pipe_flux_left[i+1]) / 2;
float average_water = (prev_water + water[i]) / 2;
if(passed_water == 0)
{
vel[i] = 0;
}
else
{
vel[i] = passed_water / (distance * average_water);
}
}
// sediment dissolution
for(int i = 0; i < SIZE; i++)
{
float local_angle;
if(vel[i] > 0)
{
local_angle = atan((heights[i] - heights[i+1]) / distance);
if(i == SIZE - 1)
{
local_angle = atan((heights[i-1] - heights[i]) / distance);
}
}
else
{
local_angle = atan((heights[i] - heights[i-1]) / distance);
if(i == 0)
{
local_angle = atan((heights[i+1] - heights[i]) / distance);
}
}
float sediment_capacity = sediment_capacity_constant * sin(local_angle) * abs(vel[i]);
cout << sediment_capacity << endl;
cout << sed[i] << endl;
cout << sin(local_angle) << " " << abs(vel[i]) << endl;
if(sediment_capacity > sed[i])
{
float sediment_exchange = dissolving_constant * (sediment_capacity - sed[i]);
cout << "eroding: " << sediment_exchange << endl;
heights[i] -= sediment_exchange;
sed[i] += sediment_exchange;
}
else if(sediment_capacity < sed[i])
{
float sediment_exchange = sedimentation_constant * (sed[i] - sediment_capacity);
cout << "depositing: " << sediment_exchange << endl;
heights[i] += sediment_exchange;
sed[i] -= sediment_exchange;
}
else
{
cout << "What?" << endl;
}
}
// sediment movement
/*
float tmp_sed[SIZE];
for(int i = 0; i < SIZE; i++)
{
float previous_position = (float)i - (vel[i] * time_step);
int previous_index = (int)previous_position;
int left_index = previous_index;
int right_index = previous_index + 1;
float interp = previous_position - (float)left_index;
//cout << "Moving from " << (int)(i - vel[i] * time_step) << " to " << i << " at " << vel[i] << endl;
if(left_index < 0)
{
tmp_sed[i] = sed[0];
}
else if(right_index >= SIZE)
{
tmp_sed[i] = sed[SIZE-1];
}
else
{
tmp_sed[i] = lerp(sed[left_index], sed[right_index], interp);
}
}
for(int i = 0; i < SIZE; i++)
{
sed[i] = tmp_sed[i];
}
*/
// evaporation
for(int i = 0; i < SIZE; i++)
{
water[i] = water[i] * (1 - evaporation_constant * time_step);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
for(int i = 0; i < SIZE; i++)
{
ofSetColor(0,0,0);
ofDrawRectangle(heights_rects[i]);
ofSetColor(0,0,255);
ofDrawRectangle(water_rects[i]);
ofSetColor(127,127,127);
ofDrawRectangle(sed_rects[i]);
}
for(int i = 0; i < SIZE; i++)
{
ofSetColor(255, 0, 0);
ofDrawLine(water_rects[i].getCenter().x, water_rects[i].getCenter().y, water_rects[i].getCenter().x + vel[i] * WIDTH, water_rects[i].getCenter().y);
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
water[(int)(x / WIDTH)] += 10;
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
window_height = h;
window_width = w;
WIDTH = window_width / SIZE;
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}