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

Saturday, 29 June 2013

Testing ruby-processing for compatibility with ruby-2.0

Since jruby-1.7.4, the default version of ruby is 1.9.3 and ruby-processing-1.0.12+ is designed to be run with ruby-1.9 compatibility, will it work with the
--2.0 flag? Here is one way to check if you used jruby to install ruby-processing:-

jruby --2.0 -S rp5 sketch.rb
Or to use built-in jruby-complete-1.7.4 you could set the JRUBY_OPTS environmental variable:-

export JRUBY_OPTS=--2.0
A third way is to set the compat.version in .jrubyrc (home directory or even the local directory).

compat.version=2.0
At present ruby-2.0 support is experimental (and incomplete) with jruby-1.7.4, however it may even be default by jruby-1.7.5?
Anyway so far so good I have not found any problems with ruby-processing-1.0.13.

Monday, 24 June 2013

A Jan Vantomme processing sketch translated to ruby-processing

# sketch by Jan Vantomme
# Part of a series of articles on Processing 2.
# Blog post here:
# http://vormplus.be/blog/article/drawing-shapes-with-quadratic-vertices
# translated for ruby-processing by Martin Prout 
# Note how to specify fill and background using hexadecimal string for color, 
# this is different from vanilla processing
#

load_library :control_panel

attr_reader :debug, :save_one, :step_angle, :cr, :detail, :panel

def setup
  size 450, 320
  control_panel do |c|
    c.title = "controller"
    c.menu(:detail, ['4', '5', '6', '7', '8', '9', '10' ], '7')
    c.button :toggle_debug
    c.button :save_image
    @panel = c
  end
  @debug = false
  @save_one = false
  smooth 8
end

def draw
  panel.set_visible true
  background color('#BDF018')
  translate width / 2, height / 2
  @step_angle = TWO_PI / (detail.to_i - 1)
  fill color('#ffffff')
  no_stroke
  @cr = map(mouse_x, 0, width, 20, 200)
  begin_shape
  detail.to_i.times { |i|
    if (i == 0)
      vertex cos_x(i), sin_y(i)
    else
      quadratic_vertex cos_cx(i), sin_cy(i), cos_x(i), sin_y(i)
    end
  }
  end_shape(CLOSE)

  if (debug)
    # draw lines between points
    stroke_weight(1)
    no_fill
    stroke(0)

    begin_shape
    detail.to_i.times { |i|
      vertex cos_cx(i), sin_cy(i) unless i == 0
      vertex cos_x(i), sin_y(i)
    }
    end_shape CLOSE

    # draw points
    stroke_weight 8
    detail.to_i.times { |i|
      stroke 0
      point cos_x(i), sin_y(i)
      stroke 255, 0, 0
      point cos_cx(i), sin_cy(i)
    }
  end

  if (save_one)
    save_frame("images/quadraticvertex-#####.png")
    @save_one = false
  end
end

def cos_x(n)
  cos(step_angle * n) * 100
end

def sin_y(n)
  sin(step_angle * n) * 100
end

def cos_cx(n)
  cos(step_angle * n - (step_angle / 2 )) * cr
end

def sin_cy(n)
  sin(step_angle * n - (step_angle / 2 )) * cr
end

def toggle_debug
  @debug = !debug
end

def save_image
  @save_one = true
end

Friday, 14 June 2013

Alternative install of ruby-processing for ArchLinux

It is possible to install JRuby using pacman on ArchLinux:-

sudo pacman -S jruby
This will install any dependency such as openjdk
You should now build the ruby-processing-1.0.12.gem

rake 
or:-

jruby -S rake 

To install:-

jruby -S gem install ruby-processing-2.2.0.gem 
Currently pacman installs JRuby-1.7.5 which is what we want, but there may be an issue of amd64 installs at runtime with it trying to load libjiffi-1.2.so for other architectures such as arm and i386 the fix is to install the latest jruby-launcher. However this should be installed locally from a downloaded gem (ie in the same way you just installed ruby-processing). You can then run the rp5 script by calling it from jruby(jruby -S may not be necessary depending o your setup):-

jruby -S rp5 run mysketch.rb 
or:-

/opt/jruby/bin/rp5 run mysketch.rb 
or after adding the path to the bin:-

rp5 run mysketch.rb 

Updated 13th October 2013

Wednesday, 5 June 2013

Ditching jruby-complete, what is holding me back?

Well it is only the glsl shader sketches (I think). So something is added by including the jruby-complete.jar classpath in the command line that allows these sketches to run. So one area of experimentation is to try to add similar classpath when firing up ruby-processing using jruby, rather than from java.

-J-cp="${RP5_ROOT}/lib/core/"

Should ensure processing core and jogl/gluegen run-times get included, but something more seems to be required...
Something that enables compiling of glsl shaders?

Useful Bookmarks?

  1. http://www.compoundtheory.com/
  2. https://github.com/markmandel/jruby-lwjgl
  3. http://codeanticode.wordpress.com/tag/glsl/
  4. http://arcsynthesis.org/gltut/ 
Hey radical thought, since jruby-lwgl seems to work really well, time to ditch ruby-processing, and try something new for a bit at least?

Monday, 3 June 2013

Using pure (gem) ruby libraries with ruby-processing

Now you can create your own ruby libraries, and it relatively easy to include them, but what about using external gems? Well essentially what you are looking at is, can the library be used from jruby, if so you can probably use it with ruby-processing? The simplest way is to just to set the GEM_HOME environmental variable

export GEM_HOME=/var/lib/gems/1.9.1 (worked for me on ubuntu linux)

If this doesn't work for you then you could install your gems using jruby.

jruby -S gem install rake (etc...)

But to be extra safe you should probably install ruby-processing the same way

jruby -S gem install ruby-processing


Further belt and braces stuff to run sketches you should probably start ruby-processing from jruby using the --ruby flag

jruby -S rp5 run --jruby sketch.rb

Here is one such sketch
require 'perlin_noise'

attr_reader :md, :points, :p, :n2d

def setup
  size(800, 600)
  @n2d = Perlin::Noise.new 2, :curve => Perlin::Curve::CUBIC
  md = false
  @points = []
  background(255)
  smooth(8)
end

def draw
  if (md)
    500.times do
      points << Point.new(rand(0 ... width), rand(0 ... height))
    end
  end
  noise_detail(8,0)

  (points.size - 1).downto(1) do |i|
    @p = points[i]
    p.update n2d
    if (p.finished)
      points.delete_at(i)
    end
  end
  # puts(points.size)
end

def mousePressed
  @md = true
end

def mouseReleased
  @md = false
end

def keyPressed
  background(255)
  (points.size - 1).downto(1) do |i|
    @p = points[i]
    points.delete_at(i)
  end
end

class Point
  include Processing::Proxy

  MAX_SPEED = 3000000

  attr_reader :finished, :x, :y, :xv, :yv, :width, :height

  def initialize(x = 0, y = 0)
    @x, @y = x, y
    @xv, @yv = 0, 0
    @finished = false
    @width = $app.width
    @height = $app.height
  end

  def update n2d
    stroke(0, 16)
    @xv = cos(n2d[x * 0.01, y * 0.01] * TWO_PI)
    @yv = -sin(n2d[x * 0.01, y * 0.01] * TWO_PI)

    if ((x > width) || (x < 0))
      @finished = true
    end

    if ((y > height) || (y < 0))
      @finished = true
    end

    if (xv > MAX_SPEED)
      @xv = MAX_SPEED
    else
      @xv = -MAX_SPEED if (xv < -MAX_SPEED)
    end

    if (yv>MAX_SPEED)
      @yv = MAX_SPEED
    else
      @yv = -MAX_SPEED if (yv < -MAX_SPEED)
    end

    @x += xv
    @y += yv

    line(x + xv, y + yv, x, y )
  end
end

Note with this sketch you would probably be better off using processing perlin noise

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