Here is good example of how nice it is to have libraries, the PeasyCam library makes configuring the camera a cinch (it also works pretty smoothly). The lsystem grammar library means you can forget about how the production string is generated. What was the question?
########################################################
# A 3D Hilbert fractal implemented using a
# Lindenmayer System in ruby-processing by Martin Prout
# Best if you've got opengl
########################################################
require 'hilbert'
class Hilbert_Test < Processing::App
full_screen # NB: All distances are relative to screen height
load_libraries 'grammar', 'PeasyCam', 'opengl'
import 'peasy'
import "processing.opengl" if library_loaded? "opengl"
attr_reader :hilbert, :cam
def setup()
library_loaded?(:opengl) ? configure_opengl : render_mode(P3D)
configure_peasycam
@hilbert = Hilbert.new(height)
hilbert.create_grammar(3)
no_stroke()
end
def configure_peasycam
cam = PeasyCam.new(self, height/10)
cam.set_minimum_distance(height/10)
cam.set_maximum_distance(height/2)
end
def configure_opengl
render_mode OPENGL
hint ENABLE_OPENGL_4X_SMOOTH # optional
hint DISABLE_OPENGL_ERROR_REPORT # optional
end
def draw()
background(0)
lights()
hilbert.render()
end
end
############################
# hilbert.rb
###########################
class Hilbert
include Processing::Proxy
attr_reader :grammar, :axiom, :production, :premis, :rule,
:theta, :scale_factor, :distance, :phi, :len
def initialize(len)
@axiom = "X"
@grammar = Grammar.new(axiom)
@production = axiom
@premis = "X"
@rule = "^<XF^<XFX-F^>>XFX&F+>>XFX-F>X->"
@len = len
@distance = len/12 # distance value relative to screen height
@theta = Math::PI/180 * 90
@phi = Math::PI/180 * 90
grammar.add_rule(premis, rule)
no_stroke()
end
def render()
translate(-len/42, len/42, -len/42) # use the "answer?" to center the Hilbert
fill(0, 75, 152)
light_specular(204, 204, 204)
specular(255, 255, 255)
shininess(1.0)
production.scan(/./) do |ch|
case(ch)
when "F"
translate(0, distance/-2, 0)
box(distance/9 , distance, distance/9)
translate(0, distance/-2, 0)
when "+"
rotateX(-theta)
when "-"
rotateX(theta)
when ">"
rotateY(theta)
when "<"
rotateY(-theta)
when "&"
rotateZ(-phi)
when "^"
rotateZ(phi)
when "X"
else
puts("character '#{ch}' not in grammar")
end
end
end
##############################
# create grammar from axiom and
# rules (adjust scale)
##############################
def create_grammar(gen)
@distance *= 0.5**gen
@production = @grammar.generate gen
end
end
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Saturday, 30 January 2010
Subscribe to:
Post Comments (Atom)
Followers
Blog Archive
-
▼
2010
(73)
-
▼
January
(14)
- Hello Peasy in ruby processing (a simple PeasyCam ...
- Using the PeasyCam library in ruby-processing
- 3D Hilbert Using LSystems and ruby-processing
- Two dimensional Hilbert fractal with LSystems and ...
- Fern Fractal Using LSystems (includes a subtle col...
- Using a symbolic grammar for LSystems ruby-processing
- Islands in ruby processing using my grammar library
- Refactoring my Penrose Snowflake
- A 3D Plant using L-Systems and ruby-processing
- Another control panel example with odd turtle turns
- Adding some controls to Sierpinski sketch
- A Sierpinski Variant using L-Systems and ruby proc...
- An interactive l-system sketch using the Control P...
- Checking the probabilities
-
▼
January
(14)
About Me
- monkstone
- I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2
No comments:
Post a Comment