# menger.rb by Martin Prout
require 'ruby-processing'
load_library 'opengl'
# full_screen required for linux with OPENGL (a bug)
full_screen if java.lang.System.get_property('os.name') == "Linux"
ANGLE_STEP = Math::PI/180
DATA = [-1, 0, 1]
MIN_SIZE = 40 # controls recursion depth (20 is pushing it for me!!)
def setup
size(600, 600, OPENGL)
library_loaded?(:opengl) ? render_mode(OPENGL) : render_mode(P3D)
@start_t = Time.now.to_f
@data = [] # initialize empty array
cube(0, 0, 0, 300) # fill data array
@angle = 0
end
# Fill @data array with cube center coordinate
# and size data (this can be done at setup)
def cube x, y, z, sz
unless (sz <= MIN_SIZE) then
u = sz/3.0
DATA.each do |i|
DATA.each do |j|
DATA.each do |k|
cube(x+(i*u), y+(j*u), z+(k*u), u) unless (i.abs + j.abs + k.abs) == 1
end
end
end
end
@data.push [x, y, z, sz] unless sz > MIN_SIZE
end
# Render the cubes (data from @data) using
# translate and custom box function, uses ruby splat
def draw_cubes
@data.each do |point|
x, y, z, sz = *point
push_matrix
translate(x, y, z)
my_box(sz)
pop_matrix
end
end
def draw
background 0
ambient_light 155, 155, 155
directional_light 225, 225, 225, 1, 1, -1
@angle = (ANGLE_STEP + @angle) % (Math::PI * 2)
translate(width/2, height/2, 0)
rotate_x @angle
rotate_y @angle
rotate_z @angle
draw_cubes
end
def my_box sz
no_stroke
begin_shape QUADS
# +Z "front" face
fill 200, 0, 0
vertex -sz/2, -sz/2, sz/2
vertex sz/2, -sz/2, sz/2
vertex sz/2, sz/2, sz/2
vertex -sz/2, sz/2, sz/2
# -Z "back" face
vertex sz/2, -sz/2, -sz/2
vertex -sz/2, -sz/2, -sz/2
vertex -sz/2, sz/2, -sz/2
vertex sz/2, sz/2, -sz/2
# +Y "bottom" face
fill 0, 200, 0
vertex -sz/2, sz/2, sz/2
vertex sz/2, sz/2, sz/2
vertex sz/2, sz/2, -sz/2
vertex -sz/2, sz/2, -sz/2
# -Y "top" face
vertex -sz/2, -sz/2, -sz/2
vertex sz/2, -sz/2, -sz/2
vertex sz/2, -sz/2, sz/2
vertex -sz/2, -sz/2, sz/2
# +X "right" face
fill 0, 0, 200
vertex sz/2, -sz/2, sz/2
vertex sz/2, -sz/2, -sz/2
vertex sz/2, sz/2, -sz/2
vertex sz/2, sz/2, sz/2
# -X "left" face
vertex -sz/2, -sz/2, -sz/2
vertex -sz/2, -sz/2, sz/2
vertex -sz/2, sz/2, sz/2
vertex -sz/2, sz/2, -sz/2
end_shape
end
I would be interested to know how this sketch would work on Windows or Mac os, with a more powerful graphics card. The static screen shot below was taken with min size set to 40 on my linux box (33 is practical limit). P3D is not so good as you get to see the joins.
Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0
Tuesday, 29 September 2009
Subscribe to:
Post Comments (Atom)
Followers
Blog Archive
-
▼
2009
(50)
-
▼
September
(9)
- Real Menger Sponge in Ruby Processing
- A Simple Rotating Cube using PVector and splat
- Tunnel a popular theme in context free
- Fake Menger Sponge
- Using Multidimensional Data Arrays
- Another of Lazydogs Little Gems translated to ruby...
- A More Complicated Sierpinski
- An Advanced Pretzel
- Non cfdg DSL Sierpinski triangle using ruby proces...
-
▼
September
(9)
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