Final processing code

From DigitalCraft_Wiki
Jump to navigation Jump to search

import SimpleOpenNI.*;

ArrayList<PVector[]> worldmaps = new ArrayList<PVector[]>();

final static int BUFFER = 10; //fps * sec


SimpleOpenNI context; float zoomF =0.3f; float rotX = radians(180); // by default rotate the hole scene 180deg around the x-axis,

                                  // the data from openni comes upside down

float rotY = radians(0);


void setup() {

 frameRate(10);
 size(displayWidth, displayHeight, P3D);


 context = new SimpleOpenNI(this);
 if (context.isInit() == false)
 {
   println("Can't init SimpleOpenNI, maybe the camera is not connected!"); 
   exit();
   return;
 }
 // disable mirror
 context.setMirror(false);
 // enable depthMap generation 
 context.enableDepth();
 stroke(255, 255, 255);
 smooth();
 perspective(radians(45), 
             float(width)/float(height), 
             10, 150000);

}

void draw() {

 // update the cam
 context.update();
 background(0, 0, 0);
 translate(width/2, height/2, 0);
 rotateX(rotX);
 rotateY(rotY);
 scale(zoomF);
 int[]   depthMap = context.depthMap();
 int     steps   = 3;  // to speed up the drawing, draw every third point
 int     index;
 PVector realWorldPoint;
 translate(0, 0, -1000);  // set the rotation center of the scene 1000 infront of the camera
 color from = color(0, 50, 200);
 color to = color(255, 0, 0);
 // strokeWeight(2);


PVector[] worldMap = new PVector[context.depthMapRealWorld().length];
 myCopy(context.depthMapRealWorld(), worldMap); 
 worldmaps.add(worldMap);
 
 
 int whichWorld = 0; 
 //int whichWorld = worldmaps.size() - BUFFER;
 println("Buffering World: " + worldmaps.size() + " of " + BUFFER);
 if (worldmaps.size() >= BUFFER) { 
   whichWorld = 0;
   //whichWorld = worldmaps.size() - 1;
   //z nnhnhnnnnawhichWorld = int(random(0, BUFFER));
   PVector[] realWorldMap = worldmaps.get(whichWorld);
   // draw pointcloud
   noFill();
   beginShape(TRIANGLES);
   for (int y=0; y < context.depthHeight (); y+=steps)
   {
     for (int x=0; x < context.depthWidth (); x+=steps)
     {
       index = x + y * context.depthWidth();
       { 
         // draw the projected point
         //        realWorldPoint = context.depthMapRealWorld()[index];
         realWorldPoint = realWorldMap[index];
         // make realworld z negative, in the 3d drawing coordsystem +z points in the direction of the eye
         //calculate the mix between 0 and 1
         float mix = map(realWorldPoint.z, 1000, 2000, 0, 1);
         color interA = lerpColor(from, to, mix);
         stroke(interA);
         if ((realWorldPoint.z>100)&&(realWorldPoint.z<2000)) vertex(realWorldPoint.x, realWorldPoint.y, realWorldPoint.z);
       }
       //println("x: " + x + " y: " + y);
     }
   } 
   endShape();
   
   worldmaps.remove(0);
 }
 // draw the kinect cam
 //context.drawCamFrustum();

}


void myCopy( PVector[] vec1 , PVector[] vec2) {

 for(int i = 0; i < vec1.length; i++) {
   vec2[i] = vec1[i].get();
 }

}

void keyPressed() {

 switch(key)
 {
 case ' ':
   context.setMirror(!context.mirror());
   break;
 }
 switch(keyCode)
 {
 case LEFT:
   rotY += 0.1f;
   break;
 case RIGHT:
   // zoom out
   rotY -= 0.1f;
   break;
 case UP:
   if (keyEvent.isShiftDown())
     zoomF += 0.02f;
   else
     rotX += 0.1f;
   break;
 case DOWN:
   if (keyEvent.isShiftDown())
   {
     zoomF -= 0.02f;
     if (zoomF < 0.01)
       zoomF = 0.01;
   } else
     rotX -= 0.1f;
   break;
 }

}