|
| 1 | +#!/usr/bin/env jruby |
| 2 | + |
| 3 | +require 'propane' |
| 4 | +# |
| 5 | +# PixelFlow | Copyright (C) 2016 Thomas Diewald - http://thomasdiewald.com |
| 6 | +# Modified for JrubyArt by Martin Prout |
| 7 | +# A Processing/Java library for high performance GPU-Computing (GLSL). |
| 8 | +# MIT License: https://opensource.org/licenses/MIT |
| 9 | +# |
| 10 | + |
| 11 | +# Simple example that shows how to add Data (density, velocity) to the fluid. |
| 12 | +# |
| 13 | +# controls: |
| 14 | +# |
| 15 | +# LMB: add Density + Velocity |
| 16 | +# MMB: add Velocity |
| 17 | +# RMB: add Velocity |
| 18 | +class FluidMinimal < Propane::App |
| 19 | + load_library :pixel_flow |
| 20 | + |
| 21 | + java_import 'com.thomasdiewald.pixelflow.java.DwPixelFlow' |
| 22 | + java_import 'com.thomasdiewald.pixelflow.java.fluid.DwFluid2D' |
| 23 | + VIEWPORT_W = 1280 |
| 24 | + VIEWPORT_H = 720 |
| 25 | + VIEWPORT_X = 230 |
| 26 | + VIEWPORT_Y = 0 |
| 27 | + BACKGROUND_COLOR = 255 |
| 28 | + |
| 29 | + attr_reader :fluidgrid_scale, :fluid, :pg_fluid, :pg_obstacles, :context |
| 30 | + |
| 31 | + def settings |
| 32 | + size(VIEWPORT_W, VIEWPORT_H, P2D) |
| 33 | + smooth(2) |
| 34 | + end |
| 35 | + |
| 36 | + def setup |
| 37 | + surface.setLocation(VIEWPORT_X, VIEWPORT_Y) |
| 38 | + # main library context |
| 39 | + @context = DwPixelFlow.new(self) |
| 40 | + context.print |
| 41 | + context.printGL |
| 42 | + # fluid simulation |
| 43 | + @fluid = DwFluid2D.new(context, VIEWPORT_W, VIEWPORT_H, fluidgrid_scale) |
| 44 | + cb_fluid_data = lambda do |obj| # obj required for signature (but unused) |
| 45 | + if mouse_pressed? |
| 46 | + vscale = 15 |
| 47 | + px = mouse_x |
| 48 | + py = height - mouse_y |
| 49 | + vx = (mouse_x - pmouse_x) * +vscale |
| 50 | + vy = (mouse_y - pmouse_y) * -vscale |
| 51 | + radius = 20 |
| 52 | + intensity = 1.0 |
| 53 | + temperature = 5.0 |
| 54 | + fluid.add_velocity(px, py, radius, vx, vy) |
| 55 | + if mouse_button == LEFT |
| 56 | + fluid.add_temperature(px, py, radius, temperature) |
| 57 | + radius = 20 |
| 58 | + fluid.add_density(px, py, radius, 0, 0, 0, intensity) |
| 59 | + radius = 16 |
| 60 | + fluid.add_density(px, py, radius, 0, 0.4, 1, intensity) |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + @fluidgrid_scale = 1 |
| 65 | + # set some simulation parameters |
| 66 | + fluid.param.dissipation_density = 0.98 |
| 67 | + fluid.param.dissipation_velocity = 0.92 |
| 68 | + fluid.param.dissipation_temperature = 0.69 |
| 69 | + fluid.param.vorticity = 0.10 |
| 70 | + # interface for adding data to the fluid simulation |
| 71 | + fluid.addCallback_FluiData(cb_fluid_data) |
| 72 | + # pgraphics for fluid |
| 73 | + @pg_fluid = create_graphics(VIEWPORT_W, VIEWPORT_H, P2D) |
| 74 | + pg_fluid.smooth(4) |
| 75 | + # pgraphics for obstacles |
| 76 | + @pg_obstacles = create_graphics(VIEWPORT_W, VIEWPORT_H, P2D) |
| 77 | + pg_obstacles.no_smooth |
| 78 | + pg_obstacles.begin_draw |
| 79 | + pg_obstacles.clear |
| 80 | + # rand obstacles |
| 81 | + pg_obstacles.rect_mode(CENTER) |
| 82 | + pg_obstacles.no_stroke |
| 83 | + pg_obstacles.fill(64) |
| 84 | + srand(0) |
| 85 | + 80.times do |
| 86 | + px = rand(width) |
| 87 | + py = rand(height) |
| 88 | + sx = rand(15..60) |
| 89 | + sy = rand(15..60) |
| 90 | + pg_obstacles.rect(px, py, sx, sy) |
| 91 | + end |
| 92 | + # border-obstacle |
| 93 | + pg_obstacles.rect_mode(CORNER) |
| 94 | + pg_obstacles.stroke_weight(20) |
| 95 | + pg_obstacles.stroke(64) |
| 96 | + pg_obstacles.no_fill |
| 97 | + pg_obstacles.rect(0, 0, pg_obstacles.width, pg_obstacles.height) |
| 98 | + pg_obstacles.end_draw |
| 99 | + # add to the fluid-solver |
| 100 | + fluid.add_obstacles(pg_obstacles) |
| 101 | + frame_rate(60) |
| 102 | + end |
| 103 | + |
| 104 | + def draw |
| 105 | + # update simulation |
| 106 | + fluid.update |
| 107 | + # clear render target |
| 108 | + pg_fluid.begin_draw |
| 109 | + pg_fluid.background(BACKGROUND_COLOR) |
| 110 | + pg_fluid.end_draw |
| 111 | + # render fluid stuff |
| 112 | + fluid.render_fluid_textures(pg_fluid, 0) |
| 113 | + # display |
| 114 | + image(pg_fluid, 0, 0) |
| 115 | + image(pg_obstacles, 0, 0) |
| 116 | + # info |
| 117 | + format_string = 'Fluid Minimal [size %d/%d] [frame %d] [fps: (%6.2f)]' |
| 118 | + surface.set_title(format(format_string, fluid.fluid_w, fluid.fluid_h, fluid.simulation_step, frame_rate)) |
| 119 | + end |
| 120 | +end |
| 121 | + |
| 122 | + |
| 123 | +FluidMinimal.new |
0 commit comments