Loading...
Logo
Processing Forum
hope you enjoy it ;)
-poserpro


Copy code
  1. /**
  2. modified by poserpro, inspired  by Amnon Owed
  3. I made this to combine Augmented Reality with OOP
  4. print hiro marker image and use the webcam to shoot
  5. then you will see the colorful rotating cube atop the hiro marker
  6. the script is not perfect, but it fits my purpose for the testing though.
  7. feel free to use and spread, just don'the abuse it ;P
  8. **marker image can be downloaded from this link
  9. http://dl.dropbox.com/u/25433933/test.jpg
  10. **/
  11. /**
  12.   NyARToolkit for proce55ing/1.0.0
  13.   (c)2008-2011 nyatla
  14.   airmail(at)ebony.plala.or.jp
  15.  
  16.   最も短いARToolKitのコードです。
  17.   Hiroマーカを用意してください。
  18.  
  19.   This sample program is most small sample as simpleLite.
  20.   The marker is "patt.hiro".
  21. */
  22. import processing.video.*;
  23. import jp.nyatla.nyar4psg.*;
  24. ArrayList <ARObject> ars = new ArrayList <ARObject> ();
  25. float mS = 0.2;
  26.     float angle = 0.01;
  27.     char axis ='y';
  28.    
  29. Capture cam;
  30. MultiMarker nya;
  31. void setup() {
  32.   size(640,480,P3D);
  33.    frameRate(20);
  34.     colorMode(RGB, 1);
  35.   noStroke();
  36.   cam=new Capture(this,640,480);
  37.   nya=new MultiMarker(this,width,height,"camera_para.dat",NyAR4PsgConfig.CONFIG_PSG);
  38.   nya.addARMarker("patt.hiro",80);
  39.   ars.add(new ARObject(0));
  40. }
  41. void draw()
  42. {
  43.   if (cam.available() !=true) {
  44.       return;
  45.   }
  46.   cam.read();
  47.   nya.detect(cam);
  48.   background(0);
  49.   nya.drawBackground(cam);
  50.   if((!nya.isExistMarker(0))){
  51.     return;
  52.   }
  53.   nya.beginTransform(0);
  54.   translate(0,0,0);
  55.   rotateX(30);
  56.   rotateY(90);
  57.   for (ARObject ar : ars) { ar.run(); }
  58.   perspective();
  59.   nya.endTransform();
  60. }
  61. void mouseDragged()
  62. {
  63. }
  64. class ARObject {
  65.   int ID; // keep track of the current the ID of the object (corresponds with the ID i of the marker)
  66.   PVector rot, speed; // in this example the cube has a certain rotation and rotates at a certain speed
  67.  
  68.   ARObject(int ID) {
  69.     this.ID = ID; // set the ID
  70.     rot = new PVector(random(TWO_PI), random(TWO_PI), random(TWO_PI)); // random x, y, z rotation
  71.     speed = new PVector(random(-mS, mS), random(-mS, mS), random(-mS, mS)); // random x, y, z speed (within maxSpeed boundaries)
  72.   }
  73.   void run() {
  74.     // always keep rotating (even when the marker is NOT detected)
  75.     rot.add(speed);
  76.     if (nya.isExistMarker(ID)) { display(); }
  77.   }
  78.    void display () {
  79.     // get the Matrix for this marker and use it (through setMatrix)
  80.     setMatrix(nya.getMarkerMatrix(ID));
  81.     scale(1, -1); // turn things upside down to work intuitively for Processing users
  82.     // hover the cube a little above the real-world marker image
  83.     translate(0, 0, 30);
  84.     // rotate the cube in 3 dimensions
  85.     rotateX(rot.x);
  86.     rotateY(rot.y);
  87.     rotateZ(rot.z);
  88.     // scale - as with the the color range - to save typing with the coordinates (and make it much easier to change the size)
  89.     scale(15);
  90.     // a cube made out of 6 quads
  91.     // the 1 range can be used for both the color and the coordinates as a result of color range and scale (see earlier)
  92.     beginShape(QUADS);
  93.     fill(0, 1, 1); vertex(-1,  1,  1);
  94.     fill(1, 1, 1); vertex( 1,  1,  1);
  95.     fill(1, 0, 1); vertex( 1, -1,  1);
  96.     fill(0, 0, 1); vertex(-1, -1,  1);
  97.  
  98.     fill(1, 1, 1); vertex( 1,  1,  1);
  99.     fill(1, 1, 0); vertex( 1,  1, -1);
  100.     fill(1, 0, 0); vertex( 1, -1, -1);
  101.     fill(1, 0, 1); vertex( 1, -1,  1);
  102.     fill(1, 1, 0); vertex( 1,  1, -1);
  103.     fill(0, 1, 0); vertex(-1,  1, -1);
  104.     fill(0, 0, 0); vertex(-1, -1, -1);
  105.     fill(1, 0, 0); vertex( 1, -1, -1);
  106.     fill(0, 1, 0); vertex(-1,  1, -1);
  107.     fill(0, 1, 1); vertex(-1,  1,  1);
  108.     fill(0, 0, 1); vertex(-1, -1,  1);
  109.     fill(0, 0, 0); vertex(-1, -1, -1);
  110.     fill(0, 1, 0); vertex(-1,  1, -1);
  111.     fill(1, 1, 0); vertex( 1,  1, -1);
  112.     fill(1, 1, 1); vertex( 1,  1,  1);
  113.     fill(0, 1, 1); vertex(-1,  1,  1);
  114.     fill(0, 0, 0); vertex(-1, -1, -1);
  115.     fill(1, 0, 0); vertex( 1, -1, -1);
  116.     fill(1, 0, 1); vertex( 1, -1,  1);
  117.     fill(0, 0, 1); vertex(-1, -1,  1);
  118.     endShape();
  119.   }
  120. }