-
Notifications
You must be signed in to change notification settings - Fork 0
/
water.cpp
645 lines (548 loc) · 15.8 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
/* Water filter
*
* (c) Copyright 2000-2007 Denis Rojo <jaromil@dyne.org>
*
* from an original idea of water algorithm by Federico 'Pix' Feroldi
*
* this code contains optimizations by Jason Hood and Scott Scriven
*
* animated background, 32bit colorspace and interactivity by jaromil
* ported to C++ and frei0r plugin API in 2007
*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* Please refer to the GNU Public License for more details.
*
* You should have received a copy of the GNU Public License along with
* this source code; if not, write to:
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* "$Id: water.c 193 2004-06-01 11:00:25Z jaromil $"
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <frei0r.hpp>
#define CLIP_EDGES \
if(x - radius < 1) left -= (x-radius-1); \
if(y - radius < 1) top -= (y-radius-1); \
if(x + radius > geo->w-1) right -= (x+radius-geo->w+1); \
if(y + radius > geo->h-1) bottom-= (y+radius-geo->h+1);
/* water physics */
#define WATER 1
#define JELLY 2
#define SLUDGE 3
#define SUPER_SLUDGE 4
/* precalculated sinusoidal tables */
#include <math.h>
#define FSINMAX 2047
#define SINFIX 16
#define FSINBITS 16
#ifndef PI
#define PI 3.14159265358979323846
#endif
typedef struct {
int16_t w;
int16_t h;
uint8_t bpp;
uint32_t size;
} ScreenGeometry;
class Water: public frei0r::filter {
public:
f0r_param_double physics;
f0r_param_bool swirl;
f0r_param_bool rain;
f0r_param_bool surfer;
f0r_param_bool smooth;
f0r_param_bool distort;
f0r_param_position position;
//bool randomize_swirl;
Water(unsigned int width, unsigned int height) {
physics = 1.0;
swirl = 1;
rain = 0;
surfer = 0;
distort = 0;
smooth = 0;
position.x = 0;
position.y = 0;
register_param(physics, "physics", "water density: from 0.0 to 1.0");
register_param(swirl, "swirl", "swirling whirpool in the center");
register_param(rain, "rain", "rain drops all over");
register_param(surfer, "surfer", "surf the surface with a wandering finger");
register_param(smooth, "smooth", "smooth up all perturbations on the surface");
register_param(distort, "distort", "distort all surface like dropping a bucket to the floor");
register_param(position, "position", "swirl position coordinate, Relative center coordinate");
//register_param(randomize_swirl, "randomize_swirl", "randomize the swirling angle");
Hpage = 0;
ox = 80;
oy = 80;
done = 0;
mode = 0x4000;
BkGdImagePre = BkGdImage = BkGdImagePost = 0;
Height[0] = Height[1] = 0;
/* default physics */
density = 4;
pheight = 600;
radius = 30;
raincount = 0;
blend = 0;
fastsrand(::time(NULL));
FCreateSines();
geo = new ScreenGeometry();
geo->w = width;
geo->h = height;
geo->size = width*(height+1)*sizeof(uint32_t);
water_surfacesize = geo->size;
calc_optimization = (height)*(width);
xang = fastrand()%2048;
yang = fastrand()%2048;
swirlangle = fastrand()%2048;
/* buffer allocation tango */
if ( width*height > 0 ) {
Height[0] = (uint32_t*)calloc(width*(height+1), sizeof(uint32_t));
Height[1] = (uint32_t*)calloc(width*(height+1), sizeof(uint32_t));
}
if ( geo->size > 0 ) {
BkGdImagePre = (uint32_t*) malloc(geo->size);
BkGdImage = (uint32_t*) malloc(geo->size);
BkGdImagePost = (uint32_t*)malloc(geo->size);
}
}
~Water() {
delete geo;
free(Height[0]);
free(Height[1]);
free(BkGdImagePre);
free(BkGdImage);
free(BkGdImagePost);
}
virtual void update(double time,
uint32_t* out,
const uint32_t* in) {
memcpy(BkGdImage, in, width*height*sizeof(uint32_t));
water_update(out);
}
private:
ScreenGeometry *geo;
/* 2 pages of Height field */
uint32_t *Height[2];
/* 3 copies of the background */
uint32_t *BkGdImagePre;
uint32_t *BkGdImage;
uint32_t *BkGdImagePost;
// uint32_t *buffer;
void *surface;
/* water effect variables */
int Hpage;
int xang, yang;
int swirlangle;
int x, y, ox, oy;
int done;
int mode;
/* precalculated to optimize a bit */
int water_surfacesize;
int calc_optimization;
/* density: water density (step 1)
pheight: splash height (step 40)
radius: waterdrop radius (step 1) */
int density, pheight, radius;
int offset;
int raincount;
int blend;
void water_clear();
void water_distort();
void water_setphysics(double physics);
void water_update(uint32_t* out);
void water_drop(int x, int y);
void water_bigsplash(int x, int y);
void water_surfer();
void water_swirl();
void water_3swirls();
void DrawWater(int page,uint32_t* out);
void CalcWater(int npage, int density);
void CalcWaterBigFilter(int npage, int density);
void SmoothWater(int npage);
void HeightBlob(int x, int y, int radius, int height, int page);
void HeightBox (int x, int y, int radius, int height, int page);
void WarpBlob(int x, int y, int radius, int height, int page);
void SineBlob(int x, int y, int radius, int height, int page);
/* precalculated sinusoidal tables */
int FSinTab[2048], FCosTab[2048];
int FSin(int angle) { return FSinTab[angle&FSINMAX]; }
int FCos(int angle) { return FCosTab[angle&FSINMAX]; }
void FCreateSines() {
int i; double angle;
for(i=0; i<2048; i++) {
angle = (float)i * (PI/1024.0);
FSinTab[i] = (int)(sin(angle) * (float)0x10000);
FCosTab[i] = (int)(cos(angle) * (float)0x10000);
}
}
/* cheap & fast randomizer (by Fukuchi Kentarou) */
uint32_t randval;
uint32_t fastrand() { return (randval=randval*1103515245+12345); };
void fastsrand(uint32_t seed) { randval = seed; };
/* integer optimized square root by jaromil */
int isqrt(unsigned int x) {
unsigned int m, y, b; m = 0x40000000;
y = 0; while(m != 0) { b = y | m; y = y>>1;
if(x>=b) { x=x-b; y=y|m; }
m=m>>2; } return y;
}
};
void Water::water_clear() {
memset(Height[0], 0, water_surfacesize);
memset(Height[1], 0, water_surfacesize);
}
void Water::water_distort() {
memset(Height[Hpage], 0, water_surfacesize);
}
void Water::water_setphysics(double physics) {
if(physics<0.25) {
// case WATER:
mode |= 0x4000;
density=4;
pheight=600;
} else if(physics<0.50) {
// case JELLY:
mode &= 0xBFFF;
density=3;
pheight=400;
} else if(physics<0.75) {
// case SLUDGE:
mode &= 0xBFFF;
density=6;
pheight=400;
} else {
// case SUPER_SLUDGE:
mode &=0xBFFF;
density=8;
pheight=400;
}
}
void Water::water_update(uint32_t* out) {
if(distort && !rain) water_distort();
if(smooth) SmoothWater(Hpage);
if(rain) {
raincount++;
if(raincount>3) {
water_drop( (fastrand()%geo->w-40)+20 , (fastrand()%geo->h-40)+20 );
raincount=0;
}
}
if(swirl) water_swirl();
if(surfer) water_surfer();
DrawWater(Hpage,out);
CalcWater(Hpage^1, density);
Hpage ^=1 ;
}
void Water::water_drop(int x, int y) {
if(mode & 0x4000)
HeightBlob(x,y, radius>>2, pheight, Hpage);
else
WarpBlob(x, y, radius, pheight, Hpage);
}
void Water::water_bigsplash(int x, int y) {
if(mode & 0x4000)
HeightBlob(x, y, (radius>>1), pheight, Hpage);
else
SineBlob(x, y, radius, -pheight*6, Hpage);
}
void Water::water_surfer() {
x = (geo->w>>1)
+ ((
(
(FSin( (xang* 65) >>8) >>8) *
(FSin( (xang*349) >>8) >>8)
) * ((geo->w-8)>>1)
) >> 16);
y = (geo->h>>1)
+ ((
(
(FSin( (yang*377) >>8) >>8) *
(FSin( (yang* 84) >>8) >>8)
) * ((geo->h-8)>>1)
) >> 16);
xang += 13;
yang += 12;
if(mode & 0x4000)
{
offset = (oy+y)/2*geo->w + ((ox+x)>>1); // QUAAA
Height[Hpage][offset] = pheight;
Height[Hpage][offset + 1] =
Height[Hpage][offset - 1] =
Height[Hpage][offset + geo->w] =
Height[Hpage][offset - geo->w] = pheight >> 1;
offset = y*geo->w + x;
Height[Hpage][offset] = pheight<<1;
Height[Hpage][offset + 1] =
Height[Hpage][offset - 1] =
Height[Hpage][offset + geo->w] =
Height[Hpage][offset - geo->w] = pheight;
}
else
{
SineBlob(((ox+x)>>1), ((oy+y)>>1), 3, -1200, Hpage);
SineBlob(x, y, 4, -2000, Hpage);
}
ox = x;
oy = y;
}
void Water::water_swirl() {
x = (geo->w>>1)
+ ((
(FCos(swirlangle)) * (25)
) >> 16);
y = (geo->h>>1)
+ ((
(FSin(swirlangle)) * (25)
) >> 16);
x += position.x;
y += position.y;
swirlangle += 50;
if(mode & 0x4000)
HeightBlob(x, y, radius>>2, pheight, Hpage);
else
WarpBlob(x, y, radius, pheight, Hpage);
}
void Water::water_3swirls() {
#define ANGLE 15
x = (95)
+ ((
(FCos(swirlangle)) * (ANGLE)
) >> 16);
y = (45)
+ ((
(FSin(swirlangle)) * (ANGLE)
) >> 16);
if(mode & 0x4000) HeightBlob(x,y, radius>>2, pheight, Hpage);
else WarpBlob(x, y, radius, pheight, Hpage);
x = (95)
+ ((
(FCos(swirlangle)) * (ANGLE)
) >> 16);
y = (255)
+ ((
(FSin(swirlangle)) * (ANGLE)
) >> 16);
if(mode & 0x4000) HeightBlob(x,y, radius>>2, pheight, Hpage);
else WarpBlob(x, y, radius, pheight, Hpage);
x = (345)
+ ((
(FCos(swirlangle)) * (ANGLE)
) >> 16);
y = (150)
+ ((
(FSin(swirlangle)) * (ANGLE)
) >> 16);
if(mode & 0x4000) HeightBlob(x,y, radius>>2, pheight, Hpage);
else WarpBlob(x, y, radius, pheight, Hpage);
swirlangle += 50;
}
/* internal physics routines */
void Water::DrawWater(int page,uint32_t* out) {
int dx, dy;
int x, y;
uint32_t offset=geo->w + 1;
uint32_t newoffset;
int *ptr = (int*)&Height[page][0];
int maxoffset=geo->size/sizeof(uint32_t);
for (y = calc_optimization; offset < y; offset+=2) {
for (x = offset+geo->w-2; offset < x; offset++) {
dx = ptr[offset] - ptr[offset+1];
dy = ptr[offset] - ptr[offset+geo->w];
newoffset = offset + geo->w*(dy>>3) + (dx>>3);
if(newoffset<maxoffset)
out[offset] = BkGdImage[newoffset];
offset++;
dx = ptr[offset] - ptr[offset+1];
dy = ptr[offset] - ptr[offset+geo->w];
newoffset = offset + geo->w*(dy>>3) + (dx>>3);
if(newoffset<maxoffset)
out[offset] = BkGdImage[newoffset];
}
}
}
void Water::CalcWater(int npage, int density) {
int newh;
int count = geo->w + 1;
int *newptr = (int*) &Height[npage][0];
int *oldptr = (int*) &Height[npage^1][0];
int x, y;
for (y = calc_optimization; count < y; count += 2) {
for (x = count+geo->w-2; count < x; count++) {
/* eight pixels */
newh = ((oldptr[count + geo->w]
+ oldptr[count - geo->w]
+ oldptr[count + 1]
+ oldptr[count - 1]
+ oldptr[count - geo->w - 1]
+ oldptr[count - geo->w + 1]
+ oldptr[count + geo->w - 1]
+ oldptr[count + geo->w + 1]
) >> 2 )
- newptr[count];
newptr[count] = newh - (newh >> density);
}
}
}
void Water::SmoothWater(int npage) {
int newh;
int count = geo->w + 1;
int *newptr = (int*) &Height[npage][0];
int *oldptr = (int*) &Height[npage^1][0];
int x, y;
for(y=1; y<geo->h-1; y++) {
for(x=1; x<geo->w-1; x++) {
/* eight pixel */
newh = ((oldptr[count + geo->w]
+ oldptr[count - geo->w]
+ oldptr[count + 1]
+ oldptr[count - 1]
+ oldptr[count - geo->w - 1]
+ oldptr[count - geo->w + 1]
+ oldptr[count + geo->w - 1]
+ oldptr[count + geo->w + 1]
) >> 3 )
+ newptr[count];
newptr[count] = newh>>1;
count++;
}
count += 2;
}
}
void Water::CalcWaterBigFilter(int npage, int density) {
int newh;
int count = (geo->w<<1) + 2;
int *newptr = (int*) &Height[npage][0];
int *oldptr = (int*) &Height[npage^1][0];
int x, y;
for(y=2; y<geo->h-2; y++) {
for(x=2; x<geo->w-2; x++) {
/* 25 pixels */
newh = (
(
(
(oldptr[count + geo->w]
+ oldptr[count - geo->w]
+ oldptr[count + 1]
+ oldptr[count - 1]
)<<1)
+ ((oldptr[count - geo->w - 1]
+ oldptr[count - geo->w + 1]
+ oldptr[count + geo->w - 1]
+ oldptr[count + geo->w + 1]))
+ ( (
oldptr[count - (geo->w<<1)]
+ oldptr[count + (geo->w<<1)]
+ oldptr[count - 2]
+ oldptr[count + 2]
) >> 1 )
+ ( (
oldptr[count - (geo->w<<1) - 1]
+ oldptr[count - (geo->w<<1) + 1]
+ oldptr[count + (geo->w<<1) - 1]
+ oldptr[count + (geo->w<<1) + 1]
+ oldptr[count - 2 - geo->w]
+ oldptr[count - 2 + geo->w]
+ oldptr[count + 2 - geo->w]
+ oldptr[count + 2 + geo->w]
) >> 2 )
)
>> 3)
- (newptr[count]);
newptr[count] = newh - (newh >> density);
count++;
}
count += 4;
}
}
void Water::HeightBlob(int x, int y, int radius, int height, int page) {
int rquad;
int cx, cy, cyq;
int left, top, right, bottom;
rquad = radius * radius;
/* Make a randomly-placed blob... */
if(x<0) x = 1+radius+ fastrand()%(geo->w-2*radius-1);
if(y<0) y = 1+radius+ fastrand()%(geo->h-2*radius-1);
left=-radius; right = radius;
top=-radius; bottom = radius;
CLIP_EDGES
for(cy = top; cy < bottom; cy++) {
cyq = cy*cy;
for(cx = left; cx < right; cx++) {
if(cx*cx + cyq < rquad)
Height[page][geo->w*(cy+y) + (cx+x)] += height;
}
}
}
void Water::HeightBox (int x, int y, int radius, int height, int page) {
int cx, cy;
int left, top, right, bottom;
if(x<0) x = 1+radius+ fastrand()%(geo->w-2*radius-1);
if(y<0) y = 1+radius+ fastrand()%(geo->h-2*radius-1);
left=-radius; right = radius;
top=-radius; bottom = radius;
CLIP_EDGES
for(cy = top; cy < bottom; cy++) {
for(cx = left; cx < right; cx++) {
Height[page][geo->w*(cy+y) + (cx+x)] = height;
}
}
}
void Water::WarpBlob(int x, int y, int radius, int height, int page) {
int cx, cy;
int left,top,right,bottom;
int square;
int radsquare = radius * radius;
radsquare = (radius*radius);
height = height>>5;
left=-radius; right = radius;
top=-radius; bottom = radius;
CLIP_EDGES
for(cy = top; cy < bottom; cy++) {
for(cx = left; cx < right; cx++) {
square = cy*cy + cx*cx;
if(square < radsquare) {
Height[page][geo->w*(cy+y) + cx+x]
+= (int)((radius-isqrt(square))*(float)(height));
}
}
}
}
void Water::SineBlob(int x, int y, int radius, int height, int page) {
int cx, cy;
int left,top,right,bottom;
int square, dist;
int radsquare = radius * radius;
float length = (1024.0/(float)radius)*(1024.0/(float)radius);
if(x<0) x = 1+radius+ fastrand()%(geo->w-2*radius-1);
if(y<0) y = 1+radius+ fastrand()%(geo->h-2*radius-1);
radsquare = (radius*radius);
left=-radius; right = radius;
top=-radius; bottom = radius;
CLIP_EDGES
for(cy = top; cy < bottom; cy++) {
for(cx = left; cx < right; cx++) {
square = cy*cy + cx*cx;
if(square < radsquare) {
dist = (int)(isqrt(square*length));
Height[page][geo->w*(cy+y) + cx+x]
+= (int)((FCos(dist)+0xffff)*(height)) >> 19;
}
}
}
}
frei0r::construct<Water> plugin("Water",
"water drops on a video surface",
"Jaromil",
3,0);