Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Wednesday, 4 April 2012

A Menger Sponge in Pyprocessing

   1 from pyprocessing import *
   2 
   3 FOV = PI/3.0
   4 angle = 0.0
   5 ANGLE_STEP = PI / 180.0
   6 
   7              
   8 
   9 def setup():
  10     size(800,600)
  11     cameraZ = (height/2.0) / tan(FOV/ 2.0)
  12     perspective(FOV, float(width)/float(height), cameraZ/10.0, cameraZ*10.0)
  13     
  14 def draw():
  15     background(0,  0,  200)
  16     noStroke()
  17     stroke(0)
  18     lights()
  19     defineLights()
  20     translate(width/2.0, height/2.0, 0)
  21     global angle
  22     angle = (angle + ANGLE_STEP) % TWO_PI
  23     rotateZ(angle)
  24     rotateY(angle)
  25     create_menger(0, 0, 0, height/2.0)
  26 
  27 def my_cube(xx,  yy,  zz,  sz):
  28     """
  29     Draw a cube with centre xx, yy, zz and size sz
  30     """
  31     beginShape(QUADS)
  32     normal(0, 0, 1)
  33     vertex(-sz / 2.0 + xx, -sz / 2.0 + yy, -sz / 2.0 + zz)
  34     vertex(+sz / 2.0 + xx, -sz / 2.0 + yy, -sz / 2.0 + zz)
  35     vertex(+sz / 2.0 + xx, +sz / 2.0 + yy, -sz / 2.0 + zz)
  36     vertex(-sz / 2.0 + xx, +sz / 2.0 + yy, -sz / 2.0 + zz)
  37     
  38     #Back face    
  39     normal(0, 0, -1)
  40     vertex(-sz / 2.0 + xx, -sz / 2.0 + yy, +sz / 2.0 + zz)
  41     vertex(+sz / 2.0 + xx, -sz / 2.0 + yy, +sz / 2.0 + zz)
  42     vertex(+sz / 2.0 + xx, +sz / 2.0 + yy, +sz / 2.0 + zz)
  43     vertex(-sz / 2.0 + xx, +sz / 2.0 + yy, +sz / 2.0 + zz)
  44     
  45     #Left face    
  46     normal(1, 0, 0)
  47     vertex(-sz / 2.0 + xx, -sz / 2.0 + yy, -sz / 2.0 + zz)
  48     vertex(-sz / 2.0 + xx, -sz / 2.0 + yy, +sz / 2.0 + zz)
  49     vertex(-sz / 2.0 + xx, +sz / 2.0 + yy, +sz / 2.0 + zz)
  50     vertex(-sz / 2.0 + xx, +sz / 2.0 + yy, -sz / 2.0 + zz)
  51     
  52     #Right face    
  53     normal(-1, 0, 0)
  54     vertex(+sz / 2.0 + xx, -sz / 2.0 + yy, -sz / 2.0 + zz)
  55     vertex(+sz / 2.0 + xx, -sz / 2.0 + yy, +sz / 2.0 + zz)
  56     vertex(+sz / 2.0 + xx, +sz / 2.0 + yy, +sz / 2.0 + zz)
  57     vertex(+sz / 2.0 + xx, +sz / 2.0 + yy, -sz / 2.0 + zz)
  58     
  59     #Top face    
  60     normal(0, 1, 0)
  61     vertex(-sz / 2.0 + xx, -sz / 2.0 + yy, -sz / 2.0 + zz)
  62     vertex(+sz / 2.0 + xx, -sz / 2.0 + yy, -sz / 2.0 + zz)
  63     vertex(+sz / 2.0 + xx, -sz / 2.0 + yy, +sz / 2.0 + zz)
  64     vertex(-sz / 2.0 + xx, -sz / 2.0 + yy, +sz / 2.0 + zz)
  65     
  66     #Bottom face    
  67     normal(0, -1, 0)
  68     vertex(-sz / 2.0 + xx, +sz / 2.0 + yy, -sz / 2.0 + zz)
  69     vertex(+sz / 2.0 + xx, +sz / 2.0 + yy, -sz / 2.0 + zz)
  70     vertex(+sz / 2.0 + xx, +sz / 2.0 + yy, +sz / 2.0 + zz)
  71     vertex(-sz / 2.0 + xx, +sz / 2.0 + yy, +sz / 2.0 + zz)
  72     endShape()
  73 
  74 def create_menger(xx, yy, zz, sz):
  75     """
  76     Create a recursive menger sponge using my_cube
  77     """ 
  78     u = sz / 3.0
  79     if (sz < 40):
  80         my_cube(xx, yy, zz, sz)
  81     else:
  82         for i in xrange(-1, 2, 1):
  83             for j in xrange(-1, 2, 1):
  84                 for k in xrange(-1, 2, 1):
  85                     if ((abs(i) + abs(j) + abs(k)) > 1):
  86                         create_menger(xx + (i * u), yy + (j * u), zz + (k * u), u)
  87                         
  88 def defineLights():
  89     """
  90     Without lights you wouldn't see the menger
  91     """
  92     ambientLight(50, 50, 50)
  93     pointLight(150, 100, 0, 200, -150, 0)
  94     directionalLight(0, 102, 255, 1, 0, 0)
  95     spotLight(255, 255, 109, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2)     
  96  
  97 
  98 run()
  99     
 100 
Experimenting to see how a pyprocessing pyglet rendered sponge compares with processing-2.05a, you could easily see another level of recursion, if you set limit size to 20, but rotation gets a bit slow (no memory issues though), I might just look at PovRAY export from pyprocessing? Even though I know there is a brilliant iso-surface rendering of a menger in PovRAY, I fancy creating a hybrid shape, where some cubes get replaced by spheres (tricky to achieve as an iso-surface).

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2