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

Sunday, 11 November 2012

Retained menger processing.py

If I had any lingering doubts about which python implementation of processing was best between pyprocessing and processing.py this sketch sealed it for me. Since processing.py was updated (not complete yet) to support processing-2.0 it is streets ahead in performance, especially now it supports retained shape (FBO object). Theoreticaly I think something similar should be possible via pyglet, however it is much harder to do, and the jvm will will probably still outperform regular python and probably pypy as well. This sketch was developed using jEdit and the development version of processing.py see previous sketch.
   1 #################
   2 # Retained menger by Martin Prout
   3 # using processing.py
   4 # featuring processing-2.0 core
   5 ################
   6 
   7 angle = 0.0
   8 STEP = PI / 360.0
   9 MIN_SIZE = 20
  10 
  11 def setup():
  12     size(800, 600, P3D)  
  13     camera(width/2, height/2, height, 0, 0, 0, 0, -1, 0)
  14     smooth(8)
  15     global menger
  16     menger = createShape(PShape.GROUP)
  17     createMenger(0, 0, 0, height * 0.8)
  18     
  19     
  20 def draw():
  21     background(20, 20, 200)
  22     noStroke()
  23     lights()
  24     defineLights()
  25     global angle, menger
  26     angle = (angle + STEP) % TWO_PI
  27     rotateZ(angle)
  28     rotateY(angle) 
  29     shape(menger)
  30 
  31 def createMenger(xx, yy, zz, sz):
  32     """
  33     A recursive function creates and stores custom cubes
  34     to menger PShape.GROUP
  35     """
  36     u = sz / 3
  37     if (sz < MIN_SIZE): # recursion limited by minimum cube size
  38         noStroke()
  39         menger.addChild(createCube(xx, yy, zz, sz)) # create and add a cube        
  40     else:
  41         for i in range(-1, 2, 1):
  42             for j in range(-1, 2, 1):
  43                 for k in range(-1, 2, 1):
  44                     if ((abs(i) + abs(j) + abs(k)) > 1):
  45                         createMenger(xx + (i * u), yy + (j * u), zz + (k * u), u)             
  46                      
  47                         
  48 def createCube(xx, yy, zz, sz):
  49     """
  50     Function returns a custom cube PShape
  51     Requires processing 
  52     """
  53     cube = createShape(QUADS)
  54     cube.ambient(50)    
  55     cube.specular(30) 
  56     
  57     # Front face
  58     
  59     cube.fill(255)
  60     cube.normal(0, 0, 1)
  61     cube.vertex(-sz / 2 + xx, -sz / 2 + yy, -sz / 2 + zz)
  62     cube.vertex(+sz / 2 + xx, -sz / 2 + yy, -sz / 2 + zz)
  63     cube.vertex(+sz / 2 + xx, +sz / 2 + yy, -sz / 2 + zz)
  64     cube.vertex(-sz / 2 + xx, +sz / 2 + yy, -sz / 2 + zz)
  65     
  66     # Back face
  67     
  68     cube.normal(0, 0, -1)
  69     cube.vertex(-sz / 2 + xx, -sz / 2 + yy, +sz / 2 + zz)
  70     cube.vertex(+sz / 2 + xx, -sz / 2 + yy, +sz / 2 + zz)
  71     cube.vertex(+sz / 2 + xx, +sz / 2 + yy, +sz / 2 + zz)
  72     cube.vertex(-sz / 2 + xx, +sz / 2 + yy, +sz / 2 + zz)
  73     
  74     # Left face
  75     
  76     cube.normal(1, 0, 0)
  77     cube.vertex(-sz / 2 + xx, -sz / 2 + yy, -sz / 2 + zz)
  78     cube.vertex(-sz / 2 + xx, -sz / 2 + yy, +sz / 2 + zz)
  79     cube.vertex(-sz / 2 + xx, +sz / 2 + yy, +sz / 2 + zz)
  80     cube.vertex(-sz / 2 + xx, +sz / 2 + yy, -sz / 2 + zz)
  81     
  82     # Right face
  83     
  84     cube.normal(-1, 0, 0)
  85     cube.vertex(+sz / 2 + xx, -sz / 2 + yy, -sz / 2 + zz)
  86     cube.vertex(+sz / 2 + xx, -sz / 2 + yy, +sz / 2 + zz)
  87     cube.vertex(+sz / 2 + xx, +sz / 2 + yy, +sz / 2 + zz)
  88     cube.vertex(+sz / 2 + xx, +sz / 2 + yy, -sz / 2 + zz)
  89     
  90     # Top face
  91     
  92     cube.normal(0, 1, 0)
  93     cube.vertex(-sz / 2 + xx, -sz / 2 + yy, -sz / 2 + zz)
  94     cube.vertex(+sz / 2 + xx, -sz / 2 + yy, -sz / 2 + zz)
  95     cube.vertex(+sz / 2 + xx, -sz / 2 + yy, +sz / 2 + zz)
  96     cube.vertex(-sz / 2 + xx, -sz / 2 + yy, +sz / 2 + zz)
  97     
  98     # Bottom face
  99     
 100     cube.normal(0, -1, 0)
 101     cube.vertex(-sz / 2 + xx, +sz / 2 + yy, -sz / 2 + zz)
 102     cube.vertex(+sz / 2 + xx, +sz / 2 + yy, -sz / 2 + zz)
 103     cube.vertex(+sz / 2 + xx, +sz / 2 + yy, +sz / 2 + zz)
 104     cube.vertex(-sz / 2 + xx, +sz / 2 + yy, +sz / 2 + zz)
 105     cube.end()
 106     return cube
 107 
 108 
 109 def defineLights():
 110     # Orange point light on the right
 111     pointLight(150, 100, 0,  200, -150, 0)     
 112     # Blue directional light from the left
 113     directionalLight(0, 102, 255, 1, 0, 0)      
 114     # Yellow spotlight from the front
 115     spotLight(255, 255, 109, 0, 40, 200, 0, -0.5, -0.5, PI / 2, 2) 

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