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

Friday, 28 January 2011

Ball of Confusion translated to processing.py

   1 """
   2 elegant_ball.py
   3 Another opengl sketch from lazydog translated to processing.py
   4 http://lazydog-bookfragments.blogspot.com/2009/05/final-version-of-ball-of-confusion-for.html
   5 """
   6 
   7 
   8 import processing.opengl
   9 import math
  10 import time
  11 
  12 # define PHI as the Golden Ratio constant
  13 PHI = (1 + math.sqrt(5))/2
  14 
  15 def setup():
  16     """
  17     processing setup
  18     """
  19     size(800, 800, OPENGL)
  20     colorMode(RGB, 1)
  21     
  22     
  23 def configureOpengl():
  24     """
  25     configure opengl
  26     """  
  27     hint(ENABLE_OPENGL_4X_SMOOTH)     
  28     hint(DISABLE_OPENGL_ERROR_REPORT)    
  29     
  30     
  31 def draw():
  32     """
  33     processing draw loop
  34     """
  35     #background(0.25)
  36     background(0)
  37     # Move the origin so that the scene is centered on the screen.
  38     translate(width/2, height/2, 0.0)
  39     # Set up the lighting.
  40     setupLights()
  41     # Rotate the local coordinate system.
  42     smoothRotation(5.0, 6.7, 7.3)
  43     # Draw the inner object.
  44     noStroke()
  45     fill(smoothColour(10.0, 12.0, 7.0))
  46     drawIcosahedron(5, 60.0, False)
  47     # Rotate the local coordinate system again.
  48     smoothRotation(4.5, 3.7, 7.3)
  49     # Draw the outer object.
  50     stroke(0.2)
  51     fill(smoothColour(6.0, 9.2, 0.7))
  52     drawIcosahedron(5, 200.0, True)
  53     
  54     
  55 def setupLights():
  56     """
  57     Setup lights
  58     """
  59     ambientLight(0.025, 0.025, 0.025)
  60     directionalLight(0.2, 0.2, 0.2, -1, -1, -1)
  61     spotLight(1.0, 1.0, 1.0, -200, 0, 300, 1, 0, -1, math.pi/4, 20)
  62     
  63     
  64 
  65 def smoothVector(s1, s2, s3):
  66     """
  67     Generate a vector whose components change smoothly over time in the range [ 0, 1 ].
  68     Each component uses a math.sin() function to map the current time in milliseconds somewhere
  69     in the range [ 0, 1 ].A 'speed' factor is specified for each component.
  70     """
  71     mills = time.time() * 0.03 
  72     x = 0.5 * math.sin(mills * s1) + 0.5
  73     y = 0.5 * math.sin(mills * s2) + 0.5
  74     z = 0.5 * math.sin(mills * s3) + 0.5
  75     return PVector(x, y, z)
  76     
  77 
  78 def smoothColour(s1, s2, s3):
  79     """
  80     Generate a colour which smoothly changes over time.
  81     The speed of each component is controlled by the parameters s1, s2 and s3.
  82     """
  83     v = smoothVector(s1, s2, s3)
  84     return color(v.x, v.y, v.z)    
  85     
  86 
  87 def smoothRotation(s1, s2, s3):
  88     """
  89     Rotate the current coordinate system.
  90     Uses smoothVector() to smoothly animate the rotation.
  91     """
  92     r1 = smoothVector(s1, s2, s3) 
  93     rotateX(2.0 * math.pi * r1.x)
  94     rotateY(2.0 * math.pi * r1.y)
  95     rotateX(2.0 * math.pi * r1.z)   
  96     
  97 
  98 def drawIcosahedron(depth, r, spherical):
  99     """
 100     Draw an icosahedron defined by a radius r and recursive depth d.
 101     Geometry data will be saved into dst. If spherical is true then the icosahedron
 102     is projected onto the sphere with radius r.
 103     """
 104 
 105     # Calculate the vertex data for an icosahedron inscribed by a sphere radius 'r'.
 106     # Use 4 Golden Ratio rectangles as the basis.
 107 
 108     h = r / math.sqrt(1.0 + PHI * PHI)
 109     v = [
 110         PVector(0, -h, h * PHI), PVector(0, -h, -h * PHI), PVector(0, h, -h * PHI), PVector(0, h, h * PHI),
 111         PVector(h, -h * PHI, 0), PVector(h, h * PHI, 0), PVector(-h, h * PHI, 0), PVector(-h, -h * PHI, 0),
 112         PVector(-h * PHI, 0, h), PVector(-h * PHI, 0, -h), PVector(h * PHI, 0, -h), PVector(h * PHI, 0, h)
 113     ]    
 114     
 115     # Draw the 20 triangular faces of the icosahedron.
 116     if (not spherical):
 117         r = 0.0          
 118           
 119     beginShape(TRIANGLES)
 120     
 121     drawTriangle(depth, r, v[0], v[7],v[4])
 122     drawTriangle(depth, r, v[0], v[4], v[11])
 123     drawTriangle(depth, r, v[0], v[11], v[3])
 124     drawTriangle(depth, r, v[0], v[3], v[8])
 125     drawTriangle(depth, r, v[0], v[8], v[7])
 126     
 127     drawTriangle(depth, r, v[1], v[4], v[7])
 128     drawTriangle(depth, r, v[1], v[10], v[4])
 129     drawTriangle(depth, r, v[10], v[11], v[4])
 130     drawTriangle(depth, r, v[11], v[5], v[10])
 131     drawTriangle(depth, r, v[5], v[3], v[11])
 132     drawTriangle(depth, r, v[3], v[6], v[5])
 133     drawTriangle(depth, r, v[6], v[8], v[3])
 134     drawTriangle(depth, r, v[8], v[9], v[6])
 135     drawTriangle(depth, r, v[9], v[7], v[8])
 136     drawTriangle(depth, r, v[7], v[1], v[9])
 137     
 138     drawTriangle(depth, r, v[2], v[1], v[9])
 139     drawTriangle(depth, r, v[2], v[10], v[1])
 140     drawTriangle(depth, r, v[2], v[5], v[10])
 141     drawTriangle(depth, r, v[2], v[6], v[5])
 142     drawTriangle(depth, r, v[2], v[9], v[6])
 143     
 144     endShape()  
 145   
 146 
 147 def drawTriangle(depth, r, p1, p2, p3):
 148     """
 149     Draw a triangle either immediately or subdivide it first.
 150     If depth is 1 then draw the triangle otherwise subdivide first.
 151     """
 152     if (depth == 1):
 153         vertex(p1.x, p1.y, p1.z)
 154         vertex(p2.x, p2.y, p2.z)
 155         vertex(p3.x, p3.y, p3.z)
 156         
 157     else:
 158         # Calculate the mid points of this triangle.
 159         v1 = PVector.mult(PVector.add(p1, p2), 0.5)
 160         v2 = PVector.mult(PVector.add(p2, p3), 0.5)
 161         v3 = PVector.mult(PVector.add(p3, p1), 0.5)
 162         if (r != 0.0):
 163             # Project the verticies out onto the sphere with radius r.
 164             v1.normalize()
 165             v1.mult(r)
 166             v2.normalize()
 167             v2.mult(r)
 168             v3.normalize()
 169             v3.mult(r)
 170         ## Generate the next level of detail
 171         depth -= 1
 172         drawTriangle(depth, r, p1, v1, v3)
 173         drawTriangle(depth, r, v1, p2, v2)
 174         drawTriangle(depth, r, v2, p3, v3)
 175         # Uncomment out the next line to include the central part of the triangle.
 176         #drawTriangle(depth, r, v1, v2, v3)  

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