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

Thursday, 9 September 2010

Testing the cube primitive in my context free DSL

Another context free DSL example, better annotated, control panel and library as previous post (not shown):-

# cube.rb

load_libraries :test_free, :control_panel

attr_reader :xrot, :yrot, :zrot

def setup_the_spiral
  @spiral = ContextFree.define do  # define the cf rules
    rule :cubeform do
      5.times do |i|
        split do                   # record the context
        cuby :ry => 72 * i
        rewind                     # restore the context

    rule :beam do                      
       6.times do |i|
        split do                    # record the context
          cube :y => 0.2 * i
        rewind                      # restore the context
    rule :cuby do
      beam :brightness => 1
      cuby :size => 0.98, :y => -0.24  # recursive call ends when size < 1


def setup              # static setup
  size 800, 800, P3D   # I can only use P3D (unless full_screen) on linux opengl might work on mac/windows

def configure_control # setup control panel gui
  control_panel do |c|
    c.title = "Attitude Control"
    c.slider :xrot, -3.1..3.1, 0.5
    c.slider :yrot, -3.1..3.1, 0.5
    c.slider :zrot, -3.1..3.1, 1.0

def draw             # animation loop    
  background 0.8     
  @spiral.render :cubeform, :start_x => 0, :start_y => 10, :start_z => -30, :size => height/300, :stop_size => 1, :color => [0, 0.8, 0.8]
  @spiral.rotate_x xrot
  @spiral.rotate_y yrot
  @spiral.rotate_z zrot   


Within the sketch :hue, :saturation and :brightness can be individually set. If you want to use :alpha, initialize :color with a 4 dimension array. Note, the color mode in the ruleset is hsb with range 0 .. 1.0. Increment and decrement is linear cf size where it is multiplicative.

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