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, 13 September 2012

Toxiclibs mesh, export to povray with processing-2.0

Here is an sketch that demonstrates the use of my povmesh library to first display toxiclibs mesh as a vbo. Manipulate the sketch using an arcball (and mousewheel "zoom"), and to export it to povray as mesh2 object.
   1 
   2 /**
   3  * This example implements a custom VolumetricSpace using an implicit function
   4  * to calculate each voxel. This is slower than the default array or HashMap
   5  * based implementations, but also has much less memory requirements and so might
   6  * be an interesting and more viable approach for very highres voxel spaces
   7  * (e.g. >32 million voxels). This implementation here also demonstrates how to
   8  * achieve an upper boundary on the iso value (in addition to the one given and
   9  * acting as lower threshold when computing the iso surface)
  10  *
  11  * Usage:
  12  * use mouse to rotate object (arcball)
  13  * mouse wheel to "zoom" in/out
  14  * l: apply laplacian mesh smooth
  15  * s: to export as PovRAY mesh2 object
  16  */
  17 
  18 /* 
  19  * Copyright (c) 2010 Karsten Schmidt (modified by Martin Prout 2012)
  20  * 
  21  * This library is free software; you can redistribute it and/or
  22  * modify it under the terms of the GNU Lesser General Public
  23  * License as published by the Free Software Foundation; either
  24  * version 2.1 of the License, or (at 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,
  29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  31  * Lesser General Public License for more details.
  32  * 
  33  * You should have received a copy of the GNU Lesser General Public
  34  * License along with this library; if not, write to the Free Software
  35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  36  */
  37 
  38 import povmesh.vbo.*;
  39 import povmesh.mesh.*;
  40 import povmesh.util.*;
  41 import toxi.geom.*;
  42 import toxi.geom.mesh.*;
  43 import toxi.math.*;
  44 import toxi.volume.*;
  45 import toxi.processing.*;
  46 
  47 int RES = 64;
  48 float ISO = 0.2;
  49 float MAX_ISO = 0.66;
  50 
  51 WETriangleMesh mesh;
  52 MeshToVBO gfx;   // tool to convert mesh to an vbo object (PShape)
  53 PShape implicit; // is the "retained shape/vbo"
  54 ArcBall arcball; //provides arcball manipulation
  55 
  56 
  57 void setup() {
  58   size(800, 600, P3D);
  59   arcball = new ArcBall(this);  // arcball defaults to centre screen and radius half width/height
  60   gfx = new MeshToVBO(this); 
  61   VolumetricSpace vol = new EvaluatingVolume(new Vec3D(400, 400, 400), RES, MAX_ISO);
  62   IsoSurface surface = new HashIsoSurface(vol);
  63   mesh = new WETriangleMesh("iso0");
  64   surface.computeSurfaceMesh(mesh, ISO);
  65   noStroke();
  66   implicit = gfx.meshToVBO(mesh, true); // convert mesh to vbo, with normals
  67 }
  68 
  69 void draw() {
  70   background(0);
  71   setupLights();
  72   translate(width / 2, height / 2); // centre the object 
  73   arcball.update();
  74   shape(implicit);  // display vbo to scrren
  75 }
  76 
  77 /**
  78  * 'S' or 's' saves geometry to a PovRAY *.inc file, each mesh is exported as a mesh2 object
  79  *  with texture. The mesh2 objects are grouped as a declared object, with named as
  80  * as "mesh_objects". Use mouse wheel to "zoom"
  81  * If no Options used to create POVMesh, defaults to RAW ie no texture.
  82  * GLASS, METAL and PHONG use a sequence of rainbow colors with finish
  83  * RED and WHITE use single color for all mesh2 objects and a PHONG finish.
  84  * RANDOM, TWOTONE are also valid options, see reference
  85  */
  86 
  87 void keyPressed() {
  88   switch(key) {
  89   case 'l':
  90   case 'L':
  91     new LaplacianSmooth().filter(mesh, 1);
  92     implicit = gfx.meshToVBO(mesh, true); // update PShape with smoothed mesh
  93     break;
  94   case 's':
  95   case 'S':
  96     POVMesh pm = new POVMesh(this);
  97     pm.beginSave(new File(sketchPath("implicit.inc")));
  98     pm.setTexture(Textures.WHITE);
  99     pm.saveAsPOV(mesh, true); // export to povray with calculated normals
 100     pm.endSave();
 101     exit();
 102     break;
 103   }
 104 }
 105 
 106 void setupLights() {
 107   lights();
 108   ambientLight(80, 80, 80);
 109   directionalLight(100, 100, 100, -1, -1, 1);
 110   ambient(40);
 111   lightSpecular(30, 30, 30);
 112   specular(60);
 113   shininess(0.7);
 114 }
 115 
 1 class EvaluatingVolume extends VolumetricSpace {
 2 
 3   private final float FREQ = PI * 3.8;
 4   private float upperBound;
 5   private SinCosLUT lut;
 6   
 7   public EvaluatingVolume(Vec3D scale, int res, float upperBound) {
 8     this(scale,res,res,res,upperBound);
 9   }
10   
11   public EvaluatingVolume(Vec3D scale, int resX, int resY, int resZ, float upperBound) {
12     super(scale, resX, resY, resZ);
13     this.upperBound = upperBound;
14     this.lut=new SinCosLUT();
15   }
16 
17   public void clear() {
18     // nothing to do here
19   }
20   
21   public final float getVoxelAt(int i) {
22     return getVoxelAt(i % resX, (i % sliceRes) / resX, i / sliceRes);
23   }
24 
25   public final float getVoxelAt(int x, int y, int z) {
26     float val = 0;
27     if (x > 0 && x < resX1 && y > 0 && y < resY1 && z > 0 && z < resZ1) {
28       float xx = (float) x / resX - 0.5f;
29       float yy = (float) y / resY - 0.5f;
30       float zz = (float) z / resZ - 0.5f;
31       val = lut.cos(xx * FREQ) * lut.sin(yy* FREQ) + lut.cos(yy* FREQ) * lut.sin(zz* FREQ) + lut.cos(zz* FREQ)* lut.sin(xx* FREQ);
32       if (val > upperBound) {
33         val = 0;
34       }
35     }
36     return val;
37   }
38 }

Here is the povray.ini file:-

   1 ; implicit.ini high quality setting set quality = 5 say for preview
   2 
   3 Input_File_Name=implicit.pov
   4 Output_File_Name=implicit.png
   5 quality=11
   6 Antialias=On
   7 Sampling_Method=2
   8 ; Use sketch Width Height to retain aspect ratio
   9 Width=800        
  10 Height=600

Here is the scene description file:-

   1 // Persistence Of Vision Ray Tracer Scene Description File
   2 // File:  Simple Scene <template for povwriter>
   3 // Vers: 3.7
   4 // Date: January 2012
   5 // Auth: Martin Prout 
   6 
   7 #version 3.7;
   8 
   9 global_settings{
  10   assumed_gamma 1.0
  11   radiosity{
  12     pretrace_start 0.04
  13     pretrace_end 0.01
  14     count 200
  15     recursion_limit 3
  16     nearest_count 10
  17     error_bound 0.5
  18   }
  19 }
  20 
  21 #include "colors.inc"
  22 #include "skies.inc"
  23 #include "my_texture.inc"
  24 #include "implicit.inc"
  25 
  26 
  27 
  28 //----------------declare scene Settings
  29 #declare camera0 = camera {            // define additional cameras to suit viewing preferences
  30    location <-1.5, 30.0, -150.0>
  31    direction <0.0, 0.0, 2.0>
  32    up  <0.0, 1.0, 0.0>
  33    right <1.25, 0.0, 0.0>
  34    look_at <0.0, 25.0, 35.0>
  35 }
  36 
  37 #declare light0 = light_source { <100.0, 100.0, -200.0> colour White }
  38 
  39 #declare ground0 = plane { <0.0, 1.0, 0.0>, 0.0  // a reflective ground plane
  40    pigment { NeonBlue }
  41    finish {reflection 0.15}
  42 }
  43 
  44 //------------------end of declare scene settings
  45 
  46 // -----------------set the scene
  47 
  48 camera { camera0 }              // ------------------------------------------
  49                                 //  The use of declared values makes it possible to easily 
  50 light_source{ light0 }          //  change the way the scene is rendered. Just define an
  51                                 //  additional camera say for your template, and do the
  52 sky_sphere{ S_Cloud3 }          //  substitution here. Retain the original definition and
  53                                 //  you can easily backtrack. Definitions can also be saved
  54 plane{ ground0 }                //  as included file see colors.inc for an example.
  55                                 // ---------------------------------------------  
  56 // -----------------end set the scene
  57 object{
  58 object{ mesh_objects }                  // mesh_objects created by povmesh library
  59 scale<0.2, 0.2, 0.2>
  60 translate<0, 33 10>
  61 }
povray ray traced implicit function

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