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

Tuesday, 23 October 2012

Another Matrix Test for Joons-Fork

Here is an idea I had for testing both rotation and translation with pushMatrix and popMatrix with my joons fork (still only single depth, but readily adjustable). First I created my own Vec3D and MyBox classes in the processing ide.

Vec3D:-

   1 class Vec3D {
   2   float x, y, z;
   3 
   4   Vec3D() {
   5     this.x = 0;
   6     this.y = 0;
   7     this.z = 0;
   8   }
   9 
  10   Vec3D(float x, float y, float z) {
  11     this.x = x;
  12     this.y = y;
  13     this.z = z;
  14   }
  15 
  16   Vec3D(Vec3D v) {
  17     this.x = v.x;
  18     this.y = v.y;
  19     this.z = v.z;
  20   }
  21 
  22   Vec3D add(Vec3D v) {
  23     Vec3D vcopy = new Vec3D(this);
  24     vcopy.x += v.x;
  25     vcopy.y += v.y;
  26     vcopy.z += v.z;
  27     return vcopy;
  28   }
  29 }
  30 

MyBox:-

   1 class MyBox {
   2   Vec3D sz;
   3   Vec3D rot;
   4   Vec3D trans;
   5 
   6   MyBox(Vec3D sz, Vec3D trans, Vec3D rot) {
   7     this.sz = sz;
   8     this.trans = trans;
   9     this.rot = rot;
  10     pushMatrix();
  11     fill(200, 0, 0);
  12     stroke(0);
  13     translate(trans.x, trans.y, trans.z);
  14     rotateX(rot.x);
  15     rotateY(rot.y);
  16     rotateZ(rot.z);
  17     box(sz.x, sz.y, sz.z);
  18     popMatrix();
  19   }
  20 }

The Sketch:-

   1 import joons.JoonsRenderer;
   2 
   3 JoonsRenderer jr;
   4 
   5 float eyeX = 0;
   6 float eyeY = 115;
   7 float eyeZ = 40;
   8 float centerX = 0;
   9 float centerY = 0;
  10 float centerZ = 40;
  11 float upX = 0;
  12 float upY = 0;
  13 float upZ = -1;
  14 float fov = PI / 4; 
  15 float aspect = 1.0;  
  16 float zNear = 5;
  17 float zFar = 10000;
  18 MyBox[] boxes;
  19 Vec3D incTrans = new Vec3D(-10, 0, 10);
  20 Vec3D incRot = new Vec3D(radians(5), 0, radians(10));
  21 Vec3D sz = new Vec3D(20, 20, 20);
  22 
  23 void setup() {
  24   size(600, 600, P3D);
  25   jr = new JoonsRenderer(this, width, height);
  26   jr.setRenderSpeed(1);  
  27   //renderSpeed is set 1 by default, if this method is not called.
  28   //speed is inversely proportional to the render quality
  29   boxes = new MyBox[4];
  30   /*SC file format:-
  31    Image Settings
  32    Lights
  33    Shaders
  34    Modifiers
  35    Objects
  36    Instances
  37    */
  38 }
  39 
  40 
  41 void draw() {
  42   if (jr.displaySketch()){
  43   beginRecord("joons.OBJWriter", "");// just call like this. Leave the second parameter as "".
  44   perspective(fov, aspect, zNear, zFar);// use perspective() before camera()!!
  45   camera(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
  46   render();
  47   endRecord();
  48   }
  49 }
  50 
  51 void render() {  // encapsulate the processing sketch as a function    
  52   Vec3D pos = new Vec3D(20, 0, 30);
  53   Vec3D rot = new Vec3D();
  54   for (MyBox myBox: boxes){
  55     myBox = new MyBox(sz, pos, rot);
  56     pos = pos.add(incTrans);
  57     rot = rot.add(incRot);
  58   }
  59 }
  60 
  61 void keyPressed() {
  62   if (key == 'r' || key == 'R' && jr.displaySketch()) {
  63     saveFrame("capture.png");
  64     jr.setShader("object0", "Red");
  65     jr.setSC(dataPath("cornell_box.sc"));
  66     jr.render("bucket");
  67     // render using render("ipr") to render quick and rough,
  68     // and render("bucket") to render slow and smooth
  69     // if successfully rendered, returns true
  70   }
  71 }
sunflow rendered sketch
processing 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