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

Saturday, 19 December 2009

3D Lindenmayer Systems using the anar+ library with ruby-processing

When you start this sketch one block is drawn, by repeatedly pressing the space-bar you create a new iteration, and more blocks are drawn (don't go mad or at least ensure there is enough memory allocated/available) scrolling zooms you in and out and you eventually you can kind-off step through the plane off the screen. Hold down the middle mouse button to rotate the plane of the image (prepare to be amazed, at what a little script can do).

load_libraries 'anar', 'opengl'
import "anar" if library_loaded? "anar"
import "lsys.Grammar" if library_loaded? "anar"
import "processing.opengl" if library_loaded? "opengl"


attr_accessor :my_obj, :grammar, :box, :ts, :tf, :init_t, :r, :s

def setup()
  library_loaded?(:opengl) ? setup_opengl : fail_opengl

def init_grammar()
  @grammar = Grammar.new("bt")
  # here define the rules
  # * means any kind of symbol
  # the example rules below are therefore non contextual
  grammar.add_rule("*b*", "brb")
  grammar.add_rule("*t*", "rssb")
  grammar.add_rule("*r*", "rs")
  grammar.add_rule("*s*", "s")
  # this one makes it context dependant
  #     grammar.add_rule("sss", "ss")

def interpret_init
  # base element
  @box = Box.new(10, 10, 50)
  # initial position
  @init_t = Translate.new(0, 50, 0)
  # base transformations
  @ts = Translate.new(Anar.Pt(0, 10, -0.001))
  @r = RotateZ.new(0.5)
  @s = Scale.new(Anar.Pt(0.99, 0.99, 0.99))

def interpret_grammar()
  @my_obj = Obj.new
  @tf = Transform.new(init_t)

  grammar.num_of_symbols.times do |i|

    case(grammar.symbol(i)[0].chr) # todo this is bit of hack, I need understand grammar better
      when 'b'
        my_copy = Obj.new(box, tf)
        @tf = Transform.new(tf)   
      when 't'
      when 'r'
      when 's'
        puts "grammar not understood"

def draw()

def key_pressed()
  if(key==' ')

def setup_opengl
  render_mode OPENGL
  hint ENABLE_OPENGL_4X_SMOOTH #optional

def fail_opengl
  abort "!!!You absolutely need opengl for this sketch!!!"

No comments:

Post a Comment


Blog Archive

About Me

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