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

Wednesday 23 September 2009

Fake Menger Sponge

 Here I have used a combination of context free art ('cfdg') and ruby-processing to produce a fake "Menger Sponge" here is the cfdg file used create a Sierpinksi carpet image.


startshape carpet
background{b 1 h 360 sat 1}

rule carpet{
patch{}
carpet{s .3333 y .3333}
carpet{s .3333 x .3333 y .3333}
carpet{s .3333 x .3333}
carpet{s .3333 x .3333 y -.3333}
carpet{s .3333 y -.3333}
carpet{s .3333 x -.3333 y -.3333}
carpet{s .3333 x -.3333}
carpet{s .3333 x -.3333 y .3333}
}

rule patch{
SQUARE{s .3333 b -1}
}




Here is the Sierpinski carpet used for the cube faces (needs to be put in data folder). Image has been cropped to remove a blank border.





require 'ruby-processing'
class FakeMenger < Processing::App
  # load_library 'opengl' ## for opengl uncomment this and adjust render mode
  # full_screen ## uncomment this for linux opengl or just if you like fullscreen

  def setup
    render_mode P3D ## OPENGL would be better if you're not on linux
    @theta = 0.0
    frame_rate 60
    @img = load_image "menger.png" ## put image in data folder
    texture_mode NORMALIZED
  end

  def draw
    background 0
    @theta += 0.01
    ambient_light 255, 0, 0
    directional_light 255, 0, 0, 1, -1, 0
    translate width/2, height/2, 0
    rotate_x @theta
    rotate_y @theta
    draw_fake_menger height/4
  end

  def draw_fake_menger sz
    stroke 0
    begin_shape QUADS
    texture @img
    # +Z "front" face
    vertex -sz, -sz,  sz, 0, 0
    vertex  sz, -sz,  sz, sz, 0
    vertex  sz,  sz,  sz, sz, sz
    vertex -sz,  sz,  sz, 0, sz

    # -Z "back" face
    vertex  sz, -sz, -sz, 0, 0
    vertex -sz, -sz, -sz, sz, 0
    vertex -sz,  sz, -sz, sz, sz
    vertex  sz,  sz, -sz, 0, sz

    # +Y "bottom" face
    vertex -sz,  sz,  sz, 0, 0
    vertex  sz,  sz,  sz, sz, 0
    vertex  sz,  sz, -sz, sz, sz
    vertex -sz,  sz, -sz, 0, sz

    # -Y "top" face
    vertex -sz, -sz, -sz, 0, 0
    vertex  sz, -sz, -sz, sz, 0
    vertex  sz, -sz,  sz, sz, sz
    vertex -sz, -sz,  sz, 0, sz

    # +X "right" face
    vertex  sz, -sz,  sz, 0, 0
    vertex  sz, -sz, -sz, sz, 0
    vertex  sz,  sz, -sz, sz, sz
    vertex  sz,  sz,  sz, 0, sz

    # -X "left" face
    vertex -sz, -sz, -sz, 0, 0
    vertex -sz, -sz,  sz, sz, 0
    vertex -sz,  sz,  sz, sz, sz
    vertex -sz,  sz, -sz, 0, sz
    
    end_shape
  end

end

FakeMenger.new :title => "Fake Menger Sponge",  :width => 300,  :height => 300
# FakeMenger.new :title => "Fake Menger Sponge",  :width => 1024,  :height => 1024



If you are not on linux try using 'opengl' it should run smoother than with P3D, where the cube actually looks decidedly spongy. It actually works tolerably well using opengl with linux, but you need to set full_screen mode and you might as well beef up the size a bit. Just for a bit of fun you can replace the black on the image with transparency, then you a bit of a ghostly image. I have implemented the menger in ruby-processing, follow the link.

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