-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionpotential3.pde
614 lines (466 loc) · 17.5 KB
/
actionpotential3.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
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
import java.util.Locale;
import com.jogamp.opengl.GL3;
import com.thomasdiewald.pixelflow.java.DwPixelFlow;
import com.thomasdiewald.pixelflow.java.flowfieldparticles.DwFlowFieldParticles;
import com.thomasdiewald.pixelflow.java.imageprocessing.DwFlowField;
import com.thomasdiewald.pixelflow.java.imageprocessing.filter.DwFilter;
import com.thomasdiewald.pixelflow.java.imageprocessing.filter.Merge;
import com.thomasdiewald.pixelflow.java.utils.DwUtils;
import controlP5.Accordion;
import controlP5.CColor;
import controlP5.ControlP5;
import controlP5.Group;
import controlP5.RadioButton;
import controlP5.Toggle;
import processing.core.*;
import processing.opengl.PGraphics2D;
import processing.opengl.PJOGL;
//
// Line Integral Convolution (LIC) Shader
//
// LIC is a low-pass filter, for sampling and averaging samples along the
// streamlines of a flowfield (vector field).
//
//
// LIC:
//
// In this demo the flowfield is created by some simply mouse-drawing.
// The background texture the sample are taken from should contain
// high-frequency noise for the LIC-Shader to work best.
//
//
// StreamLines:
//
// Additionally to the LIC filter, the streamlines are computed and rendered.
// This is quite expensive, since a lot of lines are required and each line
// needs a reasonable amount of vertices (10-40).
// All these vertices need to be generated on the fly by an alternating
// update and render pass.
// In the beginning for each line a particle is spawned and during the update
// passes the particles positions are updated using verlet integration.
// In the successive render pass, the last particle-step is rendered as a line.
//
//
boolean START_FULLSCREEN = !true;
//int viewport_w = 1680;
//int viewport_h = 1024;
int viewport_w = 1280;
int viewport_h = 720;
int viewport_x = 230;
int viewport_y = 0;
int gui_w = 200;
int gui_x = 10;
int gui_y = 10;
KinectStream k;
PGraphics2D pg_canvas;
PGraphics3D debug_canvas;
PGraphics2D pg_impulse;
PGraphics2D pg_noise;
PGraphics2D pg_tmp;
DwPixelFlow context;
DwFlowField ff_impulse;
DwFlowFieldParticles particles_stream;
int STREAMLINE_SAMPLES = 15;
int STREAMLINE_RES = 10;
int DISPLAY_MODE = 1;
boolean APPLY_IMPULSE = false;
boolean DISPLAY_STREAMLINES = !true;
boolean DISPLAY_PARTICLES = !true;
boolean BLUR_ITERATIONS = true;
float impulse_max = 256;
float impulse_mul = 10;
float impulse_tsmooth = 1f;
int impulse_radius = 30;
public void settings() {
if (START_FULLSCREEN) {
viewport_w = displayWidth;
viewport_h = displayHeight;
viewport_x = 0;
viewport_y = 0;
fullScreen(P2D);
} else {
viewport_w = (int) min(viewport_w, displayWidth * 0.9f);
viewport_h = (int) min(viewport_h, displayHeight * 0.9f);
size(viewport_w, viewport_h, P3D);
}
PJOGL.profile = 3;
//smooth(8);
}
public void setup() {
k = new KinectStream(this);
surface.setLocation(viewport_x, viewport_y);
surface.setResizable(true);
context = new DwPixelFlow(this);
context.print();
context.printGL();
ff_impulse = new DwFlowField(context);
ff_impulse.param.blur_iterations = 1;
ff_impulse.param.blur_radius = 6;
ff_impulse.param_lic.iterations = 5;
ff_impulse.param_lic.num_samples = 5;
ff_impulse.param_lic.acc_mult = 1.69f;
ff_impulse.param_lic.vel_mult = 1.15f;
ff_impulse.param_lic.intensity_mult = 1.80f;
ff_impulse.param_lic.intensity_exp = 0.57f;
ff_impulse.param_lic.TRACE_BACKWARD = true;
ff_impulse.param_lic.TRACE_FORWARD = false;
particles_stream = new DwFlowFieldParticles(context);
particles_stream.param.blend_mode = 1; // 1 = additive blending
particles_stream.param.shader_collision_mult = 0.00f;
particles_stream.param.display_line_width = 1f;
particles_stream.param.display_line_smooth = false;
particles_stream.param.steps = 1;
particles_stream.param.mul_obs = 0.0f;
particles_stream.param.mul_col = 0.0f;
particles_stream.param.mul_coh = 0.0f;
particles_stream.param.mul_acc = +5.0f;
particles_stream.param.velocity_damping = 0.30f;
particles_stream.param.acc_minmax[1] = 120;
particles_stream.param.vel_minmax[1] = 120;
particles_stream.param.size_display = 10;
particles_stream.param.size_collision = 0;
particles_stream.param.size_cohesion = 0;
particles_stream.param.mul_coh = 0.00f;
particles_stream.param.mul_col = 0.00f;
particles_stream.param.mul_obs = 0.00f;
particles_stream.param.wh_scale_col = 0;
particles_stream.param.wh_scale_coh = 5;
particles_stream.param.wh_scale_obs = 0;
resizeScene();
createGUI();
frameRate(100);
}
public void resizeScene() {
if (pg_canvas != null && width == pg_canvas.width && height == pg_canvas.height) {
return;
}
pg_canvas = (PGraphics2D) createGraphics(width, height, P2D);
pg_canvas.smooth(0);
debug_canvas = (PGraphics3D) createGraphics(width, height, P3D);
pg_tmp = (PGraphics2D) createGraphics(width, height, P2D);
pg_tmp.smooth(0);
DwUtils.changeTextureFormat(pg_canvas, GL3.GL_RGBA16F, GL3.GL_RGBA, GL3.GL_FLOAT, GL3.GL_LINEAR);
DwUtils.changeTextureFormat(pg_tmp, GL3.GL_RGBA16F, GL3.GL_RGBA, GL3.GL_FLOAT, GL3.GL_LINEAR);
pg_canvas.beginDraw();
pg_canvas.endDraw();
pg_impulse = (PGraphics2D) createGraphics(width, height, P2D);
pg_impulse.smooth(0);
DwUtils.COL_TL = new float[]{ 0, 0, 0, 255};
DwUtils.COL_TR = new float[]{ 32, 64, 128, 255};
DwUtils.COL_BL = new float[]{ 128, 64, 32, 255};
DwUtils.COL_BR = new float[]{ 0, 0, 0, 255};
pg_noise = DwUtils.createBackgroundNoiseTexture(this, width/2, height/2);
resetScene();
}
public void resetScene() {
pg_canvas.beginDraw();
pg_canvas.clear();
pg_canvas.endDraw();
}
//////////////////////////////////////////////////////////////////////////////
//
// DRAW
//
//////////////////////////////////////////////////////////////////////////////
public void addImpulse(MovingVector mv) {
//APPLY_IMPULSE = mousePressed && !cp5.isMouseOver();
final int MID = 127;
float mx, my, pmx, pmy, vx, vy;
pg_impulse.beginDraw();
pg_impulse.background(MID, MID, MID);
float mult = 200f / impulse_radius;
if (useMouse) {
mx = mouseX;
my = mouseY;
pmx = pmouseX;
pmy = pmouseY;
} else {
mx = mv.c.x + width / 2;
my = height - (mv.c.y + height / 2);
// mouse position and velocity
if (mv.p != null) {
pmx = mv.p.x + width / 2;
pmy = height - (mv.p.y + height / 2);
} else {
pmx = width / 2;
pmy = height / 2;
}
}
vx = (mx - pmx) * +impulse_mul * mult;
vy = (my - pmy) * -impulse_mul * mult; // flip vertically
// clamp velocity
float vv_sq = vx*vx + vy*vy;
float vv_sq_max = impulse_max*impulse_max;
if (vv_sq > vv_sq_max && vv_sq > 0.0) {
float vv = sqrt(vv_sq);
vx = impulse_max * vx / vv;
vy = impulse_max * vy / vv;
}
// map velocity, to UNSIGNED_BYTE range
vx = map(vx, -impulse_max, +impulse_max, 0, MID<<1);
vy = map(vy, -impulse_max, +impulse_max, 0, MID<<1);
// render impulse
//if (APPLY_IMPULSE) {
pg_impulse.noStroke();
pg_impulse.fill(vx, vy, MID);
pg_impulse.ellipse(mx, my, impulse_radius, impulse_radius);
//}
pg_impulse.endDraw();
// create impulse texture
ff_impulse.resize(width, height);
{
Merge.TexMad ta = new Merge.TexMad(ff_impulse.tex_vel, impulse_tsmooth, 0);
Merge.TexMad tb = new Merge.TexMad(pg_impulse, 1, -MID/255f);
DwFilter.get(context).merge.apply(ff_impulse.tex_vel, ta, tb);
ff_impulse.blur();
}
}
public void draw() {
resizeScene();
if (useMouse) {
addImpulse(null);
} else {
for (MovingVector hand : k.vectorsFor(new int[] {
KinectPV2.JointType_HandLeft,
KinectPV2.JointType_HandRight,
KinectPV2.JointType_FootRight,
KinectPV2.JointType_FootLeft
}
)) {
addImpulse(hand);
}
}
resetScene();
if (DISPLAY_MODE == 0) {
ff_impulse.displayPixel(pg_canvas);
ff_impulse.displayLines(pg_canvas);
}
if (DISPLAY_MODE == 1) {
ff_impulse.displayLineIntegralConvolution(pg_canvas, pg_noise);
}
if (DISPLAY_STREAMLINES || DISPLAY_PARTICLES) {
int iterations = STREAMLINE_SAMPLES;
int dim_x = width;
int dim_y = height;
int num_x = ceil(dim_x / STREAMLINE_RES);
int num_y = ceil(dim_y / STREAMLINE_RES);
DwFlowFieldParticles.SpawnRect sr = new DwFlowFieldParticles.SpawnRect();
sr.num = new int[]{num_x, num_y};
sr.dim = new float[]{dim_x, dim_y};
sr.pos = new float[]{0, 0};
sr.vel = new float[]{0, 0};
particles_stream.resizeParticlesCount(num_x, num_y);
particles_stream.resizeWorld(dim_x, dim_y);
particles_stream.reset();
particles_stream.spawn(dim_x, dim_y, sr);
particles_stream.param.shader_type = 0;
particles_stream.param.size_display = (int) max(2, (0.55f * min(dim_x/(float)num_x, dim_y/(float)num_y)));
float s1 = 0.40f;
float s2 = 0.40f;
float[] col_A = {0.25f*s1, 0.50f*s1, 1.00f*s1, 1.00f};
float[] col_B = {1.00f*s2, 0.10f*s2, 0.50f*s2, 0.50f};
float step = 1f / iterations;
for (int i = 0; i < iterations; i++) {
if (BLUR_ITERATIONS && i > 0) {
DwFilter.get(context).gaussblur.apply(pg_canvas, pg_canvas, pg_tmp, 1);
// float mult = 0.95f;
// DwFilter.get(context).multiply.apply(pg_canvas, pg_canvas, new float[]{mult,mult,mult,mult});
}
particles_stream.update(ff_impulse);
float line_uv = 1 - i * step;
line_uv = (float) Math.pow(line_uv, 2);
DwUtils.mix(col_A, col_B, line_uv, particles_stream.param.col_A);
if (DISPLAY_STREAMLINES) {
particles_stream.displayTrail(pg_canvas);
}
if (DISPLAY_PARTICLES) {
float p1 = 2.0f;
float p2 = 1f;
particles_stream.param.col_A = new float[]{0.20f*p1, 0.05f*p1, 0.02f*p1, 0.50f};
particles_stream.param.col_B = new float[]{0.00f*p2, 0.00f*p2, 0.00f*p2, 0.00f};
particles_stream.displayParticles(pg_canvas);
}
}
}
blendMode(REPLACE);
image(pg_canvas, 0, 0);
blendMode(BLEND);
if (showDebug) {
debug_canvas.beginDraw();
debug_canvas.clear();
k.drawDebugs(debug_canvas);
debug_canvas.endDraw();
image(debug_canvas, 0, 0);
}
info();
}
void info() {
String txt_app = getClass().getSimpleName();
String txt_device = context.gl.glGetString(GL3.GL_RENDERER).trim().split("/")[0];
String txt_fps = String.format(Locale.ENGLISH, "[%s] [%s] [%d/%d] [%7.2f fps]",
txt_app, txt_device, pg_canvas.width, pg_canvas.height, frameRate);
surface.setTitle(txt_fps);
}
public void keyReleased() {
if (key == 'r') reset();
if (key == 'h') toggleGUI();
if (key == 'd') toggleDebugs();
if (key == 'm') toggleMouse();
if (key >= '1' && key <= '9') DISPLAY_MODE = key - '1';
}
public void reset() {
ff_impulse.reset();
particles_stream.reset();
resetScene();
}
public void setDisplayType(int val) {
DISPLAY_MODE = val;
}
public void setLicStates(float[] val) {
ff_impulse.param_lic.TRACE_BACKWARD = val[0] > 0;
ff_impulse.param_lic.TRACE_FORWARD = val[1] > 0;
}
public void setDisplayStreamLine(float[] val) {
DISPLAY_STREAMLINES = val[0] > 0;
DISPLAY_PARTICLES = val[1] > 0;
}
boolean useMouse = false;
public void toggleMouse() {
useMouse = !useMouse;
println(useMouse);
}
boolean showDebug = true;
public void toggleDebugs() {
showDebug = !showDebug;
}
public void toggleGUI() {
if (cp5.isVisible()) cp5.hide();
else cp5.show();
}
public void setStreamLineStates(float[] val) {
particles_stream.param.display_line_smooth = val[0] > 0;
BLUR_ITERATIONS = val[1] > 0;
}
float mult_fg = 1f;
float mult_active = 2f;
float CR = 96;
float CG = 64;
float CB = 8;
int col_bg, col_fg, col_active;
ControlP5 cp5;
public void createGUI() {
col_bg = color(4, 220);
col_fg = color(CR*mult_fg, CG*mult_fg, CB*mult_fg);
col_active = color(CR*mult_active, CG*mult_active, CB*mult_active);
int col_group = color(8, 64);
CColor theme = ControlP5.getColor();
theme.setForeground(col_fg);
theme.setBackground(col_bg);
theme.setActive(col_active);
cp5 = new ControlP5(this);
cp5.setAutoDraw(true);
int sx, sy, px, py;
sx = 100;
sy = 14;
int dy_group = 20;
int dy_item = 4;
////////////////////////////////////////////////////////////////////////////
// GUI - LIC
////////////////////////////////////////////////////////////////////////////
Group group_lic = cp5.addGroup("Line Integral Convolution");
{
group_lic.setHeight(20).setSize(gui_w, 330)
.setBackgroundColor(col_group).setColorBackground(col_group);
group_lic.getCaptionLabel().align(CENTER, CENTER);
px = 15;
py = 15;
cp5.addButton("reset").setGroup(group_lic).plugTo(this, "reset").setSize(80, 18).setPosition(px, py);
{
py += 1 * sy + dy_group;
int count = 2;
sx = (gui_w-30 - 2 * (count-1)) / count;
RadioButton rb_type = cp5.addRadio("setDisplayType").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setSpacingColumn(2).setSpacingRow(2).setItemsPerRow(count).plugTo(this, "setDisplayType")
.setNoneSelectedAllowed(true)
.addItem("flow field", 0)
.addItem("LIC", 1)
.activate(DISPLAY_MODE);
for (Toggle toggle : rb_type.getItems()) toggle.getCaptionLabel().alignX(CENTER).alignY(CENTER);
py += sy + dy_group;
}
DwFlowField.ParamLIC param = ff_impulse.param_lic;
cp5.addSlider("iterations").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(1, 20).setValue(param.iterations).plugTo(param, "iterations");
py += sy + dy_item;
cp5.addSlider("samples").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(1, 120).setValue(param.num_samples).plugTo(param, "num_samples");
py += sy + dy_item;
cp5.addSlider("blur radius").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(0, 10).setValue(ff_impulse.param.blur_radius).plugTo(ff_impulse.param, "blur_radius");
py += sy + dy_item;
cp5.addSlider("acc_mult").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(0, 5).setValue(param.acc_mult).plugTo(param, "acc_mult");
py += sy + dy_item;
cp5.addSlider("vel_mult").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(0, 3).setValue(param.vel_mult).plugTo(param, "vel_mult");
py += sy + dy_item;
cp5.addSlider("intensity_exp").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(0.0f, 3f).setValue(param.intensity_exp).plugTo(param, "intensity_exp");
py += sy + dy_item;
cp5.addSlider("intensity_mult").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(0.0f, 2.5f).setValue(param.intensity_mult).plugTo(param, "intensity_mult");
py += sy + dy_group;
cp5.addCheckBox("setLicStates").setGroup(group_lic).setSize(sy, sy).setPosition(px, py)
.setSpacingColumn(2).setSpacingRow(2).setItemsPerRow(1)
.addItem("TRACE BACKWARD", 0).activate(param.TRACE_BACKWARD ? 0 : 2)
.addItem("TRACE FORWARD", 1).activate(param.TRACE_FORWARD ? 1 : 2)
;
int count = 2;
py += sy * count + 2 * (count-1) + dy_group;
cp5.addSlider("brush").setGroup(group_lic).setSize(sx, sy).setPosition(px, py)
.setRange(0, 500).setValue(this.impulse_radius).plugTo(this, "impulse_radius");
py += sy + dy_item;
}
////////////////////////////////////////////////////////////////////////////
// GUI - STREAMLINES
////////////////////////////////////////////////////////////////////////////
Group group_streamlines = cp5.addGroup("StreamLines");
{
group_streamlines.setHeight(20).setSize(gui_w, 200)
.setBackgroundColor(col_group).setColorBackground(col_group);
group_streamlines.getCaptionLabel().align(CENTER, CENTER);
px = 15;
py = 15;
int count = 1;
cp5.addCheckBox("setDisplayStreamLine").setGroup(group_streamlines).setSize(sy, sy).setPosition(px, py)
.setSpacingColumn(2).setSpacingRow(2).setItemsPerRow(count).plugTo(this, "setDisplayStreamLine")
.addItem("STREAMLINE", 0).activate(DISPLAY_STREAMLINES ? 0 : 2)
.addItem("PARTICLES", 1).activate(DISPLAY_PARTICLES ? 1 : 2)
;
py += 2 * sy + dy_group;
DwFlowFieldParticles.Param param = particles_stream.param;
cp5.addSlider("StreamLine.samples").setLabel("samples").setGroup(group_streamlines).setSize(sx, sy).setPosition(px, py)
.setRange(5, 100).setValue(STREAMLINE_SAMPLES).plugTo(this, "STREAMLINE_SAMPLES");
py += sy + dy_item;
cp5.addSlider("StreamLine.res").setLabel("resolution").setGroup(group_streamlines).setSize(sx, sy).setPosition(px, py)
.setRange(3, 20).setValue(STREAMLINE_RES).plugTo(this, "STREAMLINE_RES");
py += sy + dy_item;
cp5.addSlider("StreamLine.vel_mult").setLabel("vel_mult").setGroup(group_streamlines).setSize(sx, sy).setPosition(px, py)
.setRange(0, 1).setValue(param.velocity_damping).plugTo(param, "velocity_damping");
py += sy + dy_item;
cp5.addSlider("StreamLine.acc_mult").setLabel("acc_mult").setGroup(group_streamlines).setSize(sx, sy).setPosition(px, py)
.setRange(-10, 10).setValue(param.mul_acc).plugTo(param, "mul_acc");
py += sy + dy_group;
cp5.addCheckBox("setStreamLineStates").setGroup(group_streamlines).setSize(sy, sy).setPosition(px, py)
.setSpacingColumn(2).setSpacingRow(2).setItemsPerRow(1)
.addItem("smooth lines", 0).activate(param.display_line_smooth ? 0 : 2)
.addItem("blur lines", 0).activate(BLUR_ITERATIONS ? 1 : 2)
;
}
cp5.addAccordion("acc").setPosition(gui_x, gui_y).setWidth(gui_w).setSize(gui_w, height)
.setCollapseMode(Accordion.MULTI)
.addItem(group_lic)
.addItem(group_streamlines)
.open()
;
}