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, 8 December 2010

A Povray rendered 3D Sierpinski gasket in processing using supercad library

To try and confirm my earlier suspicions about conflicting "y" axis direction of processing/the supercad library and povray I thought I would checkout my sierpinski (tetrahedron). Export to Wavefront Object went nicely, I even managed to get a snapshot from meshlab, had to fiddle with blender to get the object in view (learning progress is slow).
To get povray to render I had to mess with my processing code and edit the povray camera in the pov file. The rendered images below do suggest some issues with 'y' coordinate system.

   1 // tetrahedron.pde by Martin Prout
   2 import superCAD.*; 
   3 
   4 PVector[] pts = {  // points of the unit tetrahedron
   5   new PVector(-0.5, -0.5, -0.5), 
   6   new PVector(0.5, 0.5, -0.5), 
   7   new PVector(-0.5, 0.5, 0.5), 
   8   new PVector(0.5, -0.5, 0.5)
   9   };
  10 
  11   int TETRA_SIZE = 600;
  12 float ROTATE_STEP = 0.001;
  13 String cadSoftware,  ext;
  14 boolean record = false;
  15 
  16 
  17 void setup() {
  18   size(800,  800,  P3D);
  19 }
  20 
  21 
  22 void draw() {
  23   background(0);
  24   configureLights();
  25   translate(width/2,  height/2,  -100);
  26   noStroke();
  27   
  28   if(record) {
  29     beginRaw("superCAD."+cadSoftware,  "output."+ext);
  30   } 
  31   rotateX(PI);
  32  // rotateZ(frameCount * ROTATE_STEP);
  33  // rotateY(frameCount * ROTATE_STEP);
  34  // rotateX(frameCount * ROTATE_STEP);
  35   drawSierpinski(pts);
  36 
  37   if (record) {
  38     endRaw();
  39     record = false;
  40   }
  41 }
  42 
  43 void configureLights() {
  44   ambientLight(100,  100,  100);
  45   directionalLight(155,  0,  0,  -1,  -1,  -1); // colored directional
  46   directionalLight(0,  0,  155,  1,  -1,  -1);  // lights essential
  47   directionalLight(155,  155,  0,  1,  1,  1);  // for 3D illusion
  48 }
  49 
  50 PVector midPoint(PVector a,  PVector b) {
  51   PVector result = PVector.add(a,  b);
  52   result.div(2);
  53   return result;
  54 }
  55 
  56 void drawSierpinski(PVector[] pts) {
  57   if (pts[0].dist(pts[1]) < 0.1) {                 // limits recursion on relative size
  58     drawTetrahedron(pts,  TETRA_SIZE); // render the tetrahedra
  59   }
  60   else {
  61     PVector av = midPoint(pts[0],  pts[1]); // a tetrahedron midpoint vertices
  62     PVector bv = midPoint(pts[0],  pts[2]); // b
  63     PVector cv = midPoint(pts[0],  pts[3]); // b
  64     PVector dv = midPoint(pts[1],  pts[2]); // d
  65     PVector ev = midPoint(pts[1],  pts[3]); // e
  66     PVector fv = midPoint(pts[3],  pts[2]);  // e
  67     PVector[] aa = {
  68       pts[0], 
  69       av, 
  70       bv, 
  71       cv
  72     };
  73     PVector[] bb = {
  74       av, 
  75       ev, 
  76       dv, 
  77       pts[1]
  78     };
  79     PVector[] cc = {
  80       cv, 
  81       ev, 
  82       fv, 
  83       pts[3]
  84     };
  85     PVector[] dd = {
  86       bv, 
  87       dv, 
  88       fv, 
  89       pts[2]
  90     };
  91     drawSierpinski(aa); // calculate further inner tetrahedra coordinates
  92     drawSierpinski(bb);
  93     drawSierpinski(cc);
  94     drawSierpinski(dd);
  95   }
  96 }
  97 
  98 void drawTetrahedron(PVector[] pts,  float sz) {
  99   fill(255);
 100   beginShape(TRIANGLES);
 101 
 102   vertex(pts[0].x*sz,  pts[0].y*sz,  pts[0].z*sz);  // 1
 103   vertex(pts[1].x*sz,  pts[1].y*sz,  pts[1].z*sz);  // 2  
 104   vertex(pts[2].x*sz,  pts[2].y*sz,  pts[2].z*sz);  // 3
 105 
 106   vertex(pts[2].x*sz,  pts[2].y*sz,  pts[2].z*sz);  // 3
 107   vertex(pts[0].x*sz,  pts[0].y*sz,  pts[0].z*sz);  // 1  
 108   vertex(pts[3].x*sz,  pts[3].y*sz,  pts[3].z*sz);  // 4  
 109 
 110   vertex(pts[3].x*sz,  pts[3].y*sz,  pts[3].z*sz);  // 4  
 111   vertex(pts[2].x*sz,  pts[2].y*sz,  pts[2].z*sz);  // 3
 112   vertex(pts[1].x*sz,  pts[1].y*sz,  pts[1].z*sz);  // 2
 113 
 114   vertex(pts[1].x*sz,  pts[1].y*sz,  pts[1].z*sz);  // 2  
 115   vertex(pts[3].x*sz,  pts[3].y*sz,  pts[3].z*sz);  // 4
 116   vertex(pts[0].x*sz,  pts[0].y*sz,  pts[0].z*sz);  // 1
 117 
 118   endShape();
 119 }
 120 
 121 void keyPressed() {
 122 
 123   switch(key) {
 124   case 'r': 
 125     cadSoftware = "Rhino"; 
 126     ext = "rvb"; 
 127     break;
 128   case 's': 
 129     cadSoftware = "SketchUP"; 
 130     ext = "rb";
 131     break;
 132   case 'a': 
 133     cadSoftware = "AutoLISP"; 
 134     ext = "lsp";
 135     break;
 136   case 'p': 
 137     cadSoftware = "PovRAY"; 
 138     ext = "pov";
 139     break;
 140   case 'm': 
 141     cadSoftware = "Maya"; 
 142     ext = "mel";
 143     break; 
 144   case 'o': 
 145     cadSoftware = "ObjFile"; 
 146     ext = "obj";
 147     break;       
 148   case 'c': 
 149     cadSoftware = "ArchiCAD"; 
 150     ext = "gdl";
 151     break;
 152   }
 153   record = true;
 154 }


This is the rotated version

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