Augmented Reality, inspired by Amnon Owed
in
Share your Work
•
1 year ago
hope you enjoy it ;)
-poserpro
-poserpro
- /**
- modified by poserpro, inspired by Amnon Owed
- I made this to combine Augmented Reality with OOP
- print hiro marker image and use the webcam to shoot
- then you will see the colorful rotating cube atop the hiro marker
- the script is not perfect, but it fits my purpose for the testing though.
- feel free to use and spread, just don'the abuse it ;P
- **marker image can be downloaded from this link
- http://dl.dropbox.com/u/25433933/test.jpg
- **/
- /**
- NyARToolkit for proce55ing/1.0.0
- (c)2008-2011 nyatla
- airmail(at)ebony.plala.or.jp
- 最も短いARToolKitのコードです。
- Hiroマーカを用意してください。
- This sample program is most small sample as simpleLite.
- The marker is "patt.hiro".
- */
- import processing.video.*;
- import jp.nyatla.nyar4psg.*;
- ArrayList <ARObject> ars = new ArrayList <ARObject> ();
- float mS = 0.2;
- float angle = 0.01;
- char axis ='y';
- Capture cam;
- MultiMarker nya;
- void setup() {
- size(640,480,P3D);
- frameRate(20);
- colorMode(RGB, 1);
- noStroke();
- cam=new Capture(this,640,480);
- nya=new MultiMarker(this,width,height,"camera_para.dat",NyAR4PsgConfig.CONFIG_PSG);
- nya.addARMarker("patt.hiro",80);
- ars.add(new ARObject(0));
- }
- void draw()
- {
- if (cam.available() !=true) {
- return;
- }
- cam.read();
- nya.detect(cam);
- background(0);
- nya.drawBackground(cam);
- if((!nya.isExistMarker(0))){
- return;
- }
- nya.beginTransform(0);
- translate(0,0,0);
- rotateX(30);
- rotateY(90);
- for (ARObject ar : ars) { ar.run(); }
- perspective();
- nya.endTransform();
- }
- void mouseDragged()
- {
- }
- class ARObject {
- int ID; // keep track of the current the ID of the object (corresponds with the ID i of the marker)
- PVector rot, speed; // in this example the cube has a certain rotation and rotates at a certain speed
- ARObject(int ID) {
- this.ID = ID; // set the ID
- rot = new PVector(random(TWO_PI), random(TWO_PI), random(TWO_PI)); // random x, y, z rotation
- speed = new PVector(random(-mS, mS), random(-mS, mS), random(-mS, mS)); // random x, y, z speed (within maxSpeed boundaries)
- }
- void run() {
- // always keep rotating (even when the marker is NOT detected)
- rot.add(speed);
- if (nya.isExistMarker(ID)) { display(); }
- }
- void display () {
- // get the Matrix for this marker and use it (through setMatrix)
- setMatrix(nya.getMarkerMatrix(ID));
- scale(1, -1); // turn things upside down to work intuitively for Processing users
- // hover the cube a little above the real-world marker image
- translate(0, 0, 30);
- // rotate the cube in 3 dimensions
- rotateX(rot.x);
- rotateY(rot.y);
- rotateZ(rot.z);
- // scale - as with the the color range - to save typing with the coordinates (and make it much easier to change the size)
- scale(15);
- // a cube made out of 6 quads
- // the 1 range can be used for both the color and the coordinates as a result of color range and scale (see earlier)
- beginShape(QUADS);
- fill(0, 1, 1); vertex(-1, 1, 1);
- fill(1, 1, 1); vertex( 1, 1, 1);
- fill(1, 0, 1); vertex( 1, -1, 1);
- fill(0, 0, 1); vertex(-1, -1, 1);
- fill(1, 1, 1); vertex( 1, 1, 1);
- fill(1, 1, 0); vertex( 1, 1, -1);
- fill(1, 0, 0); vertex( 1, -1, -1);
- fill(1, 0, 1); vertex( 1, -1, 1);
- fill(1, 1, 0); vertex( 1, 1, -1);
- fill(0, 1, 0); vertex(-1, 1, -1);
- fill(0, 0, 0); vertex(-1, -1, -1);
- fill(1, 0, 0); vertex( 1, -1, -1);
- fill(0, 1, 0); vertex(-1, 1, -1);
- fill(0, 1, 1); vertex(-1, 1, 1);
- fill(0, 0, 1); vertex(-1, -1, 1);
- fill(0, 0, 0); vertex(-1, -1, -1);
- fill(0, 1, 0); vertex(-1, 1, -1);
- fill(1, 1, 0); vertex( 1, 1, -1);
- fill(1, 1, 1); vertex( 1, 1, 1);
- fill(0, 1, 1); vertex(-1, 1, 1);
- fill(0, 0, 0); vertex(-1, -1, -1);
- fill(1, 0, 0); vertex( 1, -1, -1);
- fill(1, 0, 1); vertex( 1, -1, 1);
- fill(0, 0, 1); vertex(-1, -1, 1);
- endShape();
- }
- }
1