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

Thursday, 28 January 2010

3D Hilbert Using LSystems and Processing

See the P3D applet running at the open processing gallery.
Revised version 30 January 2010, I'm now using the excellent PeasyCam library, it makes setting up the camera so easy... Use mouse wheel to zoom, mouse drag to rotate etc.
   1 import processing.opengl.*;
   2 import java.text.*;
   3 import lsystem.*;  // My custom LSystem library available at Kenai version 0.5.0
   4                    // http://kenai.com/projects/l-system-utilities/downloads
   5 import peasy.*;    // PeasyCam custom library v0.8.1 available at 
   6                    // http://mrfeinberg.com/peasycam/#download
   7 PeasyCam cam;
   8 Grammar grammar;
   9 
  10 float distance = 72;
  11 int depth = 3;
  12 float theta = radians(90);
  13 float phi = radians(90);
  14 String production = "";
  15 
  16 void setup() {
  17   size(500, 500, OPENGL);
  18   configureOpenGL();  
  19   cam = new PeasyCam(this, 100);  
  20   cam.setMinimumDistance(100);
  21   cam.setMaximumDistance(500);
  22   setupGrammar();
  23   noStroke();  
  24 }
  25 
  26 void configureOpenGL(){
  27   hint(ENABLE_OPENGL_4X_SMOOTH);
  28   hint(DISABLE_OPENGL_ERROR_REPORT);
  29 }
  30 
  31 void setupGrammar(){
  32   grammar = new SimpleGrammar(this, "X");   // this only required to allow applet to call dispose()
  33   grammar.addRule('X', "^<XF^<XFX-F^>>XFX&F+>>XFX-F>X->");
  34   production = grammar.createGrammar(depth);
  35   distance *= pow(0.5, depth);
  36 }
  37 
  38 void draw() {
  39   background(0);
  40   lights();
  41   render();
  42 }
  43 
  44 void render() {
  45   translate(-40, 40, -40);  // center the hilbert empirically set looks good to me
  46   fill(0, 75, 152); 
  47   lightSpecular(204, 204, 204); 
  48   specular(255, 255, 255); 
  49   shininess(1.0); 
  50   CharacterIterator it = new StringCharacterIterator(production);
  51   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  52     switch (ch) {
  53     case 'F': 
  54       translate(0, distance/-2, 0);
  55       box(distance/9, distance, distance/9);
  56       translate(0, distance/-2, 0);
  57       break;
  58     case '-':
  59       rotateX(theta);
  60       break;
  61     case '+':
  62       rotateX(-theta);
  63       break;
  64     case '>':
  65       rotateY(theta);
  66       break;
  67     case '<':
  68       rotateY(-theta);
  69       break;
  70     case '&':
  71       rotateZ(-phi);
  72       break;
  73     case '^':
  74       rotateZ(phi);
  75       break;
  76     case 'X':
  77       break;  
  78     default:
  79       System.err.println("character " + ch + " not in grammar");
  80     }
  81   }
  82 }


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