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

Tuesday, 20 December 2011

"Instant PovRAY" rendering of processing sketches

Actually instant is a bit of strong claim but with a moderate quality setting PovRAY tracing can be pretty quick. My revised povwriter library makes it possible to directly render processing sketches from the processing ide. However as the sketch.pov and associated sketch.ini are saved it is also possible to render them again just using PovRAY. This is convenient as once the scene has the overall look you want, you can then edit texture etc. This could be done in the processing GUI, however particularly Linux users (no GUI) may want take a look at jEdit which has syntax highlighting for PovRAY built in, and I have created a commando file to make it easy to run PovRAY from jEdit. More excellent hemesh stuff here.
I'm looking for volunteers with MAC or Windows Systems to test the library. Here is an example of the library in action, tested with processing-1.5.1 and PovRAY-3.7-RC3 on linux:-

   1 import povexport.*;
   2 import povexport.povwriter.*;
   3 import wblut.math.*;
   4 import wblut.processing.*;
   5 import wblut.hemesh.creators.*;
   6 import wblut.hemesh.core.*;
   7 import processing.opengl.*;
   8 import wblut.hemesh.modifiers.*;
   9 
  10 HE_Mesh mesh;
  11 HE_Mesh invMesh;
  12 WB_Render render;
  13 int res;
  14 boolean record = false;
  15 PovExporter export;
  16 Process povray;
  17 void setup() {
  18   size(800, 800, OPENGL);
  19   export = new PovExporter(this);
  20   smooth();
  21   res=20;
  22   float[][][] values=new float[res+1][res+1][res+1];
  23   for (int i = 0; i < res+1; i++) {
  24     for (int j = 0; j < res+1; j++) {
  25       for (int k = 0; k < res+1; k++) {
  26         values[i][j][k]=2.1*noise(0.35*i, 0.35*j, 0.35*k);
  27       }
  28     }
  29   }
  30 
  31   HEC_IsoSurface creator=new HEC_IsoSurface();
  32   creator.setResolution(res, res, res);// number of cells in x,y,z direction
  33   creator.setSize(400.0/res, 400.0/res, 400.0/res);// cell size
  34   creator.setValues(values);// values corresponding to the grid points
  35   // values can also be double[][][]
  36   creator.setIsolevel(1.0);// isolevel to mesh
  37   creator.setInvert(false);// invert mesh
  38   creator.setBoundary(100.0);// value of isoFunction outside grid
  39   // use creator.clearBoundary() to rest boundary values to "no value".
  40   // A boundary value of "no value" results in an open mesh
  41 
  42   mesh=new HE_Mesh(creator);
  43   mesh.modify(new HEM_Smooth().setIterations(10).setAutoRescale(true));
  44   creator.setInvert(true);
  45 
  46   invMesh=new HE_Mesh(creator);
  47   invMesh.modify(new HEM_Smooth().setIterations(10).setAutoRescale(true));
  48   render=new WB_Render(this);
  49   // Quality 1 ...7... 11 (low ... medium ... high)
  50   export.createIniFile(dataPath("twin_iso.ini"), 9); 
  51   export.writeIniFile();
  52 }
  53 
  54 void draw() {
  55   background(120);
  56   lights();
  57   // this needs to be outside the record loop
  58   if (record) {    
  59     noLights();    // let PovRAY do the lighting
  60     noLoop();      // don't loop while recording sketch
  61     beginRaw(PovExporter.POV, dataPath("twin_iso.pov"));
  62   }
  63   try {  
  64     if (povray != null && povray.waitFor() == 0) {      
  65       display();
  66     }
  67     else {
  68       translate(400, 400, 0);
  69       rotateY(mouseX*1.0f/width*TWO_PI);
  70       rotateX(mouseY*1.0f/height*TWO_PI);
  71       noStroke();
  72       fill(255);
  73       render.drawFaces(mesh);
  74       fill(255, 0, 0);
  75       render.drawFaces(invMesh);
  76       stroke(0);
  77       render.drawEdges(mesh);
  78       stroke(255, 0, 0, 80);
  79       render.drawEdges(invMesh);
  80     }
  81   }
  82   catch(InterruptedException e) {
  83     e.printStackTrace();
  84   }
  85   if (record) {
  86     endRaw();
  87     record = false;
  88     loop();
  89   }
  90 }
  91 
  92 
  93 void display() {
  94   PImage img = loadImage(dataPath("twin_iso.png"), "png");
  95   background(img);
  96 }
  97 
  98 
  99 
 100 void keyPressed() {
 101   switch(key) {
 102   case 'r':  // create twin_iso.pov file 
 103   case 'R':  
 104     record = true;
 105 
 106     break;
 107   case 't':  // run PovRAY calling twin_iso.ini
 108   case 'T':  
 109     povray = export.rayTrace(); 
 110     break;
 111   case 'e':
 112   case 'E':
 113     if (this.record == false) {
 114       String[] args = {
 115         export.getJEditPath(), dataPath("twin_iso.pov")
 116         };
 117         exec(args); // open twin_iso.pov with jEdit
 118     }
 119     break;
 120   }
 121 }
 122 

This was not an instant render

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