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

Wednesday, 18 January 2012

Processing-2.0 PovRAY export Noise Surface Sketch

As a proof of concept I re compiled my modified toxiclibs (supports export to PovRAY mesh2) against the latest version of processing-2.0 (svn 9103). I fired up the new version of processing (ant run) and ran  a modified version of the toxicilibs Noise Surface Example sketch.
   1 import toxi.geom.*;
   2 import toxi.geom.mesh.*;
   3 import toxi.volume.*;
   4 import toxi.math.noise.*;
   5 import toxi.processing.*;
   6 
   7 /**
   8  * NoiseSurface demo showing how to utilize the IsoSurface class to
   9  * efficiently visualise volumetric data, in this case using 3D
  10  * SimplexNoise. The demo also shows how to save the generated mesh as
  11  * PovRAY inc file for later use in a PovRAY scene
  12  * Controls: Click mouse button to toggle rendering style between
  13  * shaded/wireframe. Press 's' to save generated mesh as PovRAY inc file
  14  */
  15 
  16 /*
  17  * Copyright (c) 2010 Karsten Schmidt. Modified 2012 by Martin Prout for 
  18  * PovRAY export instead of STL export
  19  *
  20  * This demo & library is free software; you can redistribute it and/or
  21  * modify it under the terms of the GNU Lesser General Public License as
  22  * published by the Free Software Foundation; either version 2.1 of the
  23  * License, or (at your option) any later version.
  24  *
  25  * http://creativecommons.org/licenses/LGPL/2.1/
  26  *
  27  * This library is distributed in the hope that it will be useful, but
  28  * WITHOUT ANY WARRANTY; without even the implied warranty of
  29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  30  * General Public License for more details.
  31  *
  32  * You should have received a copy of the GNU Lesser General Public License
  33  * along with this library; if not, write to the Free Software Foundation,
  34  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  35  */
  36 int DIMX = 192;
  37 int DIMY = 32;
  38 int DIMZ = 64;
  39 float ISO_THRESHOLD = 0.1;
  40 float NS = 0.03f;
  41 Vec3D SCALE = new Vec3D(3, 0.5, 1).scaleSelf(300);
  42 IsoSurface surface;
  43 TriangleMesh mesh;
  44 boolean isWireframe = false;
  45 float currScale = 1;
  46 ToxiclibsSupport gfx;
  47 
  48 void setup() {
  49   size(1024, 768, P3D);
  50   gfx = new ToxiclibsSupport(this);
  51   strokeWeight(0.5f);
  52   VolumetricSpace volume = new VolumetricSpaceArray(SCALE, DIMX, DIMY, DIMZ);
  53   // fill volume with noise
  54   for (int z = 0; z < DIMZ; z++) {
  55     for (int y = 0; y < DIMY; y++) {
  56       for (int x = 0; x < DIMX; x++) {
  57         volume.setVoxelAt(x, y, z, (float) SimplexNoise.noise(x * NS, y * NS, z * NS) * 0.5);
  58       }
  59     }
  60   }
  61   volume.closeSides();
  62   long t0 = System.nanoTime();
  63   // store in IsoSurface and compute surface mesh for the given threshold value
  64   mesh = new TriangleMesh("iso");
  65   surface = new HashIsoSurface(volume, 0.333333);
  66   surface.computeSurfaceMesh(mesh, ISO_THRESHOLD);
  67   float timeTaken = (System.nanoTime() - t0) * 1e-6f;
  68   println(timeTaken + "ms to compute " + mesh.getNumFaces() + " faces");
  69 }
  70 
  71 
  72 void draw() {
  73   background(128);
  74   translate(width / 2, height / 2, 0);
  75   rotateX(mouseY * 0.01);
  76   rotateY(mouseX * 0.01);
  77   scale(currScale);
  78   ambientLight(48, 48, 48);
  79   lightSpecular(230, 230, 230);
  80   directionalLight(255, 255, 255, 0, -0.5, -1);
  81   specular(255, 255, 255);
  82   shininess(16.0f);
  83   beginShape(TRIANGLES);
  84   if (isWireframe) {
  85     stroke(255);
  86     noFill();
  87   } 
  88   else {
  89     noStroke();
  90     fill(255);
  91   }
  92   gfx.mesh(mesh);
  93 }
  94 
  95 void mousePressed() {
  96   isWireframe = !isWireframe;
  97 }
  98 
  99 void keyPressed() {
 100   if (key == '-') {
 101     currScale = max(currScale - 0.1, 0.5);
 102   }
 103   if (key == '=') {
 104     currScale = min(currScale + 0.1, 10);
 105   }
 106   if (key == 's') {
 107     PrintWriter pw = createWriter(sketchPath("noise_surface.inc"));
 108     mesh.saveAsPOV(pw);
 109     pw.flush();
 110     pw.close();
 111     exit();
 112   }
 113 }
 114 
NB now using PrintWriter............. see gist here
PovRAY rendered sketch

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