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

Monday, 13 August 2012

BenTilbert Processing-2.0 (featuring povwriter2)

Update March 2013 I've switched back to java.net for the processing-2.0 compatible version of my processing library.

Processing 2.0 is coming, and I've re-compiled my LSystem library against the latest version, this sketch doesn't require that (since it is only really needed with the 3DTurtle implementation). Other changes to note is that I've now dropped createGrammar syntax in favour of generateGrammar for version 0.90. The main difference is that createGrammar returned a String (production) whereas generateGrammar returns a void. Access to the production String is via the getIterator() function that now does not require any parameters (previously production String). Here's BenTilbert featuring these changes and export to povray using my povwriter2 library. Update 10th October 2012, lines 50 and 51 are not required in latest revision of code.
   1 import povexport2.*;
   2 import povexport2.povwriter.*;
   3 import lsystem.util.*;
   4 import lsystem.*;
   5 
   6 PovExporter export;
   7 float HALF = PI/360;   // introduce half a degree error
   8 String axiom = "A";
   9 float THETA = HALF_PI + HALF;
  10 float PHI = HALF_PI - HALF;
  11 ArcBall arcball;
  12 Grammar grammar;
  13 boolean record = false;
  14 final float[] ADJUST = {  // adjust center max depth = 5
  15   0.0, 0.5, 1.5, 3.5, 7.5, 15
  16 };
  17 float len = 300;
  18 int depth = 4;   // nice depth for povray (else for processing 3 is good)
  19 
  20 void setup()
  21 {
  22   size(600, 600, P3D);
  23   arcball = new ArcBall(this);
  24   export = new PovExporter(this);
  25   export.chooseTemplate();
  26   export.setPovrayPath("/usr/local/bin/povray"); //use this once to set povray path
  27   // Quality PREVIEW, MEDIUM, HIGH, HIGHEST, GRAYSCALE
  28   export.createIniFile(dataPath("benTilbert.ini"), Quality.HIGHEST);
  29   export.addDeclareOption("TransYP5", "25"); // custom declare,translates Y axis in PovRAY
  30   noStroke();
  31   grammar = new SimpleGrammar(this, axiom);
  32   grammar.addRule('A', "B>F<CFC<F>D+F-D>F<1+CFC<F<B1^");
  33   grammar.addRule('B', "A+F-CFB-F-D1->F>D-1>F-B1>FC-F-A1^");
  34   grammar.addRule('C', "1>D-1>F-B>F<C-F-A1+FA+F-C<F<B-F-D1^");
  35   grammar.addRule('D', "1>CFB>F<B1>FA+F-A1+FB>F<B1>FC1^");
  36   grammar.generateGrammar(depth);
  37   len *= pow(0.5, depth);  // scale hilbert according to depth
  38 }
  39 
  40 void draw() { 
  41   if (export.traced()) { // display traced image
  42     display();
  43   } 
  44   else {
  45     background(50, 50, 150);
  46     lights(); // NB: lights outside export
  47     ambientLight(30, 30, 30); 
  48     directionalLight(60, 60, 60, 0, 0, 1);    
  49     export.beginRaw(dataPath("benTilbert.pov"));
  50     translate(width/2, height/2); 
  51     arcball.update();
  52     export.setHue(Hue.STEEL); // sets fill processing sketch and finish PovRAY
  53     render();
  54     export.endRaw();  //end tracing
  55   }
  56 }
  57 
  58 void render() {
  59   int repeats = 1;
  60   translate(ADJUST[depth]*len, -ADJUST[depth]*len, ADJUST[depth]*len);
  61   CharacterIterator it = grammar.getIterator();
  62   for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
  63     switch (ch) {
  64     case 'F':    
  65       translate(0, 0, -len / 2);
  66       box(5, 5, len - 1.6);     
  67       translate(0, 0, -len / 2);
  68       box(5);
  69       break;
  70     case '+':
  71       rotateX(THETA * repeats);
  72       repeats = 1;
  73       break;
  74     case '-':
  75       rotateX(-THETA * repeats);
  76       repeats = 1;
  77       break; 
  78     case '>':
  79       rotateY(THETA * repeats);
  80       repeats = 1;
  81       break;
  82     case '<':
  83       rotateY(-THETA * repeats);
  84       repeats = 1;
  85       break;
  86     case '^':
  87       rotateZ(PHI * repeats);
  88       repeats = 1;
  89       break;
  90     case '1':
  91       repeats += 1; 
  92     case 'A':
  93     case 'B':
  94     case 'C':
  95     case 'D':
  96       break;
  97     default:
  98       System.err.println("character " + ch + " not in grammar");
  99     }
 100   }
 101 }
 102 
 103 /**
 104  * Display ray traced image in sketch window
 105  */
 106 void display() {
 107   background(loadImage(dataPath("benTilbert.png")));
 108 }
This is the generated povray ini file:-
   1 ; benTilbert.ini
   2 
   3 Input_File_Name=/home/sid/sketchbook/benTilbert/data/benTilbert.pov
   4 Output_File_Name=/home/sid/sketchbook/benTilbert/data/benTilbert.png
   5 Width=600
   6 Height=600
   7 Declare=ASPECT_RATIO=1.0000
   8 Declare=ZDEPTH=173.2050
   9 Quality=11
  10 Display=off
  11 Output_File_Type=N8
  12 Max_Image_Buffer_Memory=516
  13 Antialias=on
  14 Sampling_Method=2
  15 Antialias_Threshold=0.3
  16 Declare=TransYP5=25
And an extract of the generated pov file:-
   1 // Persistence Of Vision Ray Tracer Scene Description File
   2 // File:  Simple Scene <template for povwriter2 version-0.57>
   3 // Vers: 3.7
   4 // Date: July 2012
   5 // Auth: Martin Prout 
   6 
   7 // +w300 +h300
   8 
   9 #version 3.7;
  10 
  11 global_settings{
  12   assumed_gamma 1.0
  13   radiosity{
  14     pretrace_start 0.04
  15     pretrace_end 0.01
  16     count 200
  17     recursion_limit 3
  18     nearest_count 10
  19     error_bound 0.5
  20   }
  21 }
  22 
  23 #include "colors.inc"
  24 #include "skies.inc"
  25 
  26 // --------------- begin declare adjustments to scene -------------------------
  27 
  28 // Default values in case not using ini file or they are not declared there ---
  29 #ifndef (ScaleP5)
  30 #declare ScaleP5 = 1.0;     // scale factor
  31 #end
  32 #ifndef (TransXP5)
  33 #declare TransXP5 = -10;    // translate in X axis
  34 #end
  35 #ifndef (TransYP5)
  36 #declare TransYP5 = 40;    // translate in Y axis
  37 #end
  38 #ifndef (TransZP5)
  39 #declare TransZP5 = -15;    // translate in Z axis
  40 #end
  41 #ifndef (RotYP5)
  42 #declare RotYP5 = 15;       // rotate around Y axis
  43 #end
  44 #ifndef (RotXP5)
  45 #declare RotXP5 = 0;        // rotate around X axis
  46 #end
  47 #ifndef (RotZP5)
  48 #declare RotZP5 = 0;         // rotate around Z axis
  49 #end
  50 // ---------------end of declare adjustments to scene
  51 
  52 //----------------declare default colors
  53 #declare CornellRed = rgb<0.57, 0.025, 0.025>;  // Right wall Cornell Box
  54 #declare CornellGreen = rgb<0.025, 0.236, 0.025>; // Left wall Cornell Box
  55 #declare LineFill = rgb<1.0, 0.9, 0.8>; // polygon outline color
  56 #declare LineCol = rgb<0.3, 0.225, 0.12>; // polygon outline color
  57 #declare TransFill = rgbf<1.0, 0.95, 0.8, 0.7>; // polygon outline color
  58 #declare BuddhaGold = rgb<195, 160, 4>/255; // Custom Gold
  59 //----------------end declare default colors #### paste sketch after this ####
  60 
  61 //----------------begin declare finish
  62 
  63 #declare Finish0=finish{diffuse 0.75 emission 0}  // Stroke
  64 #declare Finish1=finish{diffuse 0.78 emission 0}  // Cornell Box Light Patch
  65 #declare Finish2=finish{emission 0.1 phong 0.5 phong_size 10.0}   // Processing object finish
  66 
  67 //----------------end declare finish
  68 
  69 #declare SWIDTH=0.4; // processing equivalent is stroke width
  70 
  71 //----------------begin declare pigment
  72 
  73 #declare Pigment0 = pigment{rgb<1, 1, 1>}  // Cornell Box Light Patch
  74 
  75 //----------------end declare pigment
  76 
  77 //----------------begin declare texture
  78 
  79 #declare WhiteT=texture{pigment{White} finish{Finish0}} //
  80 #declare RedT=texture{pigment{CornellRed} finish{Finish0}}   // Cornell Box Walls
  81 #declare GreenT=texture{pigment{CornellGreen} finish{Finish0}} //
  82 #declare Texture0=texture{pigment{LineFill} finish{Finish1}} // this is for 'stroke' color
  83 
  84 //----------------end declare texture
  85 
  86 #ifndef (ASPECT_RATIO)
  87 #declare ASPECT_RATIO=1.25;
  88 #end
  89 
  90 //----------------declare scene Settings
  91 #declare camera0 = camera {            // define additional cameras to suit viewing preferences
  92    location <-1.5, 30.0, -150.0>
  93    direction <0.0, 0.0, 2.0>
  94    up  <0.0, 1.0, 0.0>
  95    right <ASPECT_RATIO, 0.0, 0.0>
  96    look_at <0.0, 25.0, 35.0>
  97 }
  98 
  99 #declare light0 = light_source { <100.0, 100.0, -200.0> colour White }
 100 
 101 #declare ground0 = plane { <0.0, 1.0, 0.0>, 0.0  // a reflective ground plane
 102    pigment { NeonBlue }
 103    finish {reflection 0.15}
 104 }
 105 
 106 //------------------end of declare scene settings
 107 
 108 // -----------------set the scene
 109 
 110 camera { camera0 }              // ------------------------------------------
 111                                 //  The use of declared values makes it possible to easily 
 112 light_source{ light0 }          //  change the way the scene is rendered. Just define an
 113                                 //  additional camera say for your template, and do the
 114 sky_sphere{ S_Cloud3 }          //  substitution here. Retain the original definition and
 115                                 //  you can easily backtrack. Definitions can also be saved
 116 plane{ ground0 }                //  as included file see colors.inc for an example.
 117                                 // ---------------------------------------------  
 118 // -----------------end set the scene
 119 
 120 // -----------------processing sketch begins here------------------------
 121 
 122 // ----------------- triangle and line macros -----------------------------------------------------------
 123 
 124 /**
 125 Adjust global triangle mesh here, or at #declare for Finish2, if you change my_hue you will lose color data
 126 from the processing sketch, however as sketch colors are also #declare, you can safely change those declares, 
 127 unless they are used in other parts of processing scene (eg checker color Red, Blue). You can change these 
 128 as well but just remember to hand edit them back to original (this is particularly important for light source)
 129 Note a built in scale to 20% of original sketch size, units in PovRAY are different
 130 */
 131 
 132 #macro my_triangle ( x1, y1, z1, x2, y2, z2, x3, y3, z3, texture0 )  
 133      triangle{<x1 * 0.2,y1 * 0.2,z1 * 0.2>,<x2 * 0.2,y2 * 0.2,z2 * 0.2>,<x3 * 0.2,y3 * 0.2,z3 * 0.2> 
 134      texture{ texture0 } }  
 135 #end 
 136 
 137 /**
 138 Adjust global line properties here, or at #declare for SWIDTH, LineCol, Texture0
 139 Note a built in scale to 50% of original sketch size, units in PovRAY are different
 140 */
 141 
 142 #macro my_line ( x1, y1, z1, x2, y2, z2 )  
 143 blob { threshold 0.65 cylinder { <x1* 0.2 , y1 * 0.2, z1 * 0.2>, <x2 * 0.2, y2* 0.2, z2 * 0.2>, SWIDTH 1 pigment{ LineCol } texture{ Texture0 } } }
 144 #end 
 145 
 146 //------------------ end of macro definitions --------------------------------------
 147 
 148 
 149 
 150 
 151 
 152 // --------------Begin declare colours from sketch
 153 // If empty then sketch colors already colored
 154 // --------------end of declare colours for sketch
 155 
 156 
 157 // --------------processing sketch begins here
 158 
 159 
 160 #include "metals.inc"
 161 #declare Texture1 = texture{ pigment{P_Chrome2} finish{ F_MetalA }} 
 162 union{
 163 my_triangle(-117.87892, -196.33084, 491.2763, -116.20359, -200.97145, 492.08743, -122.260086, -197.54916, 493.35504, Texture1)
 164 my_triangle(-116.20359, -200.97145, 492.08743, -120.58476, -202.18979, 494.16614, -122.260086, -197.54916, 493.35504, Texture1)
 165 my_triangle(-116.20359, -200.97145, 492.08743, -122.14319, -205.79828, 476.73993, -120.58476, -202.18979, 494.16614, Texture1)
 166 my_triangle(-122.14319, -205.79828, 476.73993, -126.52435, -207.0166, 478.81866, -120.58476, -202.18979, 494.16614, Texture1)
 167 my_triangle(-122.14319, -205.79828, 476.73993, -123.81851, -201.15765, 475.92883, -126.52435, -207.0166, 478.81866, Texture1)
 168 my_triangle(-123.81851, -201.15765, 475.92883, -128.19968, -202.37599, 478.00754, -126.52435, -207.0166, 478.81866, Texture1)
 169 my_triangle(-123.81851, -201.15765, 475.92883, -117.87892, -196.33084, 491.2763, -128.19968, -202.37599, 478.00754, Texture1)
 170 my_triangle(-117.87892, -196.33084, 491.2763, -122.260086, -197.54916, 493.35504, -128.19968, -202.37599, 478.00754, Texture1)
 171 my_triangle(-123.81851, -201.15765, 475.92883, -122.14319, -205.79828, 476.73993, -117.87892, -196.33084, 491.2763, Texture1)
 172 my_triangle(-122.14319, -205.79828, 476.73993, -116.20359, -200.97145, 492.08743, -117.87892, -196.33084, 491.2763, Texture1)
 173 my_triangle(-122.260086, -197.54916, 493.35504, -120.58476, -202.18979, 494.16614, -128.19968, -202.37599, 478.00754, Texture1)
 174 my_triangle(-120.58476, -202.18979, 494.16614, -126.52435, -207.0166, 478.81866, -128.19968, -202.37599, 478.00754, Texture1)
 175 ...
 176 ...
 177 ...
 178 // -----------------------------End of processing scene
 179 // -----------------------------Adjust the processing scene
 180 translate<TransXP5, TransYP5, TransZP5>
 181 rotate<RotXP5, RotYP5, RotZP5>
 182 scale<ScaleP5, ScaleP5, ScaleP5>
 183 }
Here is the genereated image:-

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