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
| import com.thomasdiewald.pixelflow.java.DwPixelFlow; import com.thomasdiewald.pixelflow.java.fluid.DwFluid2D;
import controlP5.Accordion; import controlP5.ControlP5; import controlP5.Group; import controlP5.RadioButton; import controlP5.Toggle; import processing.core.*; import processing.opengl.PGraphics2D; import processing.opengl.PJOGL;
import java.util.ArrayList; import KinectPV2.KJoint; import KinectPV2.*;
KinectPV2 kinect; private class MyFluidData implements DwFluid2D.FluidData{ @Override public void update(DwFluid2D fluid) { float tempX,tempY,tempX2,tempY2,px, py, vx, vy, radius, vscale, temperature; int hstate; int hstate2; radius = 15; vscale = 10; px = width/2; tempX = width/2; tempX2 = width/2; py = 50; tempY = 50; tempY2 = 50; vx = 1 * +vscale; vy = 1 * vscale; radius = 40; temperature = 1f; hstate = KinectPV2.HandState_NotTracked; hstate2 = KinectPV2.HandState_NotTracked;
ArrayList<KSkeleton> skeletonArray = kinect.getSkeletonDepthMap(); for (int i = 0; i < skeletonArray.size(); i++) { KSkeleton skeleton = (KSkeleton) skeletonArray.get(i); if (skeleton.isTracked()) { KJoint[] joints = skeleton.getJoints(); KJoint RightHand = joints[KinectPV2.JointType_HandRight]; KJoint LeftHand = joints[KinectPV2.JointType_HandLeft]; tempX = RightHand.getX(); tempY = height - RightHand.getY(); hstate = RightHand.getState();
tempX2 = LeftHand.getX(); tempY2 = height - LeftHand.getY(); hstate2 = LeftHand.getState(); } }
fluid.addDensity(px, py, radius, 0.2f, 0.3f, 0.5f, 1.0f); fluid.addTemperature(px, py, radius, temperature); particles.spawn(fluid, px, py, radius, 100); boolean mouse_input = !cp5.isMouseOver() && mousePressed; switch(hstate2){ case KinectPV2.HandState_Closed : fluid.addDensity(tempX2, tempY2, radius, 0.2f, 0.3f, 0.5f, 1.0f); fluid.addTemperature(tempX2, tempY2, radius, temperature); particles.spawn(fluid, tempX2, tempY2, radius, 100); break; case KinectPV2.HandState_Open : radius = 15; vscale = 15; vx = +3*vscale; vy = -3*vscale; px = tempX2; py = tempY2; fluid.addDensity (px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f); fluid.addVelocity(px, py, radius, vx, vy); particles.spawn(fluid, px, py, radius*2, 300); break; }
switch(hstate){ case KinectPV2.HandState_Closed : fluid.addDensity(tempX, tempY, radius, 0.2f, 0.3f, 0.5f, 1.0f); fluid.addTemperature(tempX, tempY, radius, temperature); particles.spawn(fluid, tempX, tempY, radius, 100); break; case KinectPV2.HandState_Open : radius = 15; vscale = 15; vx = +3*vscale; vy = -3*vscale; px = tempX; py = tempY; fluid.addDensity (px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f); fluid.addVelocity(px, py, radius, vx, vy); particles.spawn(fluid, px, py, radius*2, 300); break; }
if(mouse_input && mouseButton == LEFT){ radius = 15; vscale = 15; px = mouseX; py = height-mouseY; vx = (mouseX - pmouseX) * +vscale; vy = (mouseY - pmouseY) * -vscale;
fluid.addDensity (px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f); fluid.addVelocity(px, py, radius, vx, vy); particles.spawn(fluid, px, py, radius*2, 300); } if(mouse_input && mouseButton == LEFT){ radius = 15; vscale = 15; px = mouseX; py = height-mouseY; vx = (mouseX - pmouseX) * +vscale; vy = (mouseY - pmouseY) * -vscale; fluid.addDensity (px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f); fluid.addVelocity(px, py, radius, vx, vy); particles.spawn(fluid, px, py, radius*2, 300); } if(mouse_input && mouseButton == CENTER){ radius = 15; vscale = 15; px = mouseX; py = height-mouseY; temperature = 2f; fluid.addDensity(px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f); fluid.addTemperature(px, py, radius, temperature); particles.spawn(fluid, px, py, radius, 100); } if(mouse_input && mouseButton == RIGHT){ px = mouseX; py = height - 1 - mouseY; radius = 50; particles.spawn(fluid, px, py, radius, 300); } } }
int viewport_w = 512; int viewport_h = 424; int viewport_x = 230; int viewport_y = 0;
int gui_w = 200; int gui_x = 20; int gui_y = 20; KJoint[] joints; int fluidgrid_scale = 3;
DwFluid2D fluid;
PGraphics2D pg_fluid;
PGraphics2D pg_obstacles;
MyParticleSystem particles;
int BACKGROUND_COLOR = 0; boolean UPDATE_FLUID = true; boolean DISPLAY_FLUID_TEXTURES = false; boolean DISPLAY_FLUID_VECTORS = false; int DISPLAY_fluid_texture_mode = 0; boolean DISPLAY_PARTICLES = true;
public void settings() { size(viewport_w, viewport_h, P2D); smooth(4); PJOGL.profile = 3; }
public void setup() { kinect = new KinectPV2(this); kinect.enableDepthMaskImg(true); kinect.enableSkeletonDepthMap(true); kinect.init(); surface.setLocation(viewport_x, viewport_y); DwPixelFlow context = new DwPixelFlow(this); context.print(); context.printGL();
fluid = new DwFluid2D(context, viewport_w, viewport_h, fluidgrid_scale);
fluid.param.dissipation_density = 0.999f; fluid.param.dissipation_velocity = 0.99f; fluid.param.dissipation_temperature = 0.80f; fluid.param.vorticity = 0.10f; MyFluidData cb_fluid_data = new MyFluidData(); fluid.addCallback_FluiData(cb_fluid_data); pg_fluid = (PGraphics2D) createGraphics(viewport_w, viewport_h, P2D); pg_fluid.smooth(4); pg_fluid.beginDraw(); pg_fluid.background(BACKGROUND_COLOR); pg_fluid.endDraw(); pg_obstacles = (PGraphics2D) createGraphics(viewport_w, viewport_h, P2D); pg_obstacles.smooth(4); pg_obstacles.beginDraw(); pg_obstacles.clear(); float radius; radius = 200; pg_obstacles.stroke(64); pg_obstacles.strokeWeight(10); pg_obstacles.noFill(); pg_obstacles.rect(1*width/2f, 1*height/4f, radius, radius, 20); pg_obstacles.strokeWeight(20); pg_obstacles.stroke(64); pg_obstacles.noFill(); pg_obstacles.rect(0, 0, pg_obstacles.width, pg_obstacles.height); pg_obstacles.endDraw(); fluid.addObstacles(pg_obstacles); particles = new MyParticleSystem(context, 1000 * 1000);
createGUI(); background(0); frameRate(60); }
public void draw() { if(UPDATE_FLUID){ fluid.addObstacles(pg_obstacles); fluid.update(); particles.update(fluid); } pg_fluid.beginDraw(); pg_fluid.background(BACKGROUND_COLOR); pg_fluid.endDraw(); image(kinect.getDepthMaskImage(), 0, 0);
if(DISPLAY_FLUID_TEXTURES){ fluid.renderFluidTextures(pg_fluid, DISPLAY_fluid_texture_mode); } if(DISPLAY_FLUID_VECTORS){ fluid.renderFluidVectors(pg_fluid, 10); } if( DISPLAY_PARTICLES){ particles.render(pg_fluid, BACKGROUND_COLOR); }
image(pg_fluid , 0, 0); image(pg_obstacles, 0, 0); }
public void fluid_resizeUp(){ fluid.resize(width, height, fluidgrid_scale = max(1, --fluidgrid_scale)); } public void fluid_resizeDown(){ fluid.resize(width, height, ++fluidgrid_scale); } public void fluid_reset(){ fluid.reset(); } public void fluid_togglePause(){ UPDATE_FLUID = !UPDATE_FLUID; } public void fluid_displayMode(int val){ DISPLAY_fluid_texture_mode = val; DISPLAY_FLUID_TEXTURES = DISPLAY_fluid_texture_mode != -1; } public void fluid_displayVelocityVectors(int val){ DISPLAY_FLUID_VECTORS = val != -1; }
public void fluid_displayParticles(int val){ DISPLAY_PARTICLES = val != -1; }
public void keyReleased(){ if(key == 'p') fluid_togglePause(); if(key == '+') fluid_resizeUp(); if(key == '-') fluid_resizeDown(); if(key == 'r') fluid_reset(); if(key == '1') DISPLAY_fluid_texture_mode = 0; if(key == '2') DISPLAY_fluid_texture_mode = 1; if(key == '3') DISPLAY_fluid_texture_mode = 2; if(key == '4') DISPLAY_fluid_texture_mode = 3; if(key == 'q') DISPLAY_FLUID_TEXTURES = !DISPLAY_FLUID_TEXTURES; if(key == 'w') DISPLAY_FLUID_VECTORS = !DISPLAY_FLUID_VECTORS; }
void drawBody(KJoint[] joints) { drawBone(joints, KinectPV2.JointType_Head, KinectPV2.JointType_Neck); drawBone(joints, KinectPV2.JointType_Neck, KinectPV2.JointType_SpineShoulder); drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_SpineMid); drawBone(joints, KinectPV2.JointType_SpineMid, KinectPV2.JointType_SpineBase); drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_ShoulderRight); drawBone(joints, KinectPV2.JointType_SpineShoulder, KinectPV2.JointType_ShoulderLeft); drawBone(joints, KinectPV2.JointType_SpineBase, KinectPV2.JointType_HipRight); drawBone(joints, KinectPV2.JointType_SpineBase, KinectPV2.JointType_HipLeft);
drawBone(joints, KinectPV2.JointType_ShoulderRight, KinectPV2.JointType_ElbowRight); drawBone(joints, KinectPV2.JointType_ElbowRight, KinectPV2.JointType_WristRight); drawBone(joints, KinectPV2.JointType_WristRight, KinectPV2.JointType_HandRight); drawBone(joints, KinectPV2.JointType_HandRight, KinectPV2.JointType_HandTipRight); drawBone(joints, KinectPV2.JointType_WristRight, KinectPV2.JointType_ThumbRight);
drawBone(joints, KinectPV2.JointType_ShoulderLeft, KinectPV2.JointType_ElbowLeft); drawBone(joints, KinectPV2.JointType_ElbowLeft, KinectPV2.JointType_WristLeft); drawBone(joints, KinectPV2.JointType_WristLeft, KinectPV2.JointType_HandLeft); drawBone(joints, KinectPV2.JointType_HandLeft, KinectPV2.JointType_HandTipLeft); drawBone(joints, KinectPV2.JointType_WristLeft, KinectPV2.JointType_ThumbLeft);
drawBone(joints, KinectPV2.JointType_HipRight, KinectPV2.JointType_KneeRight); drawBone(joints, KinectPV2.JointType_KneeRight, KinectPV2.JointType_AnkleRight); drawBone(joints, KinectPV2.JointType_AnkleRight, KinectPV2.JointType_FootRight);
drawBone(joints, KinectPV2.JointType_HipLeft, KinectPV2.JointType_KneeLeft); drawBone(joints, KinectPV2.JointType_KneeLeft, KinectPV2.JointType_AnkleLeft); drawBone(joints, KinectPV2.JointType_AnkleLeft, KinectPV2.JointType_FootLeft);
drawJoint(joints, KinectPV2.JointType_HandTipLeft); drawJoint(joints, KinectPV2.JointType_HandTipRight); drawJoint(joints, KinectPV2.JointType_FootLeft); drawJoint(joints, KinectPV2.JointType_FootRight);
drawJoint(joints, KinectPV2.JointType_ThumbLeft); drawJoint(joints, KinectPV2.JointType_ThumbRight);
drawJoint(joints, KinectPV2.JointType_Head); }
void drawJoint(KJoint[] joints, int jointType) { pushMatrix(); translate(joints[jointType].getX(), joints[jointType].getY(), joints[jointType].getZ()); ellipse(0, 0, 25, 25); popMatrix(); }
void drawBone(KJoint[] joints, int jointType1, int jointType2) { pushMatrix(); translate(joints[jointType1].getX(), joints[jointType1].getY(), joints[jointType1].getZ()); ellipse(0, 0, 25, 25); popMatrix(); line(joints[jointType1].getX(), joints[jointType1].getY(), joints[jointType1].getZ(), joints[jointType2].getX(), joints[jointType2].getY(), joints[jointType2].getZ()); }
void drawHandState(KJoint joint) { noStroke(); handState(joint.getState()); pushMatrix(); translate(joint.getX(), joint.getY(), joint.getZ()); println(joint.getX(), joint.getY(), joint.getZ()); ellipse(0, 0, 70, 70); popMatrix(); }
void handState(int handState) { switch(handState) { case KinectPV2.HandState_Open: fill(0, 255, 0); break; case KinectPV2.HandState_Closed: fill(255, 0, 0); break; case KinectPV2.HandState_Lasso: fill(0, 0, 255); break; case KinectPV2.HandState_NotTracked: fill(100, 100, 100); break; } }
ControlP5 cp5;
public void createGUI(){ cp5 = new ControlP5(this); int sx, sy, px, py, oy; sx = 100; sy = 14; oy = (int)(sy*1.5f);
Group group_fluid = cp5.addGroup("fluid"); { group_fluid.setHeight(20).setSize(gui_w, 300) .setBackgroundColor(color(16, 180)).setColorBackground(color(16, 180)); group_fluid.getCaptionLabel().align(CENTER, CENTER); px = 10; py = 15; cp5.addButton("reset").setGroup(group_fluid).plugTo(this, "fluid_reset" ).setSize(80, 18).setPosition(px , py); cp5.addButton("+" ).setGroup(group_fluid).plugTo(this, "fluid_resizeUp" ).setSize(39, 18).setPosition(px+=82, py); cp5.addButton("-" ).setGroup(group_fluid).plugTo(this, "fluid_resizeDown").setSize(39, 18).setPosition(px+=41, py); px = 10; cp5.addSlider("velocity").setGroup(group_fluid).setSize(sx, sy).setPosition(px, py+=(int)(oy*1.5f)) .setRange(0, 1).setValue(fluid.param.dissipation_velocity).plugTo(fluid.param, "dissipation_velocity"); cp5.addSlider("density").setGroup(group_fluid).setSize(sx, sy).setPosition(px, py+=oy) .setRange(0, 1).setValue(fluid.param.dissipation_density).plugTo(fluid.param, "dissipation_density"); cp5.addSlider("temperature").setGroup(group_fluid).setSize(sx, sy).setPosition(px, py+=oy) .setRange(0, 1).setValue(fluid.param.dissipation_temperature).plugTo(fluid.param, "dissipation_temperature"); cp5.addSlider("vorticity").setGroup(group_fluid).setSize(sx, sy).setPosition(px, py+=oy) .setRange(0, 1).setValue(fluid.param.vorticity).plugTo(fluid.param, "vorticity"); cp5.addSlider("iterations").setGroup(group_fluid).setSize(sx, sy).setPosition(px, py+=oy) .setRange(0, 80).setValue(fluid.param.num_jacobi_projection).plugTo(fluid.param, "num_jacobi_projection"); cp5.addSlider("timestep").setGroup(group_fluid).setSize(sx, sy).setPosition(px, py+=oy) .setRange(0, 1).setValue(fluid.param.timestep).plugTo(fluid.param, "timestep"); cp5.addSlider("gridscale").setGroup(group_fluid).setSize(sx, sy).setPosition(px, py+=oy) .setRange(0, 50).setValue(fluid.param.gridscale).plugTo(fluid.param, "gridscale"); RadioButton rb_setFluid_DisplayMode = cp5.addRadio("fluid_displayMode").setGroup(group_fluid).setSize(80,18).setPosition(px, py+=(int)(oy*1.5f)) .setSpacingColumn(2).setSpacingRow(2).setItemsPerRow(2) .addItem("Density" ,0) .addItem("Temperature",1) .addItem("Pressure" ,2) .addItem("Velocity" ,3) .activate(DISPLAY_fluid_texture_mode); for(Toggle toggle : rb_setFluid_DisplayMode.getItems()) toggle.getCaptionLabel().alignX(CENTER); cp5.addRadio("fluid_displayVelocityVectors").setGroup(group_fluid).setSize(18,18).setPosition(px, py+=(int)(oy*2.5f)) .setSpacingColumn(2).setSpacingRow(2).setItemsPerRow(1) .addItem("Velocity Vectors", 0) .activate(DISPLAY_FLUID_VECTORS ? 0 : 2); } Group group_display = cp5.addGroup("display"); { group_display.setHeight(20).setSize(gui_w, 50) .setBackgroundColor(color(16, 180)).setColorBackground(color(16, 180)); group_display.getCaptionLabel().align(CENTER, CENTER); px = 10; py = 15; cp5.addSlider("BACKGROUND").setGroup(group_display).setSize(sx,sy).setPosition(px, py) .setRange(0, 255).setValue(BACKGROUND_COLOR).plugTo(this, "BACKGROUND_COLOR"); cp5.addRadio("fluid_displayParticles").setGroup(group_display).setSize(18,18).setPosition(px, py+=(int)(oy*1.5f)) .setSpacingColumn(2).setSpacingRow(2).setItemsPerRow(1) .addItem("display particles", 0) .activate(DISPLAY_PARTICLES ? 0 : 2); } cp5.addAccordion("acc").setPosition(gui_x, gui_y).setWidth(gui_w).setSize(gui_w, height) .setCollapseMode(Accordion.MULTI) .addItem(group_fluid) .addItem(group_display) .open(4); }
|