Quantcast
Channel: a bit(e) of everything
Viewing all articles
Browse latest Browse all 10

Augmented reality

$
0
0

I have tried to play with NyARToolkit in a Processing sketch. Here the video:


It follows the code of the growing mountain:

import java.io.*; import processing.opengl.*; import jp.nyatla.nyar4psg.*; import codeanticode.gsvideo.*; String camPara = "/Users/federicozannier/Documents/ITPSpring2012/Computational Camera/7thWeek/NyARToolkit_dynamic/data/camera_para.dat"; String patternPath = "/Users/federicozannier/Documents/ITPSpring2012/Computational Camera/7thWeek/NyARToolkit_dynamic/data"; int arWidth = 640; int arHeight = 360; int numMarkers = 2; int resX = 60; int resY = 60; float[][] val = new float[resX][resY]; GSCapture cam; MultiMarker nya; float[] scaler = new float[numMarkers]; float[] noiseScale = new float[numMarkers]; float[] mountainHeight = new float[numMarkers]; float[] mountainGrowth = new float[numMarkers]; void setup() {   size(1280, 720, OPENGL);   cam = new GSCapture(this, 1280, 720);   cam.start();   noStroke();   nya = new MultiMarker(this, arWidth, arHeight, camPara, NyAR4PsgConfig.CONFIG_DEFAULT);   nya.setLostDelay(1);   String[] patterns = loadPatternFilenames(patternPath);   for (int i=0; i<numMarkers; i++) {     nya.addARMarker(patternPath + "/" + patterns[i], 80);     scaler[i] = random(0.8, 1.9);     noiseScale[i] = random(0.02, 0.075);     mountainHeight[i] = random(75, 150);   } } void draw() {   if (cam.available()) {     cam.read();     background(0);     image(cam, 0, 0, width, height);     PImage cSmall = cam.get();     cSmall.resize(arWidth, arHeight);     nya.detect(cSmall);     drawMountains();   } } void drawMountains() {   nya.setARPerspective();   lights();   for (int i=0; i<numMarkers; i++) {     if ((!nya.isExistMarker(i))) {       if (mountainGrowth[i] > 0) { mountainGrowth[i] -= 0.05; }       continue;     }     if (mountainGrowth[i] < 1) { mountainGrowth[i] += 0.03; }     float xoff = 0.0;     for (int x=0; x<resX; x++) {       xoff += noiseScale[i];       float yoff = 0;       for (int y=0; y<resY; y++) {         yoff += noiseScale[i];         val[x][y] = noise(i*10+xoff+frameCount*0.05, yoff) * mountainHeight[i];         float distance = dist(x, y, resX/2, resY/2);         distance = map(distance, 0, resX/2, 1, 0);         if (distance < 0) { distance = -distance; }         val[x][y] *= distance;       }     }     setMatrix(nya.getMarkerMatrix(i));     scale(1, -1);     scale(scaler[i]);     translate(-resX/2, -resY/2);     for (int x=0; x<resX-1; x++) {       for (int y=0; y<resY-1; y++) {         fill(x*20+y*20, 255-x*5, y*5);         beginShape();         vertex(x, y, val[x][y] * mountainGrowth[i]);         vertex((x+1), y, val[x+1][y] * mountainGrowth[i]);         vertex((x+1), (y+1), val[x+1][y+1] * mountainGrowth[i]);         vertex(x, (y+1), val[x][y+1] * mountainGrowth[i]);         endShape(CLOSE);       }     }   }   perspective(); } String[] loadPatternFilenames(String path) {   File folder = new File(path);   FilenameFilter pattFilter = new FilenameFilter() {     public boolean accept(File dir, String name) {       return name.toLowerCase().endsWith(".patt");     }   };   return folder.list(pattFilter); }

and the code for the rotating cube:

import java.io.*; import processing.opengl.*; import jp.nyatla.nyar4psg.*; import codeanticode.gsvideo.*; MultiMarker nya; GSCapture cam; ArrayList <ARObject> ars = new ArrayList <ARObject> (); String camPara = "/Users/federicozannier/Documents/ITPSpring2012/Computational Camera/7thWeek/NyARToolkit_OOB/data/camera_para.dat"; String patternPath = "/Users/federicozannier/Documents/ITPSpring2012/Computational Camera/7thWeek/NyARToolkit_OOB/data"; int arWidth = 640; int arHeight = 360; int numMarkers = 2; float mS = 0.2; void setup() {   size(1280, 720, OPENGL);   cam = new GSCapture(this, 1280, 720);   cam.start();   nya = new MultiMarker(this, arWidth, arHeight, camPara, NyAR4PsgConfig.CONFIG_DEFAULT);   nya.setLostDelay(1);   String[] patterns = loadPatternFilenames(patternPath);   for (int i=0; i<numMarkers; i++) {     nya.addARMarker(patternPath + "/" + patterns[i], 80);     ars.add(new ARObject(i));   }   colorMode(RGB, 1);   noStroke(); } void draw() {   if (cam.available()) {     cam.read();     background(0);     image(cam, 0, 0, width, height);     PImage cSmall = cam.get();     cSmall.resize(arWidth, arHeight);     nya.detect(cSmall);     nya.setARPerspective();     for (ARObject ar : ars) { ar.run(); }     perspective();   } } String[] loadPatternFilenames(String path) {   File folder = new File(path);   FilenameFilter pattFilter = new FilenameFilter() {     public boolean accept(File dir, String name) {       return name.toLowerCase().endsWith(".patt");     }   };   return folder.list(pattFilter); }

this is the class used for rendering the cube:

class ARObject {   int ID;   PVector rot, speed;     ARObject(int ID) {     this.ID = ID;     rot = new PVector(random(TWO_PI), random(TWO_PI), random(TWO_PI));     speed = new PVector(random(-mS, mS), random(-mS, mS), random(-mS, mS));   }   void run() {     rot.add(speed);     if (nya.isExistMarker(ID)) { display(); }   }   void display () {     setMatrix(nya.getMarkerMatrix(ID));     scale(1, -1);     translate(0, 0, 30);     rotateX(rot.x);     rotateY(rot.y);     rotateZ(rot.z);     scale(15);     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();   } }

Viewing all articles
Browse latest Browse all 10

Trending Articles