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

Recursive Image in processing.py

   1 """
   2 mirror.py was originally create by lazdog in vanilla processing
   3 http://lazydog-bookfragments.blogspot.com/2009/05/mirror-of-confusion.html
   4 Modified by monkstone to run in processing.py, note deliberate use of pythonic time()
   5 rather than processing millis(). Also using python math.sin() vs processing sin()
   6 """
   7 import math
   8 import time
   9 import processing.opengl
  10 
  11 #
  12 # Dimensions of screen.
  13 #
  14 KWIDTH = 480
  15 KHEIGHT = 320
  16 
  17 #
  18 # Dimensions of texture.
  19 # For fast PCs reduce or remove the denominator.
  20 #
  21 KTEXTURE_WIDTH = KWIDTH/2
  22 KTEXTURE_HEIGHT = KHEIGHT/2
  23 
  24 #
  25 # declare history at module level, used to store the current 
  26 # frame buffer as a texture. Initialize now for use in draw.
  27 #
  28 history = None
  29 now = time.time()
  30 
  31 def setup():  
  32   """
  33   P3D doesn't work well in this sketch!
  34   """
  35   size(KWIDTH, KHEIGHT, OPENGL)
  36   hint(DISABLE_OPENGL_ERROR_REPORT)
  37   hint(ENABLE_OPENGL_4X_SMOOTH)
  38   colorMode(RGB, 255)
  39   smooth()
  40   global history
  41   
  42   history = createImage( KTEXTURE_WIDTH, KTEXTURE_HEIGHT, RGB )
  43   textureMode( NORMALIZED )
  44   
  45   
  46 def draw():
  47   """
  48   Is the processing draw loop, require history as global, because it gets updated
  49   in every loop (and is initialized in setup)
  50   """
  51   
  52   global history, now 
  53   background(252) 
  54   translate(width/2, height/2)
  55   
  56   #
  57   # Draw the ground - use a simple grid for detail.
  58   #
  59   # All numbers are fudged to make the grid and ground look about right.
  60   #
  61   hint(DISABLE_DEPTH_TEST)
  62 
  63   noStroke()
  64   fill(240, 189, 180)
  65   rect(-width/2, 24, width/2, 200)
  66   
  67   fill(200, 220, 255)
  68   rect(0, 24, width, 200)
  69   
  70   stroke(0)
  71   strokeWeight(1.0)
  72   
  73   for i in range(0, 10):
  74     
  75       z = -2000 + i * 200
  76       line(-width*10, 200, z, width* 10, 200, z)
  77       
  78       x = i* 200
  79       
  80       line(-x, 200, -2000, -x, 200, 200)
  81       line(x, 200, -2000, x, 200, 200)
  82       
  83       
  84   hint(ENABLE_DEPTH_TEST)
  85   
  86   #
  87   # Draw the mirror.
  88   #
  89   # The mirror is animated by rotating around the y-axis.
  90   # The mirror is drawn using the last frame buffer as the reflection in the mirror.
  91   # The view in the mirror is dimmed slightly by drawing an alpha rectangle over it.
  92   # Also, the mirror is oultined to make it stand out a bit.
  93   #
  94   pushMatrix()
  95   
  96   #  rotateX( 0.03 * sin( millis() * 0.0023 )  )
  97   rotateY(0.3 * sin(time.time() - now))
  98   translate(-width/2, -height/2, -200)
  99   
 100   noStroke()
 101   beginShape()
 102   texture(history)
 103   vertex(0.0, 0.0, 0, 0)
 104   vertex(width, 0, 1, 0)
 105   vertex(width, height, 1, 1)
 106   vertex(0, height, 0, 1)
 107   endShape()
 108   
 109   fill(0, 0, 0, 30)
 110   beginShape()
 111   vertex(0.0, 0.0)
 112   vertex(width, 0)
 113   vertex(width, height)
 114   vertex(0, height )
 115   endShape()
 116   
 117   stroke(200, 200, 200)
 118   strokeWeight(2)
 119   noFill()
 120   beginShape()
 121   vertex(0, 0)
 122   vertex(width, 0)
 123   vertex(width, height)
 124   vertex(0, height)
 125   endShape(CLOSE) 
 126   popMatrix()
 127   
 128   #
 129   # Draw the foreground scene.
 130   #
 131   translate(200 * math.sin(time.time() - now), 200 * math.sin((time.time() - now)* 1.3))
 132   
 133   lights()
 134   noStroke() 
 135   fill(238, 80, 70)
 136   sphere(100)
 137   
 138   #
 139   # Use the current frame as the texture for the
 140   # mirror in the next frame.
 141   #
 142   # if ( frameCount % 4 == 0 )
 143   history.copy(get(), 0, 0, width, height, 0, 0, KTEXTURE_WIDTH, KTEXTURE_HEIGHT)


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