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).

Friday, 4 November 2011

coffeescript, it's about time I tried it out

Well I think Jeremy Ashkenas (Jashkenas) is a very bright chap, and he has invented this thing called coffescript that turns writing javascript from something you might hate to do (but might have to do, to feed the family) to something enjoyable/satisfying. As I'm a linux user I don't have access to textmate, which is the obvious choice for a mac user (there is SublimeText2 but that is still in development, and they still want you to pay for it!!!). I'm comfortable with JEdit so I chose that, I've even created a little commando script so I can compile/run scripts from the editor (a coffee.xml syntax highlight mode is available here).
Here is something distasteful in coffeescript that I created earlier:-

fred = "wanker"
fred is "wanker"
fred isnt "tosser"
This is compiled to this javascript:-
(function() {
  var fred;
  fred = "wanker";
  fred === "wanker";
  fred !== "tosser";
}).call(this);
Actually this script is obviously completely pointless, but makes more sense when each command is run consecutively in the REPL, which is great way of exploring coffeescript syntax (just like in python or ruby).
tux@monkstone:~$ coffee
coffee> fred = "wanker"
'wanker'
coffee> fred is "wanker"
true
coffee> fred isnt "tosser"
true
By the way you don't even have to install coffescript to try it out, try it here http://jashkenas.github.com/coffee-script/ Further coffee/javascripts can/do work just as well on the server side as the client side check out the examples in this free book. Look what I've just found, as I thought someone would have done it already combining coffeescript and processing.js, I will definitely be giving it a go.

Wednesday, 2 November 2011

Developing Processing-2.0 Applications in NetBeans

See updated version here.

Processing-2.0a2 has just been released, and I have taken the opportunity to create some tools for those wishing to develop processing applications and/or processing libraries using netbeans (7.1beta in my case). With the advent of processing-2.0 P3D is essentially dead, and such sketches are rendered via OPENGL. Therefore when you create a "Processing library" (essentially you just need to define the path to the required jars) then you should now also add the opengl jars as described in my previous post. You should also define the path to the native binaries -Djava.lib.path={path}/library at runtime. I have produced some template files that you can use to customise NetBeans to make it even easier to develop processing using NetBeans here:-


To get the template menu to display a nice processing icon use a 16x16 png image and edit .nbattrs to match your system here is mine:-
processing.png




You could just put try putting these files in the following directory {home}.netbeans/7.1beta/config/Templates/Classes folder (or the equivalent for your system) and the files should show up as templates (you will then just need to adjust the url to your processing.png file).NB: If you have already created custom templates, then edit your .nbattrs file, instead of replacing it.

Here's an image of a sketch produced using the P5Main.java template, I have specified P3D as mode to get the sketch to show Thread support is activated for P3D, of course this particular sketch would also run as the default, java2d:-

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