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
end
end
end
rule :beam do
6.times do |i|
split do # record the context
cube :y => 0.2 * i
rewind # restore the context
end
end
end
rule :cuby do
beam :brightness => 1
cuby :size => 0.98, :y => -0.24 # recursive call ends when size < 1
end
end
end
def setup # static setup
size 800, 800, P3D # I can only use P3D (unless full_screen) on linux opengl might work on mac/windows
configure_control
smooth
setup_the_spiral
end
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
end
end
def draw # animation loop
background 0.8
lights
@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
end
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