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

Tuesday 24 August 2010

Steps to Get Preston Lees Starfield Ruby Processing App Running on Linux

Update 13th October 2011, there should be no need for this since ruby-processing version 1.0.10, linux no longer requires fullscreen to run opengl, further 64 bit system should be automatically detected.

The original screencast can be seen here http://www.prestonlee.com/2010/05/17/3d-osx-applications-with-ruby-processing-screencast/

If you are up and running with GitHub you may wish to download the code directly from Preston Lees GitHub (and modify the starfield.rb as below) otherwise follow instructions below:-


1. Install ruby processing (follow the instructions at http://wiki.github.com/jashkenas/ruby-processing/getting-started)

2. You should create a directory starfield, and create a sub directory data

3. Download Univers66.vlw.gz from http://github.com/preston/Starfield and put it in the data folder

4. Create starfield.rb, star.rb and asychronous_object.rb in the starfield directory here is the code:-




5. In a terminal cd to the starfield directory and enter rp5 run starfield.rb

If you want export the app you will need to modify starfield.rb to require asychronous_object.rb.

Because there is currently an issue with resizing opengl applications with linux you will need to run the app in full screen
mode, use the escape key to quit full screen mode. If it hangs alt tab to another console pgrep java to get the offending process id
the issue kill -s KILL pid where pid is the process id.

Possible issues are:-

Your OS is hogging the graphics driver (solution turn down the level of eye candy)
You don't have a working 3D graphics driver (solution install a 3D graphics driver)
You are running a 64 bit operating system see my last post

Here is a screen-shot of the app running on Kubuntu, with the desktop eye candy turned down.

Running Ruby- Processing Opengl Apps on a 64 bit box

Hardware Considerations

When I came to upgrading my linux-box recently, the obvious choice was a bare bones system with a dual core AMD 64 bit processor (cheap and powerful enough). I couldn't see any point in running a 32 bit operating system on it so I installed Kubuntu 64 bit OS which provides, as default a Nouveau graphics driver to support a NVIDIA graphics processor. This driver only supports 2D acceleration, and 3D acceleration is provided by software emulation (mesa driver, slow). However the Ubuntu distro gives you the option of installing proprietary NVIDIA drivers, either using the scripts provided by Ubuntu, or direct from NVIDIA (but you are warned not to !!). Do a bit of research before installing the latest drivers directly from NVIDIA, ideally you should be comfortable compiling a kernel, updating grub etc. I usually find myself compiling a custom kernel for some reason other, so I opted for the latest drivers from NVIDIA, but then I've been using linux for at least six years.

OpenGL drivers

Like vanilla (java) processing the native libraries for opengl included are 32 bit, which is absolutely as it should be, however I don't think it should not stay that way!!!!
Fortunately the 64 bit native libraries already available, they are just stuck in a jar.
Larry Kyrala produced a script to extract the relevant 64 bit *.so files for vanilla processing. I present a modified version here.
Famous last words "It Worked For Me", I offer no guarantees and you may need to further modify the script for your installation. Depending on where you install your gems you may need to have root authority to run the script.


#!/usr/bin/env ruby

########################
# fix-rp5-opengl.rb
# based on a script by
# Larry Kyrala for
# vanilla processing
# extracts 64-bit native linux binaries
# for opengl (properly backs-up 32-bit binaries)
########################

require 'fileutils'

# script to fix ruby-processing for 64-bit linux
rp5_dir = "/var/lib/gems/1.9.1/gems/ruby-processing-1.0.9"

library_dir = File.join(rp5_dir,"library","opengl","library");
backup_dir = File.join(library_dir,"original-native");
unpack_dir = File.join(library_dir,"unpack-amd64");
Dir.mkdir backup_dir unless File.exists?(backup_dir)
Dir.mkdir unpack_dir unless File.exists?(unpack_dir)

jars = Dir.glob(File.join(library_dir,"*.jar")).select {|f| f =~ /linux-amd64/ }
jars.each do |jar_file|
  system "unzip -o #{jar_file} -d #{unpack_dir}"
end  

libraries = Dir.glob(File.join(unpack_dir,"*.so"))
libraries.each do |so_file|
  src = File.join(library_dir,File.basename(so_file))
  unless File.exists?(File.join(backup_dir,File.basename(so_file)))
    FileUtils.mv(src, backup_dir)
  end
  FileUtils.cp(so_file, library_dir, :preserve => true)
end
 


Ruby-Processing Issue

Presently there is an unresolved opengl issue with sketch resizing with ruby-processing. In practice that means you can only run ruby-processing opengl sketches in full-screen mode on linux.
Adding the line at the beginning of the sketch
full_screen
is often all you need to do to get the sketch to run.

Friday 20 August 2010

Exploring Threads in JRuby

I was well impressed recently with some ruby-processing work by Preston Lee, in which he has created a multi-threaded starfield http://github.com/preston/Starfield (seemingly using ruby-native threads). That got me thinking about exploring the use of java threads in jruby and possibly ruby-processing. Here is some of my experimentation:-


require 'java' 

module JavaLang                    # create a namespace for java.lang
  include_package "java.lang"      # we don't want to clash with Ruby Thread?
end

class ThreadImpl
  include JavaLang::Runnable       # include interface as a 'module'
  
  attr_reader :runner   # instance variables
  
  def initialize
    @runner = JavaLang::Thread.current_thread # get access to main thread
    puts "...in thread #{JavaLang::Thread.current_thread.get_name}"
  end
  
  def run
    puts "...in thread #{JavaLang::Thread.current_thread.get_name}"
  end
  
end

begin
  thread0 = JavaLang::Thread.new(ThreadImpl.new).start
  thread1 = JavaLang::Thread.new(ThreadImpl.new).start
  thread2 = JavaLang::Thread.new(ThreadImpl.new).start
  thread3 = JavaLang::Thread.new(ThreadImpl.new).start
  thread4 = JavaLang::Thread.new(ThreadImpl.new).start
  thread5 = JavaLang::Thread.new(ThreadImpl.new).start
  thread6 = JavaLang::Thread.new(ThreadImpl.new).start
  thread7 = JavaLang::Thread.new(ThreadImpl.new).start
rescue 
  puts $!
end


# Output when run:-
#
#...in thread main
#...in thread main
#...in thread Thread-0
#...in thread main
#...in thread Thread-1
#...in thread main
#...in thread Thread-2
#...in thread main
#...in thread Thread-3
#...in thread main
#...in thread Thread-4
#...in thread main
#...in thread Thread-5
#...in thread main
#...in thread Thread-6
#...in thread Thread-7

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