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, 1 December 2010

Sunflow Raytracing a 3D Hilbert in processing

Here is an example of the original processing/sunflow interface (which despite rumours to the contrary still works with the latest development version of processing-0191, I also think I might have a fix for the hryk fork problem with triangle render, I've renamed the Integer[] version of flatten to flattenI, which otherwise conflicts with the Float[] version).
Because I have been experimenting with the more recent interfaces, notably joons and the sunflowapiapi libraries, I put the sunflow and janino libraries in separate library (this requires a specific import of org.sunflow.*) which means only one sunflow.jar for the three interfaces. Currently joons requires an earlier version of sunflow, but I have modified it to use the common version 0.07.3. Not content with that I have updated the sunflow library to be compatible with the latest version of janino.
You may note that there is a bit of clipping in the ray traced image below (which exposes the box ends), I think it looks OK, plus it takes too long to render. I have found that previewing the scene, in P3D mode, is absolutely no guide to the size of object rendered by sunflow (both the export to sunflow and the internal ray tracing in the latest StructureSynth by comparison is far superior in this regard, but not in its export to povray).

   1 import lsystem.turtle.*;      // version 0.6.7 of the LSystem library is modular
   2 import lsystem.collection.*;
   3 import lsystem.*;
   4 
   5 import java.text.*;
   6 
   7 import org.sunflow.*;
   8 import hipstersinc.*;
   9 import hipstersinc.sunflow.*;
  10 
  11 Grammar grammar;
  12 P5Sunflow sunflow;
  13 float distance = 50;
  14 int depth = 3;
  15 float theta = radians(90);
  16 float phi = radians(90);
  17 String production = "";
  18 
  19 void setup() {
  20   size(640, 512, "hipstersinc.P5Sunflow");
  21   sunflow = (P5Sunflow)g;
  22   setupGrammar();
  23   noStroke();
  24   noLoop();
  25 }
  26 
  27 void draw() {
  28   background(255);
  29   setupCamera();
  30   setupScene();
  31   // Adjust perspective
  32   translate(width/2, height/2, -10);
  33   rotateX(QUARTER_PI);
  34   rotateZ(QUARTER_PI);
  35   translate(-width/5, height * 0.6);
  36   render();
  37 }
  38 
  39 void setupCamera() {
  40   sunflow.camera.setType(SunflowCamera.THINLENS);
  41 }
  42 
  43 void setupScene() {
  44   sunflow.scene.setAaMax(6);
  45   sunflow.scene.setAaSamples(8);
  46 }
  47 
  48 void setupGrammar() {
  49   grammar = new SimpleGrammar(this,  "X");   
  50   grammar.addRule('X',  "^<XF^<XFX-F^>>XFX&F+>>XFX-F>X->");
  51   production = grammar.createGrammar(depth);
  52 }
  53 
  54 void render() {  
  55   fill(0,  75,  152); 
  56   CharacterIterator it = grammar.getIterator(production);
  57   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  58     switch (ch) {
  59     case 'F': 
  60       translate(0,  distance/-2,  0);
  61       box(distance/5,  distance,  distance/5);
  62       translate(0,  distance/-2,  0);
  63       break;
  64     case '-':
  65       rotateX(theta);
  66       break;
  67     case '+':
  68       rotateX(-theta);
  69       break;
  70     case '>':
  71       rotateY(theta);
  72       break;
  73     case '<':
  74       rotateY(-theta);
  75       break;
  76     case '&':
  77       rotateZ(-phi);
  78       break;
  79     case '^':
  80       rotateZ(phi);
  81       break;
  82     case 'X':
  83       break;  
  84     default:
  85       System.err.println("character " + ch + " not in grammar");
  86     }
  87   }
  88 }


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