-
Notifications
You must be signed in to change notification settings - Fork 96
/
Water.cpp
executable file
·1769 lines (1487 loc) · 64.6 KB
/
Water.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 3D World - OpenGL CS184 Computer Graphics Project
// by Frank Gennari and Hiral Patel
// 3/15/02
#include "mesh.h"
#include "spillover.h"
#include "physics_objects.h"
#include "openal_wrap.h"
#include "shaders.h"
#include "gl_ext_arb.h"
float const RIPPLE_DAMP1 = 0.95;
float const RIPPLE_DAMP2 = 0.02;
float const RIPPLE_MAT_ATTEN = 0.965;
float const MAX_RIPPLE_HEIGHT = 1.0;
float const MAX_SPLASH_SIZE = 80.0;
float const SMALL_DZ = 0.001;
float const WATER_WIND_EFF2 = 0.0005;
float const SNOW_THRESHOLD = 8.0;
float const MELT_RATE = 10.0;
float const SHADE_MELT = 0.5;
float const NIGHT_MELT = 0.3;
float const RAIN_VOLUME = 0.05;
float const BLOOD_VOLUME = 0.01;
float const FLOW_HEIGHT0 = 0.001;
float const G_W_STOP_DEPTH = 0.0;
float const G_W_START_DEPTH = 0.1;
float const SQUARE_S_AREA = 1.0;
float const INIT_AREA = 10.0;
float const EVAPORATION = 1.0E-5; // small for now
float const EVAP_TEMP_POINT = 0.0;
float const W_TEX_STRETCH = 2.0;
float const MAX_WATER_ACC = 25.0;
float const DARK_WATER_ATTEN = 1.0; // higher is lighter, lower is darker
float const INT_WATER_ATTEN = 0.5; // for interior water
unsigned const NUM_WATER_SPRINGS = 2;
unsigned const MAX_RIPPLE_STEPS = 2;
unsigned const UPDATE_STEP = 8; // update water only every nth ripple computation
int const EROSION_DIST = 4;
float const EROSION_RATE = 0.0; //0.01
bool const DEBUG_WATER_TIME = 0; // DEBUGGING
bool const DEBUG_RIPPLE_TIME = 0;
bool const NO_ICE_RIPPLES = 0;
bool const USE_SEA_FOAM = 1;
int const UPDATE_UW_LANDSCAPE = 2;
float const w_spec[2][2] = {{0.9, 80.0}, {0.5, 60.0}};
enum {SPILL_NONE, SPILL_OUTSIDE, SPILL_INSIDE};
struct water_spring { // size = 40;
int enabled;
float dpf, diff, acc;
point pos;
vector3d vel;
water_spring() : enabled(0), dpf(0.0f), diff(0.0f), acc(0.0f) {}
water_spring(int en, float dpf_, float diff_, float acc_, point const &pos_, vector3d const &vel_)
: enabled(en), dpf(dpf_), diff(diff_), acc(acc_), pos(pos_), vel(vel_) {}
};
struct water_section {
float zval, wvol;
int x1, y1, x2, y2;
water_section() : zval(0.0f), wvol(0.0f), x1(0), y1(0), x2(0), y2(0) {}
water_section(float x1_, float y1_, float x2_, float y2_, float zval_, float wvol_) : zval(zval_), wvol(wvol_),
x1(get_xpos_clamp(x1_)), y1(get_ypos_clamp(y1_)), x2(get_xpos_clamp(x2_)), y2(get_ypos_clamp(y2_))
{
if (x1 > x2) swap(x1, x2);
if (y1 > y2) swap(y1, y2);
}
bool is_nonzero() const {return (x1 < x2 && y1 < y2);}
};
// Global Variables
bool water_is_lava(0);
int total_watershed(0), has_accumulation(0), start_ripple(0), DISABLE_WATER(0), first_water_run(0), has_snow_accum(0), added_wsprings(0);
float max_water_height, min_water_height, def_water_level;
vector<valley> valleys;
vector<water_spring> water_springs;
vector<water_section> wsections;
spillover spill;
extern bool using_lightmap, has_snow, fast_water_reflect, enable_clip_plane_z, begin_motion;
extern int display_mode, frame_counter, TIMESCALE2, I_TIMESCALE2, world_mode, rand_gen_index, animate, animate2, blood_spilled;
extern int landscape_changed, xoff2, yoff2, scrolling, dx_scroll, dy_scroll, INIT_DISABLE_WATER;
extern float temperature, zmax, zmin, zbottom, ztop, light_factor, water_plane_z, fticks, mesh_scale, water_h_off_rel, clip_plane_z;
extern float TIMESTEP, TWO_XSS, TWO_YSS, XY_SCENE_SIZE;
extern vector3d up_norm, wind, total_wind;
extern int coll_id[];
extern obj_group obj_groups[];
extern coll_obj_group coll_objects;
extern water_params_t water_params;
void calc_water_normals();
void compute_ripples();
void update_valleys_and_draw_spillover();
void update_water_volumes();
void draw_spillover(vector<vert_norm_color> &verts, int i, int j, int si, int sj, int index, int vol_over, float blood_mix, float mud_mix);
int calc_rest_pos(vector<int> &path_x, vector<int> &path_y, vector<char> &rp_set, int &x, int &y);
void calc_water_flow();
void init_water_springs(int nws);
void process_water_springs();
void add_waves();
void update_accumulation(int xpos, int ypos);
bool update_depth_if_underwater(point const &pos, float &depth);
void add_hole_in_landscape_texture(int xpos, int ypos, float blend);
void setup_mesh_and_water_shader(shader_t &s, bool use_detail_normal_map, bool is_water);
inline bool cont_surf(int wsi, int wsi2, float zval) {
assert(size_t(wsi) < valleys.size() && size_t(wsi2) < valleys.size());
return (wsi == wsi2 || fabs(zval - valleys[wsi2].zval) < SMALL_DZ || spill.member2way(wsi, wsi2));
}
bool get_water_enabled(int x, int y) {
if (water_enabled == NULL) return 1;
int const xx(x + xoff2), yy(y + yoff2);
return (point_outside_mesh(xx, yy) || water_enabled[yy][xx]);
}
bool has_water(int x, int y) {
return (world_mode == WMODE_GROUND && !point_outside_mesh(x, y) && wminside[y][x] && get_water_enabled(x, y));
}
bool mesh_is_underwater(int x, int y) {
return (has_water(x, y) && mesh_height[y][x] < water_matrix[y][x]);
}
void water_color_atten_at_pos(colorRGBA &c, point const &pos) {
if (DISABLE_WATER || world_mode == WMODE_UNIVERSE || pos.z > ((world_mode == WMODE_GROUND) ? max_water_height : water_plane_z)) return;
int const x(get_xpos(pos.x)), y(get_ypos(pos.y));
if (world_mode != WMODE_GROUND || (has_water(x, y) && pos.z < water_matrix[y][x])) {
if (water_is_lava) {c = LAVA_COLOR; return;} // placeholder
water_color_atten((float *)&(c.R), x, y, pos);
}
}
void select_water_ice_texture(shader_t &shader, colorRGBA &color) {
if (water_is_lava) {
select_texture(LAVA_TEX);
shader.set_specular(0.1, 10.0); // low specular
color = LAVA_COLOR;
return;
}
bool const is_ice(temperature <= W_FREEZE_POINT);
select_texture(is_ice ? (int)ICE_TEX : (int)WATER_TEX);
shader.set_specular(w_spec[is_ice][0], w_spec[is_ice][1]);
color = (is_ice ? ICE_C : WATER_C);
color *= DARK_WATER_ATTEN;
}
void set_tt_water_specular(shader_t &shader) {
if (water_is_lava) {shader.set_specular(0.2, 20.0); return;} // 2x low specular
bool const is_ice(get_cur_temperature() <= W_FREEZE_POINT);
shader.set_specular(2.0*w_spec[is_ice][0], 2.0*w_spec[is_ice][1]); // 2x specular
}
colorRGBA get_tt_water_color() {
if (water_is_lava) {return LAVA_COLOR;}
bool const is_ice(get_cur_temperature() <= W_FREEZE_POINT);
colorRGBA color((is_ice ? ICE_C : WATER_C).modulate_with(texture_color(is_ice ? (int)ICE_TEX : (int)WATER_TEX)));
color *= DARK_WATER_ATTEN;
blend_color(color, colorRGBA(0.15, 0.1, 0.05, 1.0), color, water_params.mud, 0); // blend in mud color
color.alpha *= water_params.alpha;
color *= water_params.bright;
return color;
}
colorRGBA get_landscape_color(int xpos, int ypos) {
int cindex; // unused
assert(!point_outside_mesh(xpos, ypos));
point const pos(get_xval(xpos), get_yval(ypos), mesh_height[ypos][xpos]);
float const diffuse(coll_pt_vis_test(pos, get_light_pos(), 0.0, cindex, -1, 0, 3) ? 1.0 : 0.0); // Note: ignoring cloud shadows on mesh
return get_landscape_texture_color(xpos, ypos)*(0.5*(1.0 + diffuse)); // half ambient and half diffuse
}
void get_object_color(int cindex, colorRGBA &color) {
coll_obj const &cobj(coll_objects.get_cobj(cindex));
if (cobj.cp.coll_func == smiley_collision && has_invisibility(cobj.cp.cf_index)) return; // invisible simleys don't have a reflection
color = cobj.get_avg_color();
}
struct water_vertex_calc_t {
bool draw_fast;
float zval1, zval2;
water_vertex_calc_t() : draw_fast(0), zval1(def_water_level), zval2(def_water_level) {}
void calc_zvals(int i, int j, int wsi, int dx, int dy, bool outside_water) {
float const water_zmin(zbottom - MIN_WATER_DZ);
zval1 = max(water_matrix[i][j], water_zmin) - SMALL_NUMBER; // never go below water_zmin, what if wminside[i][j]==0 ?
if (i+dy >= 0 && i+dy < MESH_Y_SIZE) {
if (outside_water || watershed_matrix[i+dy][j].wsi == wsi ||
(wminside[i+dy][j] == 1 && cont_surf(wsi, watershed_matrix[i+dy][j].wsi, valleys[wsi].zval)))
{
zval2 = max(water_matrix[i+dy][j], water_zmin) - SMALL_NUMBER; // never go below water_zmin
}
}
}
void blend_reflection_color(point const &v, colorRGBA &color, vector3d const &n, point const &camera) {
vector3d const view_dir((v - camera).get_norm());
float const view_dp(dot_product(view_dir, n));
//if (view_dp >= 0.0) {color = ALPHA0; return;} // back facing water - not valid (need to check all connected vertices/faces?)
// add some green at shallow view angles
blend_color(color, colorRGBA(0.0, 1.0, 0.5, 1.0), color, 0.25*(1.0 - fabs(view_dp)), 0);
vector3d dir;
calc_reflection_angle(view_dir, dir, n);
point vs(v), ve(v + dir*(4.0*XY_SCENE_SIZE/dir.mag()));
colorRGBA rcolor(ALPHA0);
bool mesh_int(0), cobj_int(0);
if (!fast_water_reflect) {
bool skip_mesh(!do_line_clip_scene(vs, ve, zbottom, max(ztop, czmax)));
int xpos, ypos;
float mesh_zval;
int const fast(2); // less accurate but faster
if (!skip_mesh && line_intersect_mesh(vs, ve, xpos, ypos, mesh_zval, fast, 1) /*&& mesh_zval > vs.z*/) { // not sure about this last test
float const t((fabs(ve.z - vs.z) < TOLERANCE) ? 0.0 : (mesh_zval - vs.z)/(ve.z - vs.z));
ve = (vs + (ve - vs)*t);
rcolor = get_landscape_color(xpos, ypos);
mesh_int = 1;
if (dir.z < 0.0 && is_underwater(ve)) {
// if mesh int point is underwater, attenuate along light path and blend like we do when drawing the mesh
// could calculate approx water intersection and call recursively, but there are too many problems with that approach
water_color_atten_pt(&rcolor.R, xpos, ypos, ve, v, get_light_pos());
float const t2((fabs(v.z - ve.z) < TOLERANCE) ? 0.0 : (water_matrix[ypos][xpos] - ve.z)/(v.z - ve.z));
ve = (ve + (v - ve)*t2); // updated approx water collision point
blend_color(rcolor, color, rcolor, 0.5, 1); // add in a watery color
}
}
int cindex;
point cpos; // unused
vector3d cnorm; // unused
assert(!is_nan(vs) && !is_nan(ve));
if (check_coll_line_exact(vs, ve, cpos, cnorm, cindex, 0.0, -1, 1, 0, 0)) {
get_object_color(cindex, rcolor);
cobj_int = 1;
}
}
if (!mesh_int && !cobj_int) { // no mesh intersect and no cobj intersect
vector3d const vdir((ve - vs).get_norm());
float const cloud_density(get_cloud_density(vs, vdir)); // cloud_color vs. bkg_color
blend_color(rcolor, get_cloud_color(), get_bkg_color(vs, vdir), CLIP_TO_01(2.0f*cloud_density), 1);
}
if (rcolor.alpha > 0.0) {
float const refract_ix((temperature <= W_FREEZE_POINT) ? ICE_INDEX_REFRACT : WATER_INDEX_REFRACT);
float r(CLIP_TO_01(get_fresnel_reflection(-view_dir, n, 1.0, refract_ix))); // maybe incorrect if out of the [0.0, 1.0] range?
rcolor.alpha = 1.0; // transparent objects
blend_color(color, rcolor, color, r*rcolor.alpha, 1);
}
}
void calc_vertex_cn(vert_norm_color &vnc, int i, int j, colorRGBA const &color_in) {
assert(!point_outside_mesh(j, i));
vnc.n = wat_vert_normals[i][j];
colorRGBA color(color_in); // note that the texture is blue, so that's why the color is more whiteish
if (!(display_mode & 0x20) && !draw_fast && !has_snow && vnc.v.z > mesh_height[i][j]) { // calculate water reflection and blend into color
point const camera(get_camera_pos());
if (camera.z > vnc.v.z) {blend_reflection_color(vnc.v, color, vnc.n, camera);} // below the camera
}
vnc.set_c4(color);
}
};
unsigned const RESERVED_IX = 0xFFFFFFFF;
class mesh_strip_drawer : public indexed_vao_manager_t {
protected:
vector<vert_norm_color> verts; // vertex data
private:
vector<unsigned> pt_to_ix; // maps mesh {x,y} values to vertex index
vector<unsigned> indices; // indexes into verts
public:
mesh_strip_drawer() {pt_to_ix.resize(XY_MULT_SIZE, RESERVED_IX);}
bool add_vertex(int x, int y, float zval=0.0) {
unsigned const pos(y*MESH_X_SIZE + x);
assert(pos < pt_to_ix.size());
unsigned &ix(pt_to_ix[pos]);
bool const is_new_vertex(ix == RESERVED_IX);
if (is_new_vertex) {
ix = verts.size();
vert_norm_color vnc; // zval/color/normal not filled in here - up to the caller to do this
vnc.v.assign(get_xval(x), get_yval(y), zval);
verts.push_back(vnc);
}
indices.push_back(ix);
return is_new_vertex;
}
void end_strip() {indices.push_back(RESERVED_IX);}
void draw() {
if (verts.empty()) {assert(indices.empty()); return;} // nothing to do
create_and_upload(verts, indices, 2);
vert_norm_color::set_vbo_arrays();
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(RESERVED_IX);
glDrawRangeElements(GL_TRIANGLE_STRIP, 0, verts.size(), indices.size(), GL_UNSIGNED_INT, NULL);
++num_frame_draw_calls;
glDisable(GL_PRIMITIVE_RESTART);
post_render();
}
void clear() {
clear_vbos();
verts.clear();
indices.clear();
for (auto i = pt_to_ix.begin(); i != pt_to_ix.end(); ++i) {*i = RESERVED_IX;}
}
};
class water_strip_drawer : public mesh_strip_drawer, public water_vertex_calc_t {
public:
void add_segment(int i, int j, int wsi, int dx, int dy) {
calc_zvals(i, j, wsi, dx, dy, 1); // outside_water=1
for (int d = 0; d < 2; ++d) {
bool const dd((d != 0) ^ (dx < 0) ^ (dy < 0));
add_vertex(j, i+dd*dy, (dd ? zval2 : zval1));
}
}
void calc_vertex_colors_normals(colorRGBA const &color_in) {
if (verts.empty()) return;
// Note: frame_counter check is a hack to avoid Nvidia driver perf problem where we get only a few FPS for the first 8s
bool const use_threads(!fast_water_reflect && !draw_fast && !(display_mode & 0x20) /*&& frame_counter > 100*/);
// run on multiple threads when we have the slow ray-traced per-vertex water reflections enabled (assumes a quad core machine)
#pragma omp parallel for num_threads(4) schedule(dynamic,8) if (use_threads)
for (int i = 0; i < (int)verts.size(); ++i) {
vert_norm_color &vnc(verts[i]);
calc_vertex_cn(vnc, get_ypos(vnc.v.y), get_xpos(vnc.v.x), color_in);
}
}
void draw_outside_water_range(int x1, int y1, int x2, int y2, int dx, int dy) {
if (x1 == x2 || y1 == y2) return; // empty range
int last_draw(0);
for (int i = y1; i != y2; i += dy) {
for (int j = x1; j != x2+dx; j += dx) {
if (j != x2 && (
(wminside[i][j] == 2 && water_matrix[i][j] > mesh_height[i][j]) ||
(wminside[i+dy][j] == 2 && water_matrix[i+dy][j] > mesh_height[i+dy][j]) ||
(wminside[i][j+dx] == 2 && water_matrix[i][j+dx] > mesh_height[i][j+dx]) ||
(wminside[i+dy][j+dx] == 2 && water_matrix[i+dy][j+dx] > mesh_height[i+dy][j+dx])))
{
add_segment(i, j, 0, dx, dy);
last_draw = 1;
}
else if (last_draw) {
add_segment(i, j, 0, dx, dy);
end_strip();
last_draw = 0;
}
} // for j
} // for i
assert(!last_draw);
}
};
class water_surface_draw : public water_vertex_calc_t {
struct color_scale_ix {
vector3d n;
color_wrapper cw;
int ix;
color_scale_ix() : n(zero_vector), ix(-1) {}
color_scale_ix(color_wrapper const &cw_, vector3d const &n_, int ix_) : n(n_), cw(cw_), ix(ix_) {}
};
vector<color_scale_ix> last_row_colors;
vector<vert_norm_color> verts;
public:
water_surface_draw() {last_row_colors.resize(MESH_X_SIZE);}
vector<vert_norm_color> &get_verts() {return verts;}
void draw_water_surface(int i, int j, colorRGBA const &color_in, int wsi) {
assert(unsigned(j) < last_row_colors.size());
calc_zvals(i, j, wsi, 1, 1, 0); // outside_water=0
float const x(get_xval(j)), y(get_yval(i));
for (int d = 0; d < 2; ++d) {
point const v(x, y+d*DY_VAL, (d ? zval2 : zval1));
vert_norm_color vnc(v, last_row_colors[j].n, last_row_colors[j].cw.c); // note that the texture is blue, so that's why the color is more whiteish
if (last_row_colors[j].ix != (i+d)) { // gets here about half the time
calc_vertex_cn(vnc, (i+d), j, color_in);
if (d) {last_row_colors[j] = color_scale_ix(vnc, vnc.n, (i+d));}
}
verts.push_back(vnc);
}
}
void end_strip() {draw_and_clear_verts(verts, GL_TRIANGLE_STRIP);}
};
void draw_water(bool no_update, bool draw_fast) {
RESET_TIME;
int wsi(0), last_water(2), last_draw(0), lc0(landscape_changed);
colorRGBA color(WHITE);
static float tdx(0.0), tdy(0.0);
float const tx_scale(W_TEX_STRETCH/TWO_XSS), ty_scale(W_TEX_STRETCH/TWO_YSS);
if (!no_update) {
process_water_springs();
add_waves();
if (DEBUG_WATER_TIME) {PRINT_TIME("0 Add Waves");}
}
if (DISABLE_WATER) return;
bool const use_foam(USE_SEA_FOAM && !water_is_lava);
shader_t s;
if (use_foam) {s.set_prefix("#define ADD_DETAIL_TEXTURE", 1);} // FS
setup_mesh_and_water_shader(s, 0, 1);
if (use_foam) {s.add_uniform_float("detail_tex_scale", 0.0);}
point const camera(get_camera_pos());
bool const is_ice(temperature <= W_FREEZE_POINT);
if (!is_ice) {
tdx -= WATER_WIND_EFF2*wind.x*fticks;
tdy -= WATER_WIND_EFF2*wind.y*fticks;
}
float const tx_val(tx_scale*xoff2*DX_VAL + tdx), ty_val(ty_scale*yoff2*DX_VAL + tdy);
if (DEBUG_WATER_TIME) {PRINT_TIME("1 Init");}
// draw exterior water (oceans)
if (!enable_clip_plane_z || water_plane_z > clip_plane_z) { // outside water
if (camera.z >= water_plane_z) {draw_water_sides(s, 1);}
if (DEBUG_WATER_TIME) {PRINT_TIME("2.1 Draw Water Sides");}
enable_blend();
select_water_ice_texture(s, color);
setup_texgen(tx_scale, ty_scale, tx_val, ty_val, 0.0, s, 0);
if (!water_is_lava) {color.alpha *= 0.5;} // increase alpha for water
int const xend(MESH_X_SIZE-1), yend(MESH_Y_SIZE-1);
if (use_foam) { // use sea foam texture
select_texture(FOAM_TEX, 1);
setup_texgen(20.0*tx_scale, 20.0*ty_scale, 0.0, 0.0, 0.0, s, 0);
s.add_uniform_float("detail_tex_scale", 1.0);
}
static water_strip_drawer wsdraw;
wsdraw.draw_fast = draw_fast;
// draw back-to-front away from the player in 4 quadrants to make the alpha blending work correctly
point const camera_adj(camera - point(0.5*DX_VAL, 0.5*DY_VAL, 0.0)); // hack to fix incorrect offset
int const cxpos(max(0, min(xend, get_xpos(camera_adj.x)))), cypos(max(0, min(yend, get_ypos(camera_adj.y))));
wsdraw.draw_outside_water_range(cxpos, cypos, xend, yend, 1, 1);
wsdraw.draw_outside_water_range(cxpos, cypos, xend, 0, 1, -1);
wsdraw.draw_outside_water_range(cxpos, cypos, 0, yend, -1, 1);
wsdraw.draw_outside_water_range(cxpos, cypos, 0, 0, -1, -1);
wsdraw.calc_vertex_colors_normals(color);
wsdraw.draw();
wsdraw.clear();
if (use_foam) {s.add_uniform_float("detail_tex_scale", 0.0);}
if (DEBUG_WATER_TIME) {PRINT_TIME("2.2 Water Draw Fixed");}
if (camera.z < water_plane_z) {draw_water_sides(s, 1);}
}
static rand_gen_t rgen;
if (!no_update && !no_grass()) {
unsigned const num_updates(max(1, XY_MULT_SIZE/256));
for (unsigned n = 0; n < num_updates; ++n) {
int const i(rgen.rand()%MESH_Y_SIZE), j(rgen.rand()%MESH_X_SIZE); // select a random mesh element
if (wminside[i][j] == 1 && get_water_enabled(j, i) && !is_mesh_disabled(j, i) && mesh_height[i][j] < water_matrix[i][j]) {
modify_grass_at(get_mesh_xyz_pos(j, i), HALF_DXY, 0, 0, 0, 1); // check underwater
}
}
if (DEBUG_WATER_TIME) {PRINT_TIME("3 Grass Update");}
}
select_water_ice_texture(s, color);
setup_texgen(tx_scale, ty_scale, tx_val, ty_val, 0.0, s, 0);
enable_blend();
if (!no_update) {
update_valleys_and_draw_spillover(); // draws spillover sections using the same shader
if (DEBUG_WATER_TIME) {PRINT_TIME("4 Water Valleys Update");}
assert(fticks != 0.0);
if (animate2) { // call the function that computes the ripple effect
unsigned num_steps(1);
if (!(start_ripple || first_water_run) || is_ice || (!start_ripple && first_water_run)) {}
else {
assert(I_TIMESCALE2 > 0);
num_steps = max(1U, min(MAX_RIPPLE_STEPS, unsigned(min(3.0f, fticks)*min(MAX_I_TIMESCALE, I_TIMESCALE2))));
}
for (unsigned i = 0; i < num_steps; ++i) {compute_ripples();}
calc_water_normals();
}
if (DEBUG_WATER_TIME) {PRINT_TIME("5 Water Ripple Update");}
}
unsigned nin(0);
int xin[4] = {}, yin[4] = {}, last_wsi(-1);
bool const disp_snow((display_mode & 0x40) && temperature <= SNOW_MAX_TEMP);
if (!water_is_lava) {color *= INT_WATER_ATTEN;} // attenuate for interior water
colorRGBA wcolor(color);
water_surface_draw wsd;
// draw interior water (ponds)
for (int i = 0; i < MESH_Y_SIZE; ++i) {
for (int j = 0; j < MESH_X_SIZE; ++j) {
if (!is_ice && has_snow_accum && !no_update) {update_accumulation(j, i);}
nin = 0;
if (wminside[i][j] == 1) {
wsi = watershed_matrix[i][j].wsi;
assert(size_t(wsi) < valleys.size());
float const z_min(z_min_matrix[i][j]), zval(valleys[wsi].zval), wzval(water_matrix[i][j]), wzmax(max(zval, wzval));
if (wzmax >= z_min - G_W_STOP_DEPTH) {
if (!is_ice && UPDATE_UW_LANDSCAPE && !no_update && wzval > zval && (rgen.rand()&63) == 0) {
add_hole_in_landscape_texture(j, i, 1.2*fticks*(0.02 + wzmax - z_min));
}
float delta_area(1.0);
if (point_interior_to_mesh(j, i)) { // more accurate but less efficient
delta_area = 0.0;
for (int y = -1; y <= 1; ++y) {
for (int x = -1; x <= 1; ++x) {
if (zval >= mesh_height[i+y][j+x]) {delta_area += 1.0;}
}
}
delta_area /= 9.0;
}
valleys[wsi].area += SQUARE_S_AREA*delta_area; // if !no_update?
if (wzmax >= z_min && i < MESH_Y_SIZE-1 && j < MESH_X_SIZE-1) {
xin[nin] = j; yin[nin] = i; ++nin;
for (int ii = i; ii < i+2; ++ii) {
for (int jj = j + (ii == i); jj < j+2; ++jj) {
if (wminside[ii][jj] == 1 && cont_surf(wsi, watershed_matrix[ii][jj].wsi, zval)) {
xin[nin] = jj; yin[nin] = ii; ++nin;
}
}
}
if (nin == 4) {
if (disp_snow && accumulation_matrix[i][j] >= SNOW_THRESHOLD && (mesh_height[i][j] <= zval || (i > 0 && j > 0 && mesh_height[i-1][j-1] <= zval))) {
if (last_water != 0) { // snow
wsd.end_strip();
select_texture(SNOW_TEX);
s.set_specular(0.6, 20.0);
last_water = 0;
color = WHITE;
}
}
else if (last_water != 1) {
wsd.end_strip();
select_water_ice_texture(s, color);
color *= INT_WATER_ATTEN; // attenuate for interior water
last_water = 1;
}
if (wsi != last_wsi) {
float const blood_mix(valleys[wsi].blood_mix), mud_mix(valleys[wsi].mud_mix);
wcolor = color;
if (blood_mix > 0.99) {wcolor = RED;} // all blood
else {
wcolor.alpha *= 0.5;
if (mud_mix > 0.0) {blend_color(wcolor, (!is_ice ? MUD_S_C : LT_BROWN), wcolor, mud_mix, 1);}
if (blood_mix > 0.0) {blend_color(wcolor, RED, wcolor, blood_mix, 1);}
}
last_wsi = wsi;
}
wsd.draw_water_surface(i, j, wcolor, wsi);
last_draw = 1;
} // nin == 4
}
}
}
if (nin < 4) { // not a full quad of water
if (last_draw) {wsd.draw_water_surface(i, j, wcolor, wsi);}
if (nin == 3) { // single triangle, use color from previous vertex
vector<vert_norm_color> &verts(wsd.get_verts());
colorRGBA prev_color(verts.empty() ? wcolor : verts.back().get_c4()); // verts should be nonempty, so should get reset
for (unsigned p = 0; p < 3; ++p) { // Note: if last_draw==1, degenerate triangles are generated
point const pt(get_xval(xin[p]), get_yval(yin[p]), (water_matrix[yin[p]][xin[p]] - SMALL_NUMBER));
verts.emplace_back(pt, wat_vert_normals[yin[p]][xin[p]], prev_color);
}
}
if (last_draw || nin == 3) {wsd.end_strip();}
last_draw = 0;
}
} // for j
} // for i
wsd.end_strip();
disable_blend();
s.clear_specular();
s.end_shader();
if (!no_update) {
update_water_volumes();
if (!lc0 && rgen.rand()%5 != 0) {landscape_changed = 0;} // reset, only update landscape 20% of the time
first_water_run = 0;
}
if (DEBUG_WATER_TIME) {PRINT_TIME("6 Water Draw");}
}
void calc_water_normals() {
if (DISABLE_WATER) return;
vector3d *wsn0(wat_surf_normals[0]), *wsn1(wat_surf_normals[1]);
for (int i = 0; i < MESH_Y_SIZE; ++i) {
// if we have ice, the normals can't change and don't need to be updated;
// however, if they were never calculated, they do need to be update on the first frame;
// since we can't tell if the normals are valid, it's safest to always just update them
// *** but it's certainly not right to always set them to plus_z ***
#if 0
if (temperature <= W_FREEZE_POINT) { // ice
for (int j = 0; j < MESH_X_SIZE; ++j) {
if (!point_interior_to_mesh(j, i) || wminside[i][j]) {wsn1[j] = wat_vert_normals[i][j] = plus_z;}
}
} else
#endif
{ // water
for (int j = 0; j < MESH_X_SIZE; ++j) {
if (point_interior_to_mesh(j, i) && wminside[i][j] && water_matrix[i][j] >= z_min_matrix[i][j]) { // inside
wsn1[j] = get_matrix_surf_norm(water_matrix, NULL, MESH_X_SIZE, MESH_Y_SIZE, j, i);
vector3d nv(wsn1[j]);
if (i > 0) {nv += wsn0[j];}
if (i > 0 && j > 0) {nv += wsn0[j-1];}
if (j > 0) {nv += wsn1[j-1];}
wat_vert_normals[i][j] = nv.get_norm();
}
else {
wsn1[j] = wat_vert_normals[i][j] = plus_z;
}
} // for j
}
swap(wsn0, wsn1);
} // for i
}
inline void update_water_edges(int i, int j) {
if (i > 0 && wminside[i-1][j] == 1) {
water_matrix[i][j] = valleys[watershed_matrix[i-1][j].wsi].zval;
}
else if (j > 0 && wminside[i][j-1] == 1) {
water_matrix[i][j] = valleys[watershed_matrix[i][j-1].wsi].zval;
}
else {
water_matrix[i][j] = def_water_level;
}
}
void compute_ripples() {
if (DISABLE_WATER) return;
static unsigned dtime1(0), dtime2(0), counter(0);
bool const update_iter((counter%UPDATE_STEP) == 0);
RESET_TIME;
if (temperature > W_FREEZE_POINT && (start_ripple || first_water_run)) {
float const tstep(max(fticks, 0.25f)); // ensure some min amount of damping to prevent unstable ripples when the framerate is very high
float const rm_atten(pow(RIPPLE_MAT_ATTEN, tstep)), rdamp1(pow(RIPPLE_DAMP1, tstep)), rdamp2(RIPPLE_DAMP2*tstep);
start_ripple = 0;
for (int i = 0; i < MESH_Y_SIZE; ++i) {
for (int j = 0; j < MESH_X_SIZE; ++j) {
if (!wminside[i][j] || water_matrix[i][j] < z_min_matrix[i][j] /*|| !get_water_enabled(j, i)*/) continue;
short const i8(watershed_matrix[i][j].inside8);
fix_fp_mag(ripples[i][j].rval);
float const rmij(ripples[i][j].rval);
float &acc(ripples[i][j].acc);
fix_fp_mag(acc);
acc *= rm_atten;
if (!start_ripple && fabs(acc) > 1.0E-6) start_ripple = 1;
// 00 0- -0 0+ +0 -- +- ++ -+ 22 11
// 01 02 04 08 10 20 40 80 100 200 400
if (point_interior_to_mesh(j, i)) { // fast mode
float const d0( rmij - ripples[i ][j-1].rval);
if (i8 & 0x02) ripples[i ][j-1].acc += d0;
float const d2( rmij - ripples[i ][j+1].rval);
if (i8 & 0x08) ripples[i ][j+1].acc += d2;
float const d4((rmij - ripples[i-1][j-1].rval)*SQRTOFTWOINV);
if (i8 & 0x20) ripples[i-1][j-1].acc += d4;
float const d1( rmij - ripples[i-1][j ].rval);
if (i8 & 0x04) ripples[i-1][j ].acc += d1;
float const d7((rmij - ripples[i-1][j+1].rval)*SQRTOFTWOINV);
if (i8 & 0x100) ripples[i-1][j+1].acc += d7;
float const d5((rmij - ripples[i+1][j-1].rval)*SQRTOFTWOINV);
if (i8 & 0x40) ripples[i+1][j-1].acc += d5;
float const d3( rmij - ripples[i+1][j ].rval);
if (i8 & 0x10) ripples[i+1][j ].acc += d3;
float const d6((rmij - ripples[i+1][j+1].rval)*SQRTOFTWOINV);
if (i8 & 0x80) ripples[i+1][j+1].acc += d6;
acc -= d0 + d1 + d2 + d3 + d4 + d5 + d6 + d7;
fix_fp_mag(acc);
continue;
}
if (j > 0) {
float const dz(rmij - ripples[i][j-1].rval);
if (i8 & 0x02) ripples[i][j-1].acc += dz;
acc -= dz;
if (i > 0) {
float const dz((rmij - ripples[i-1][j-1].rval)*SQRTOFTWOINV);
if (i8 & 0x20) ripples[i-1][j-1].acc += dz;
acc -= dz;
}
if (i < MESH_Y_SIZE-1) {
float const dz((rmij - ripples[i+1][j-1].rval)*SQRTOFTWOINV);
if (i8 & 0x40) ripples[i+1][j-1].acc += dz;
acc -= dz;
}
}
if (i > 0) {
float const dz(rmij - ripples[i-1][j].rval);
if (i8 & 0x04) ripples[i-1][j].acc += dz;
acc -= dz;
}
if (j < MESH_X_SIZE-1) {
float const dz(rmij - ripples[i][j+1].rval);
if (i8 & 0x08) ripples[i][j+1].acc += dz;
acc -= dz;
if (i < MESH_Y_SIZE-1) {
float const dz((rmij - ripples[i+1][j+1].rval)*SQRTOFTWOINV);
if (i8 & 0x80) ripples[i+1][j+1].acc += dz;
acc -= dz;
}
if (i > 0) {
float const dz((rmij - ripples[i-1][j+1].rval)*SQRTOFTWOINV);
if (i8 & 0x100) ripples[i-1][j+1].acc += dz;
acc -= dz;
}
}
if (i < MESH_Y_SIZE-1) {
float const dz(rmij - ripples[i+1][j].rval);
if (i8 & 0x10) ripples[i+1][j].acc += dz;
acc -= dz;
}
fix_fp_mag(acc);
} // for j
} // for i
if (DEBUG_RIPPLE_TIME) dtime1 += GET_DELTA_TIME;
for (int i = 0; i < MESH_Y_SIZE; ++i) {
for (int j = 0; j < MESH_X_SIZE; ++j) {
float ripple_zval(0.0);
if (wminside[i][j]) {
float const zval(rdamp1*(ripples[i][j].rval + rdamp2*ripples[i][j].acc)); // ripple wave height
ripple_zval = ((fabs(zval) < TOLERANCE) ? 0.0 : zval); // prevent small floating point numbers
}
if (wminside[i][j] == 1) { // dynamic water
int const wsi(watershed_matrix[i][j].wsi);
assert(size_t(wsi) < valleys.size());
if (water_matrix[i][j] < z_min_matrix[i][j] && fabs(ripples[i][j].rval) < 1.0E-4 && fabs(ripples[i][j].acc) < 1.0E-4) { // under ground - no ripple
if (update_iter) water_matrix[i][j] = valleys[wsi].zval;
continue;
}
float const depth(valleys[wsi].depth);
if (depth < 0) {
ripples[i][j].rval *= rm_atten;
if (update_iter) water_matrix[i][j] = valleys[wsi].zval;
continue;
}
float const zval(max(min(ripple_zval, depth), -depth)); // max ripple height equals water depth
ripples[i][j].rval = rm_atten*zval;
water_matrix[i][j] = valleys[wsi].zval + zval;
}
else if (wminside[i][j] == 2) { // fixed water
ripples[i][j].rval = rm_atten*ripple_zval;
water_matrix[i][j] = water_plane_z + min(MAX_RIPPLE_HEIGHT, ripple_zval);
water_matrix[i][j] = max(water_matrix[i][j], zbottom);
}
else if (update_iter) {
if (get_water_enabled(j, i)) {
update_water_edges(i, j);
}
else {
ripples[i][j].rval = 0.0; // not sure if this is correct, or if there is something else that should be done here
}
}
} // for j
} // for i
if (DEBUG_RIPPLE_TIME) dtime2 += GET_DELTA_TIME;
}
else { // no ripple
matrix_clear_2d(ripples);
// must clear ripples at least once at the beginning
if (NO_ICE_RIPPLES || counter == 0 || temperature > W_FREEZE_POINT) {
for (int i = 0; i < MESH_Y_SIZE; ++i) {
for (int j = 0; j < MESH_X_SIZE; ++j) {
if (wminside[i][j] == 1) {
water_matrix[i][j] = valleys[watershed_matrix[i][j].wsi].zval;
}
else {
update_water_edges(i, j);
}
} // for j
} // for i
}
} // ripple
++counter;
if (DEBUG_RIPPLE_TIME && (counter%20) == 0) {
cout << "times = " << dtime1 << ", " << dtime2 << endl; // cumulative
dtime1 = dtime2 = 0;
}
}
// Note: we could calculate xpos and ypox from pos, but in all callers xpos and ypos are already available
void add_splash(point const &pos, int xpos, int ypos, float energy, float radius, bool add_sound, vector3d const &vadd, bool add_droplets) {
//energy *= 10.0; // debugging
if (DISABLE_WATER || !(display_mode & 0x04) || temperature <= W_FREEZE_POINT) return;
if (point_outside_mesh(xpos, ypos) || is_mesh_disabled(xpos, ypos)) return; // no mesh, no splash
if (water_matrix[ypos][xpos] < (mesh_height[ypos][xpos] - 0.5*radius)) return; // water is too low
int in_wmatrix(0), wsi(0);
float water_mix(1.0);
if (wminside[ypos][xpos] == 1) {
float const mh_r(mesh_height[ypos][xpos] + radius);
if (h_collision_matrix[ypos][xpos] < mh_r && water_matrix[ypos][xpos] < (mh_r + MAX_SPLASH_DEPTH)) {
wsi = watershed_matrix[ypos][xpos].wsi;
water_mix = (1.0 - valleys[wsi].blood_mix);
in_wmatrix = 1;
}
}
assert(energy >= 0.0);
energy = min(energy, 10000.0f);
float const splash_size(min(0.006f*(0.5f + water_mix)*sqrt((4.0f/XY_SCENE_SIZE)*energy), MAX_SPLASH_SIZE));
if (splash_size < TOLERANCE) return;
float const rad(min(4, (int)ceil(0.5f*radius/(DX_VAL + DY_VAL)))), radsq(rad*rad);
int const droplet_id(coll_id[DROPLET]);
if (add_droplets && energy > 5.0 && droplet_id >= 0 && temperature > W_FREEZE_POINT && point_interior_to_mesh(xpos, ypos)) {
// splash size = 0.3 - rain, 2.0 - ball, 60.0 - rocket
float const sqrt_energy(sqrt(energy));
int ndrops(min(MAX_SPLASH_DROP, int((0.5 + 0.2*water_mix)*sqrt_energy)));
if (ndrops > 0 && water_matrix[ypos][xpos] >= z_min_matrix[ypos][xpos] && wminside[ypos][xpos]) {
if (in_wmatrix) {
valleys[wsi].mud_mix += 0.12*sqrt_energy/(valleys[wsi].w_volume + 1.0);
valleys[wsi].mud_mix = min(1.0f, valleys[wsi].mud_mix);
}
if (wminside[ypos][xpos] == 1) { // dynamic water
ndrops = min(ndrops, int(0.25*valleys[watershed_matrix[ypos][xpos].wsi].w_volume));
}
if (ndrops > 0) {
//valleys[watershed_matrix[ypos][xpos].wsi].w_volume -= ndrops;
float const vz(5.0 + (0.2 + 0.1*water_mix)*sqrt_energy);
point const ppos(pos + vector3d(0.0, 0.0, 1.0*radius));
float const mud_mix(in_wmatrix ? valleys[wsi].mud_mix : 0.0), blood_mix(in_wmatrix ? valleys[wsi].blood_mix : 0.0);
add_water_particles(ppos, vadd+vector3d(0.0, 0.0, 1.5*vz), 0.25*vz, 1.0*radius, mud_mix, blood_mix, (20 + (rand()&7))*ndrops);
obj_group &objg(obj_groups[droplet_id]);
for (int o = 0; o < ndrops; ++o) {
int const i(objg.choose_object());
objg.create_object_at(i, ppos);
objg.get_obj(i).velocity = vadd + gen_rand_vector(vz*rand_uniform(0.05, 0.1), 20.0, PI_TWO);
vadd_rand(objg.get_obj(i).pos, 1.0*radius, 1);
}
if (add_sound && energy > 10.0) {gen_sound(SOUND_SPLASH1, pos, min(1.0, 0.05*sqrt_energy));}
}
}
}
int const irad(ceil(rad)), x1(max((xpos - irad), 0)), y1(max((ypos - irad), 0));
int const x2(min((xpos + irad), MESH_X_SIZE-1)), y2(min((ypos + irad), MESH_Y_SIZE-1));
for (int i = y1; i <= y2; i++) {
for (int j = x1; j <= x2; j++) {
if (((i - ypos)*(i - ypos) + (j - xpos)*(j - ypos)) <= radsq && wminside[i][j]) {ripples[i][j].rval += splash_size;}
}
}
start_ripple = 1;
}
void add_waves() { // add waves due to wind
//RESET_TIME;
if (DISABLE_WATER || !(display_mode & 0x04) || !(display_mode & 0x0100) || water_is_lava) return;
if (temperature <= W_FREEZE_POINT || !animate2) return;
float const wave_amplitude(0.006/mesh_scale), wave_freq(0.06), depth_scale(20.0);
float const wind_freq(0.02), wind_amplitude(0.15/mesh_scale), wind_resist(0.75);
float const wxoff(wind_resist*total_wind.x/TWO_XSS/MESH_X_SIZE);
float const wyoff(wind_resist*total_wind.y/TWO_YSS/MESH_Y_SIZE);
float const fticks_clamped(min(fticks, 10.0f));
static float wave_time(0.0);
wave_time += fticks_clamped;
if (wave_time > 4000.0) {wave_time = 0.0;} // reset at 4000 ticks (2 min. or so) to avoid FP error
#pragma omp parallel for schedule(static,8) num_threads(2)
for (int y = 0; y < MESH_Y_SIZE; ++y) {
for (int x = 0; x < MESH_X_SIZE; ++x) {
if (!wminside[y][x] || !get_water_enabled(x, y)) continue; // only in water
float const wh(water_matrix[y][x]), depth(wh - mesh_height[y][x]);
if (depth < SMALL_NUMBER) continue; // not deep enough for waves
vector3d const local_wind(get_local_wind(x, y, wh));
float const lwmag(local_wind.mag());
float const tx(min(0.2f, fabs(local_wind.y))*wind_freq*(x + xoff2)/lwmag - wxoff);
float const ty(min(0.2f, fabs(local_wind.x))*wind_freq*(y + yoff2)/lwmag - wyoff);
float const val(get_texture_component(WIND_TEX, tx, ty, 0));
float const wval(wind_amplitude*min(2.5f, sqrt(lwmag))*val*min(depth, 0.1f));
if (wminside[y][x] == 2) { // outside water (oceans)
ripples[y][x].rval += wval + wave_amplitude*fticks_clamped*sin(wave_freq*wave_time + depth_scale*depth);
}
else if (fabs(ripples[y][x].rval) < 0.1*wval) { // don't add wind if already rippling to prevent instability
ripples[y][x].rval += wval;
}
start_ripple = 1;
}
}
//PRINT_TIME("Add Waves");
}
// *** BEGIN VALLEYS/SPILLOVER ***
void check_spillover(int i, int j, int ii, int jj, int si, int sj, float zval, int wsi) { // does wsi overflow?
float const z_over(zval - mesh_height[ii][jj]);
if (z_over <= max(0.0f, valleys[wsi].sf.z_over)) return;
if (wminside[i][j] != 1) { // spillover outside
valleys[wsi].sf = valley::spill_func(-1, i, j, si, sj, SPILL_OUTSIDE, z_over);
return;
}
int const index(watershed_matrix[i][j].wsi); // spills into this pool
assert(size_t(index) < valleys.size());
if (index != wsi && zval > valleys[index].zval) { // spillover inside
if (!spill.member(index, wsi)) valleys[wsi].sf = valley::spill_func(index, i, j, si, sj, SPILL_INSIDE, z_over); // pools not combined
valleys[wsi].spill_index = index;
}
}
void sync_water_height(int wsi, int skip_ix, float zval, float z_over, vector<unsigned> &cc) {
spill.get_connected_components(wsi, cc);
for (unsigned k = 0; k < cc.size(); ++k) {
if ((int)cc[k] == wsi || (int)cc[k] == skip_ix) continue;
valley &v(valleys[cc[k]]);
v.zval = zval;//max(zval, v.zval);
v.dz -= z_over;
v.w_volume = v.lwv; // ???