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

Sunday, 29 January 2012

How to export from processing to povray

When using toxiclibs libraries:-

If you are up for a bit of hacking you could recompile toxiclibscore.jar to include my POVWriter.java and a modified TriangleMesh.java library classes code available as a gist https://gist.github.com/1639360. Depends on creation of mesh. Revised 29 February 2012 download my povmesh library here.

When using hemesh libraries:-

If you are up for a bit of hacking you could recompile hemesh.jar to include my HET_POVWriter.java a modified HET_Export.java library classes code available as a gist https://gist.github.com/1658952. Depends on creation of mesh.  Latest version of hemesh 1.7 beta now includes POVWriter functionality.
update 21 Feb 2012

In both these cases the geometry is exported as 1 or more "declared" mesh2 objects in a povray inc, so a pov file needs to be created to include and use the mesh2 objects and to set the scene (camera, background etc) and to add texture. It doesn't harm to create an ini file to call the pov file (the ini file can be used to set the quality etc). It may be necessary to scale/and or translate the mesh2 objects in the pov file. Otherwise upto and including processing-1.5.1 use my custom povwriter library (most 3d sketches, captures color information) get the povwriter library here http://java.net/projects/povwriter/pages/Home or since processing-2.0a6 https://github.com/monkstone/povwriter2/wiki.

What to expect, rendering using my povwriter library use a union of triangles, usefully lines/stroke can be output as a blob (a cylinder with a sphere at either end. You see triangle facets. The library is designed to allow for ray-tracing direct from the processing ide, currently both geometry and scene description are output to a pov file (with camera adjusted to aspect ratio using a declared variable in the ini file), the quality settings (accessible in the processing ide as enum constant) are output to an ini file.

Using my modified hemesh and toxiclibs library the mesh is exported in povray mesh2 format, and the default is include normals, hence output is rendered as a smooth surface, but ther is the option to not include the normals and facets are visible.

Here's my latest experiment exploring toxis implicit mesh function.

Sunday, 22 January 2012

Exporting Hemesh HE_Mesh directly as PovRAY mesh2

Well there is another library that I've hacked to export directly as a PovRAY mesh2. Update 8 March 2012, export to to PovRAY included in library since version 1.70 beta, signature change from saveAsPOV to saveToPov.
Here is the result of the TwinIso example from the hemesh library (albeit slightly modified) exported as a PovRAY mesh2, here's the additional/modified code.

Here is the sketch code:-
PovRAY rendered with silver and copper textures

Thursday, 19 January 2012

Twin toxiclibs Voxelizer sketches, united in one PovRAY scene

Most kudos to Karsten Schmidt for his excellent toxiclibs libraries. I have hacked the toxiclibs core library to export PovRAY mesh2, and I think the results are excellent. Here's toxi voxelizer sketch (well twice run actually) rendered with Povray 3.7 as red marble. The mesh2 matches PovRAY internals and render pretty quickly. This is the result of combing two "inc" one for each run of the processing sketch, into 1 pov file. Run was controlled by an ini file.  I know Karsten has experimented with Luxrender, but I like PovRAY the 3.7 beta version is multithreaded, and there is lot of stuff that is out there (textures/tutorials etc).
Read about the latest povmesh library here.

Applying the FTest to my TriangleMesh to PovRAY export

If you've seen anything to to with my previous raytracing experiments, you will have come accross my FTest, here I have translated it to work with toxilibs mesh classes (using my totally custom version of toxiclibscore.jar which allows export as PovRAY mesh). As you will see from my "F" coding I'm not really that familiar with toxicilibs, Karsten would probably have a laugh:-


   1 import java.io.FileNotFoundException;
   2 import java.io.FileOutputStream;
   3 import java.io.PrintWriter;
   4 import java.util.logging.Level;
   5 import java.util.logging.Logger;
   6 import toxi.geom.AABB;
   7 import toxi.geom.Vec3D;
   8 import toxi.geom.mesh.TriangleMesh;
   9 import toxi.processing.*;
  10 
  11 
  12 /**
  13  * <p>FtestPOV demonstrates how to save a model as PovRAY mesh2
  14  * format to a generic Java PrintWriter (nested BufferedWriter/FileWriter would be better)
  15  * backend. inside the sketch folder.</p>
  16  */
  17 
  18 /*
  19 * Copyright (c) 2012 Martin Prout
  20  *
  21  * This library is free software; you can redistribute it and/or modify it
  22  * under the terms of the GNU Lesser General Public License as published by
  23  * the Free Software Foundation; either version 2.1 of the License, or (at
  24  * your option) any later version.
  25  *
  26  * http://creativecommons.org/licenses/LGPL/2.1/
  27  *
  28  * This library is distributed in the hope that it will be useful, but
  29  * WITHOUT ANY WARRANTY; without even the implied warranty of
  30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  31  * General Public License for more details.
  32  *
  33  * You should have received a copy of the GNU Lesser General Public License
  34  * along with this library; if not, write to the Free Software Foundation,
  35  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  36  */
  37 TriangleMesh mesh;
  38 ToxiclibsSupport gfx;
  39 
  40 void setup() {
  41   size(200, 200, P3D);
  42   gfx = new ToxiclibsSupport(this);
  43   // define a rounded cube using the SuperEllipsoid surface function
  44   AABB vert = AABB.fromMinMax(new Vec3D(-1.0f, -4.5f, -1.0f), new Vec3D(1.0f, 3.5f, 1.0f));
  45   AABB box = AABB.fromMinMax(new Vec3D(1.0f, 1.5f, -1.0f), new Vec3D(3.0f, 3.5f, 1.0f));
  46   AABB box2 = AABB.fromMinMax(new Vec3D(1.0f, -2.5f, -1.0f), new Vec3D(3.0f, -0.5f, 1.0f));
  47   mesh = (TriangleMesh) box.toMesh();
  48   mesh.addMesh((TriangleMesh) vert.toMesh());
  49   mesh.addMesh((TriangleMesh) box2.toMesh());
  50 }
  51 
  52 void draw() {
  53   translate(width/2, height/2);
  54   scale(15);
  55   rotateY(radians(-10));
  56   gfx.mesh(mesh);
  57 }
  58 
  59 void keyPressed() {
  60   if (key == 'e') {
  61     // attempt to create a PrintWriter and save to it 
  62     PrintWriter pw = null;
  63     try {
  64       String fileID = "FTest";//+(System.currentTimeMillis()/1000);
  65       pw = new PrintWriter(sketchPath(fileID + ".inc"));
  66       mesh.saveAsPOV(pw);
  67     } 
  68     catch (FileNotFoundException ex) {
  69       Logger.getLogger(ftest.class.getName()).log(Level.SEVERE, null, ex);
  70     } 
  71     finally {
  72       pw.flush();
  73       pw.close();
  74     }
  75     exit();
  76   }
  77     if (key == 's') {
  78     saveFrame("/home/tux/Fpde.png");
  79   }
  80 }
  81 




This was always going to produce an upside 'F' an indeed it does here is the screen image:-
Saved sketch



PovRAY traced (no normals)








However as expected the raytraced image is the right way up, so now I will try and make the raytraced image upside down to match the sketch. (exported normals play badly with this sketch, so I have removed them here). This has now been done (by mutliplying y and z coordinates by -1 during PovRAY export) here is the resulting PovRAY traced image:-

Raytraced image with corrected library

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

Exporting toxi TriangleMesh to PovRAY Mesh2 as an inc file

This is a completly different approach to my previous exporting PovRAY from processing (but is very similar to the toxiclibs STL/OBJ export). The beauty of this approach it is not reliant on the processing renderer (my povwriter library relies on PGraphics3D, which seems to have gone away in processing-2.0). Further the generated mesh2 should be handled more efficiently by PovRAY than a union of triangles.
Here is a brief outline of the story so far:-

1. I did a "hg clone https://bitbucket.org/postspectacular/toxiclibs"
2. Set up a NetBeans Project with the source (including processing-1.5.1 core.jar as a library)
3. I copied OBJWriter (with refactoring) to POVWriter (see gist)
4. I modified the TriangleMesh class to include copies of OBJWriter methods as POVWriter methods (see gist)
5. I wrote tests based on the SuperEllipsoid example (see gist)
6. I wrote pov file to #include "SuperEllisoid.inc"

There was a mystery to solve the face indices were out of range, it was difficult to pinpoint too many faces/vertices.

Update 21/Jan/2012 from PovRAY tutorial
 http://www.povray.org/documentation/view/3.6.1/293/
 The indexes are ZERO-BASED! So the first item in each list has an index of zero.
Presumably list in wavefront obj are not see 7 and toxis code, seems strange though?


Update 22/Jan/2012 from obj specification
The indices start at ONE (thought comes to mind I could now probably hack the joons sunflow-renderer, to export everything bar spheres (and possibly cube) as a PovRAY mesh2 object).

7. I wrote a really simple test based the mesh of a single cube (and created a pov file to include "cube.pov") (see gist)

There is some kludge (which is presumably be required for STL or OBJ export) where faces indices are incremented by one. I removed this adjustment and the problem seemed to be solved (see update above).

8. I ran the SuperEllisoid example again, and here is the resulting PovRAY rendered sketch.

Update 28 Feb (I deleted old code) see revised code here which has been re factored to use a File output, rather than PrintWriter directly.
Rendered with PovRAY 3.7beta


Looks pretty good to me, unlike the cube where the normals seem to be odd, more work to be done there (normalizing the normals did not seem to help). From further reading calculating normals is not trivial.

















Carried away with the success I thought I would have a go with the implicit function test, here is the result, intestingly I had to do this adjustment to fit the object in the scene scale<0.009, 0.009, 0.009>:-
Now rendered with Radiosity on took a bit longer, but it was worth it





Here is pretty funky red marble rendering

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