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, 28 November 2011

Rapid PovRAY raytracing from processing

Update 19 May 2011 this entry concerns and earlier version of my original povwriter library, since then, I've made it even easier to to run PovRAY from the processing sketch see this blog entry.

Recently I have been prototyping possible improvements to my povwriter library in ruby-processing, using a control panel to either enter command line arguments, or to create a PovRAY ini file. This was done to make end to end control of the raytracing from processing (with eventual display of rendered image in the processing sketch). It occurred to me that I could make most of these features available in vanilla processing using the existing library. Here is a sketch that ilustrates how to do it (Not shown is the selection of the PovRAY template file, this like the path to povray is set in a properties file, and only needs to be called if you want to change the template).

   1 import povexport.*;
   2 import povexport.povwriter.*;
   3 
   4 PovExporter exporter;
   5 boolean record = false;
   6 boolean display = false;
   7 Process povray;
   8 
   9 void setup() {
  10   size(400, 400, P3D);
  11   exporter = new PovExporter(this);
  12   exporter.setPovrayPath("/usr/local/bin/povray");
  13   exporter.storeSettings();
  14 }
  15 
  16 void draw()
  17 {  
  18   if (record) {
  19     noLoop();
  20     beginRaw(PovExporter.POV, dataPath("FTest.pov"));
  21   }
  22   try {  
  23     if (povray != null && povray.waitFor() == 0) {      
  24       display();
  25     }
  26     else
  27       render();
  28   }
  29   catch(InterruptedException e) {
  30     e.printStackTrace();
  31   }
  32   if (record) {
  33     endRaw();
  34     record = false;
  35     loop();
  36   }
  37 }
  38 
  39 void render() {  // encapsulate initial processing sketch in a function
  40   translate(width/2, height*0.7, width/20);
  41   fill(255, 0, 0);
  42   box(50);
  43   translate(0, -50, 0);
  44   box(50);
  45   translate(50, 0, 0);
  46   box(50);
  47   translate(-50, -50, 0);
  48   box(50);
  49   translate(0, -50, 0);  
  50   box(50);
  51   translate(50, 0, 0);
  52   box(50);
  53 }
  54 
  55 
  56 void rayTrace(int quality) {
  57   String[] jargs = {  
  58     exporter.getPovrayPath(), 
  59     String.format("+W%s +H%s +Q%d +N8 +I%s", width, height, quality, dataPath("FTest.pov"))
  60     };
  61     if (this.record == false) {
  62       povray = exec(jargs);
  63     }
  64 }
  65 
  66 void display() {
  67   background(255);
  68   PImage img = loadImage(dataPath("FTest.png"));
  69   image(img, 0, 0);
  70 }
  71 
  72 void keyPressed() {
  73   switch(key) {
  74   case 'r':    // do this first 
  75   case 'R':
  76     record = true;
  77     break;
  78   case 'p':   // Preview Quality
  79   case 'P': 
  80     rayTrace(5);
  81     break;
  82   case 't':  // High Quality render
  83   case 'T':  
  84     rayTrace(11);  
  85     break;   
  86   }
  87 }
  88 

Original Sketch Displaying PovRAY rendered image

The advantage of this method of working is that you get to see the raytraced image almost immediately, there are alternative methods of working you may wish to look at. One I have been reasonably impressed with is the exporting of the processing sketch as a wavefront 'obj' file (there are libraries to do this, and it is a feature available for toxis libraries). You can then use a program like yaprm to export the object as a PovRAY 'inc' file, the beauty of this is that yaprm exports meshes as a PovRAY mesh2 object. You will normally end up creating three files a PovRAY ini file to start the sketch, which will call PovRAY 'pov' file where you will set up the camera, and other parts of the scene. The 'pov' will then need to include the yaprm generated 'inc' file. My plans for my povwriter library are to generate the 'inc' file directly (ie mesh2 object for meshes and preferable where appropriate use the PovRAY primitives directly).

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