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

Thursday, 21 July 2011

New sphere primitive for use with processing and povwriter

In a previous blog entry I described the hand editing of a PovRAY file to add a sphere to sketch exported from processing to PovRAY, this was because the sphere primitive in processing is rather crude and does not export well for ray tracing. Now I include the possibility of providing an alternative sphere within the processing sketch that exports rather better, see version 0.58 of my povwriter library. The sphere is loosely based on lazydogs Ball of Confusion, the sphere is essentially an inflated icosahedron, which has the faces recursively subdivided to create greater detail. Here is an examples sketch using the alternative sphere primitive, the sphere detail is set somewhat arbitrarily according to the size of the sphere, and does not respond to the processing sphereDetail() directive:-

/** 
 * Copyright (c) 2011 Martin Prout
 * 
 * This demo & library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * http://creativecommons.org/licenses/LGPL/2.1/
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


import povexport.*;
import povexport.povwriter.*;

/**
* This sketch is adapted from the DXF export processing library example
* But using a custom sphere based on an inflated recursive icosahedron
*/

boolean record = false;
PovExporter export;

void setup() {
  size(400, 400, P3D);
  export = new PovExporter(this);
  export.chooseTemplate();
  //export.setPovrayPath("/home/tux/bin/jedit"); //use this once to set povray path
  export.storeSettings(); // NB call storeSettings() after changes
  noStroke();
  sphereDetail(18);
}

void draw()
{ 
  lights();        // this needs to be outside the record loop
  if (record) {    
    noLights();    // let PovRAY do the lighting
    noLoop();      // don't loop while recording sketch
    beginRaw("povexport.RawPovray", dataPath("balls.pov"));
  }
  render();
  if (record) {
    endRaw();
    record = false;
    loop();
  }
}

void render() {  
  background(0);
  translate(width*0.7, height*0.7, width/20);
  for (int y = -1; y < 1; y++) {
    for (int x = -1; x < 1; x++) {
      for (int z = -1; z < 1; z++) {
        pushMatrix();
        translate(140*x, 140*y, -140*z);
        fill(random(255), random(255), random(255)); // a nice test for my colorFactory class
        export.sphere(60); // use a better sphere primitive for ray tracing
        popMatrix();
      }
    }
  }
}

void keyPressed() {
  switch(key) {
  case 'r': 
    record = true;
    break;
  case 'e':             // edit pov output and/or run povray (use commando file to run from jedit)
     String[] jargs = {"/home/tux/bin/jedit", dataPath("balls.pov")};
     //String[] jargs = {export.getPovrayPath(), dataPath("output.pov")}; // use this after path has been set
      //open(jargs); // this processing shortcut doesn't work for me on my linux box hence.....
      Runtime runtime = Runtime.getRuntime();
    try {
      if (this.record == false) {
        runtime.exec(jargs); // open jedit with FTest.pov file
      }
    } 
    catch (IOException ex) {
      ex.printStackTrace();
    }
    break;
  }
}


PovRAY rendered processing sketch

PovRAY rendered custom processing sphere in a Random Cage
using WBlut hemesh library to create the random cage (code by WBlut)

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