Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Monday 12 March 2012

Toxiclibs InflateMesh in ruby-processing (with PovRAY export)

See vanilla processing version here for PovRAY files and a ray-traced image.
#
# Copyright (c) 2010 Karsten Schmidt & ruby-processing version Martin Prout 2012
# This library is free software you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation either
# version 2.1 of the License, or (at your option) any later version.
# 
# http://creativecommons.org/licenses/LGPL/2.1/
# 
# This library 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.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

load_libraries 'toxiclibscore', 'toxiclibs_p5', 'volumeutils', 'control_panel', 'povmesh', 'verletphysics'
include_package 'povmesh.mesh'
include_package 'toxi.geom'
include_package 'toxi.geom.mesh'
include_package 'toxi.physics'
include_package 'toxi.physics.behaviors'
include_package 'toxi.processing'
include_package 'toxi.physics.constraints'
include_package 'toxi.volume'

attr_reader :mesh, :gfx, :physics, :render, :box, :pm, :inflate

def setup
  size(680, 382, P3D)
  @gfx = ToxiclibsSupport.new(self)
  @pm = POVMesh.new(self)
  @render = true
  init_physics
end

def draw
  physics.update
  box.vertices.values().each { |v| v.set(physics.particles.get(v.id))}
  box.center(nil)
  box.vertices.values().each { |v| physics.particles.get(v.id).set(v)}
  box.compute_face_normals()
  box.face_outwards()
  box.compute_vertex_normals()
  background(51)
  translate(width / 2.0, height / 2.0, 0)
  rotate_x((height / 2 - mouse_x) * 0.01)
  rotate_y((width / 2 - mouse_y) * 0.01)
  no_fill
  lights
  directional_light(255, 255, 255, -200, 1000, 500)
  specular(255)
  shininess(16)
  gfx.origin(Vec3D.new, 50)
  fill(192)
  no_stroke
  if (render)
    gfx.mesh(box, true, 5)
  else
    no_loop
    pm.begin_save(java.io.File.new(sketchPath("box.inc")))
    pm.set_texture(Textures::METAL) # sequential rainbow color 1st is a red?
    pm.saveAsPOV(box)   # default smooth ie normals
    pm.end_save
    exit # no way back
  end
end

def init_physics
  @box = WETriangleMesh.new
  # create a simple start mesh
  #box.addMesh(Cone.new(Vec3D.new(0, 0, 0), Vec3D.new(0, 1, 0), 10, 50, 100).to_mesh(4))
  box.add_mesh(AABB.new(Vec3D.new, 50).to_mesh())
  # then subdivide a few times...
  4.times { box.subdivide }
  @physics = VerletPhysics.new
  physics.set_world_bounds(AABB.new(Vec3D.new, 180))
  # turn mesh vertices into physics particles
  box.vertices.values.each { |v| physics.add_particle(VerletParticle.new(v))}
  # turn mesh edges into springs
  box.edges.values.each do |e|
    a = physics.particles.get((e.a).id)
    b = physics.particles.get((e.b).id)
    physics.add_spring(VerletSpring.new(a, b, a.distance_to(b), 0.005))
  end
end


def key_pressed
  case key
  when 'r'
    init_physics
  when 's'
    @render = false
  end
end

def mouse_pressed
    @inflate = AttractionBehavior.new(Vec3D.new, 400, -0.3, 0.001)
  physics.add_behavior(inflate);
end

def mouse_released
  physics.remove_behavior(inflate)
end

No comments:

Post a Comment

Followers

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2