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

Saturday 25 September 2010

An Infinity Loop? With My 3D Context Free Library


# infinity.rb
# An 'infinity loop?'

load_libraries :test_free

full_screen

attr_reader :xrot, :yrot, :zrot, :wheel, :col

def setup_the_spiral
  @spiral = ContextFree.define do

    rule :infinity do
      360.times do |i|
      split do
        segment :rz => i, :ry => Math.sin(i*Math::PI/180)*0.2, :x => Math.cos(i*Math::PI/180)*3
      rewind
      end
      end
    end

    rule :segment do                      
      cube :alpha => 0.8
    end
   
    rule :segment do                      
      cube :brightness => 1.0, :saturation => 1.0
    end

  end
end



def setup
  render_mode P3D
  @wheel = JWheelListener.new(-50, -20)      # initialize listener with start_z and maximum values
  self.add_mouse_wheel_listener(wheel)       # register the mouse listener  
  smooth
  @xrot = 0.01
  @yrot = 0
  @zrot = 0
  @col = 0
  setup_the_spiral
end

def draw
  background 0
  setup_lights col
  specular col, 1, 1
  emissive 0.05
  shininess 10
  smooth_rotation(3, 3.2)
  smooth_color(6.0)
  @spiral.render :infinity, :start_x => 0, :start_y => 0, :start_z => wheel.zoom, :size => height/350,
  :stop_size => 0.2, :color => [0, 0.7, 0.7, 0.5], :rx => xrot, :ry => yrot, :rz => zrot
end

##
# Generate a gradient value that changes smoothly over time in the range [ 0, 1 ].
# The Math.sin() function is used to map the current time in milliseconds somewhere
# in the range [ 0, 1 ]. A 'speed' factor is specified by the parameters s1.
#
def smooth_gradient(s1)
  mills = millis * 0.00003   
  gradient = 0.5 * Math.sin(mills * s1) + 0.5
end
 
##
# Rotate the current coordinate system.
# Uses smooth_gradient() to smoothly animate the rotation.
#
def smooth_rotation(s1, s2)    
  @yrot = 2.0 * Math::PI * smooth_gradient(s1)
  @zrot = 2.0 * Math::PI * smooth_gradient(s2)
end

##
# Generate a 'hue' value which smoothly changes over time.
# The speed of change is controlled by the parameter s1.
#
def smooth_color(s1)
  @col = smooth_gradient(s1)
end

def setup_lights(col)   
  directional_light(col, 0.8, 0.8, -0.5, 0.5, -1)
  point_light(col, 0.8, 0.8, -0.5, 0.5, -1)
  ambient_light(col, 0.8, 0.8, 1, 1, 1)
end





No comments:

Post a Comment

Followers

Blog Archive

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