Java to Python (PixelFlow library)

Following @monkstone suggestion I put all 3 shaders’ files in the main sketch file and it worked.
Also I apologize as I realize my first draft was full of typos. Please find below a fully working and cleaned-up version.

Script
add_library('PixelFlow')
from com.jogamp.opengl import GL2ES2


def setup():
    global fluid, pg_fluid, context, particles, tex_particles, shader_particleSpawn, shader_particleUpdate, shader_particleRender
    size(1000, 800, P2D)
    frameRate(1000)
    smooth(8)

    context = DwPixelFlow(this)
    fluid = DwFluid2D(context, width, height, 0)
    tex_particles = DwGLTexture.TexturePingPong()

    particles = Particles(context, 1000 * 1000)
    pg_fluid = createGraphics(width, height, P2D)

    shader_particleSpawn  = context.createShader("particleSpawn.frag")
    shader_particleUpdate = context.createShader("particleUpdate.frag")
    shader_particleRender = context.createShader("particleRender.glsl",  "particleRender.glsl")
    shader_particleRender.vert.setDefine("SHADER_VERT", 1)
    shader_particleRender.frag.setDefine("SHADER_FRAG", 1)
    
    fluid.param.dissipation_velocity = 0.99
    fluid.param.dissipation_density  = 0.99
    fluid.param.dissipation_temperature = 0.1
    fluid.param.vorticity = 0.00
    fluid.param.timestep = 0.1
    fluid.addCallback_FluiData(MyFluidData())

    
def draw():
    image(pg_fluid, 0, 0)
    
    pg_fluid.beginDraw()
    pg_fluid.background(0)
    pg_fluid.endDraw()
    
    fluid.update()
    #fluid.renderFluidTextures(pg_fluid, 0)

    particles.update(fluid)
    particles.render(pg_fluid, 0)

    
class MyFluidData(DwFluid2D.FluidData):
    def update(_, fluid):
        if mousePressed:
            radius = 15
            px = mouseX
            py = height-mouseY
            vx = (mouseX-pmouseX) * radius
            vy = (mouseY-pmouseY) * - radius
            fluid.addVelocity(px, py, 20, vx, vy)
            fluid.addDensity(px, py, 20, 0.0, 0.4, .7, .5)
            particles.spawn(fluid, px, py, radius, 100)
            
            
class Particles(object):
    def __init__(self, context, n_particles):
        self.context = context
        self.npart = n_particles
        self.ALIVE_LO = 0
        self.ALIVE_HI = 0
        self.ALIVE_PARTICLES = 0
        self.dissipation = 0.90
        self.inertia = 0.20
        self.particles_x = 0
        self.particles_y = 0
        self.resize(context, n_particles)

        
    def resize(self, context, n_particles):
        self.particles_x = ceil(sqrt(n_particles))
        self.particles_y = self.particles_x

        context.begin()
        tex_particles.release()
        tex_particles.resize(context, GL2ES2.GL_RGBA32F, self.particles_x, self.particles_y, GL2ES2.GL_RGBA, GL2ES2.GL_FLOAT, GL2ES2.GL_NEAREST, 4, 4)
        context.end("ParticleSystem.resize")
        
    def spawn(self, fluid, px, py, radius, count):
        count = round(count * 1.0)
        spawn_lo = int(self.ALIVE_HI)
        spawn_hi = int(min(spawn_lo + count, self.npart))
        n = float(random(1)) * PI
        
        context.begin()
        context.beginDraw(tex_particles.dst)
        shader_particleSpawn.begin()
        if fluid is not None: shader_particleSpawn.uniform2f("wh_viewport", fluid.viewp_w, fluid.viewp_h)
        shader_particleSpawn.uniform1i("spawn_lo", spawn_lo)
        shader_particleSpawn.uniform1i("spawn_hi", spawn_hi)
        shader_particleSpawn.uniform2f("spawn_origin", px, py)
        shader_particleSpawn.uniform1f("spawn_radius", radius)
        shader_particleSpawn.uniform1f("noise", n)
        shader_particleSpawn.uniform2f("wh_particles" , self.particles_x, self.particles_y)
        shader_particleSpawn.uniformTexture("tex_particles" , tex_particles.src)
        shader_particleSpawn.drawFullScreenQuad()
        shader_particleSpawn.end()
        context.endDraw()
        context.end("ParticleSystem.spawn")
        tex_particles.swap()
        
        self.ALIVE_HI = spawn_hi
        self.ALIVE_PARTICLES = max(self.ALIVE_PARTICLES, self.ALIVE_HI - self.ALIVE_LO)
    
    def update(self, fluid):
        self.context.begin()
        self.context.beginDraw(tex_particles.dst)
        shader_particleUpdate.begin()
        shader_particleUpdate.uniform2f("wh_fluid", fluid.fluid_w, fluid.fluid_h)
        shader_particleUpdate.uniform2f("wh_particles", self.particles_x, self.particles_y)
        shader_particleUpdate.uniform1f("timestep", fluid.param.timestep)
        shader_particleUpdate.uniform1f("rdx", 1.0 / fluid.param.gridscale)
        shader_particleUpdate.uniform1f("dissipation", self.dissipation)
        shader_particleUpdate.uniform1f("inertia", self.inertia)
        shader_particleUpdate.uniformTexture("tex_particles", tex_particles.src)
        shader_particleUpdate.uniformTexture("tex_velocity" , fluid.tex_velocity.src)
        shader_particleUpdate.uniformTexture("tex_obstacles", fluid.tex_obstacleC.src)
        shader_particleUpdate.drawFullScreenQuad()
        shader_particleUpdate.end()
        self.context.endDraw()
        self.context.end("ParticleSystem.update")
        tex_particles.swap()
        
        
    def render(self, dst, bg):
        num_points_to_render = self.ALIVE_PARTICLES
        w = dst.width
        h = dst.height
    
        dst.beginDraw();
        dst.blendMode(PConstants.BLEND)
        if bg == 0: dst.blendMode(PConstants.ADD)
        context.begin();
        shader_particleRender.begin()
        shader_particleRender.uniform2f("wh_viewport", w, h)
        shader_particleRender.uniform2i("num_particles", self.particles_x, self.particles_y)
        shader_particleRender.uniform1f("point_size", 1.5)
        shader_particleRender.uniformTexture("tex_particles", tex_particles.src)
        shader_particleRender.drawFullScreenPoints(num_points_to_render)
        shader_particleRender.end()
        context.end("ParticleSystem.render")
        dst.endDraw()

Thank you

1 Like