Augmented Reality, or: I Still Hate 3D (but Errors can be Cool)

I hate programming in 3D. I am trying not to hate it, but I can’t help it. Every time I build something in 3D it does not come out the way I expect it to. I think part of this is because I don’t completely understand the order in which Processing constructs the points for a 3D object, and I find it hard to imagine the correlation between actual and perceived distance along the z-axis.

Anyway, in Computational Cameras we had to do an augmented reality project. Because augmented reality is meant to add virtual content to our 3D world, 3D objects look better than 2D objects. So of course, I had to build something in 3D. I wanted to build a tube, because it was the easiest shape I could imagine the points for. When I actually built it, the order in which I typed in the points (informed by my thought process), created a more unusual shape, something akin to a pair of linked toroids. I finally did figure out the order of points to generate a tube, but the original looked better, so I kept it. Tubes are kind of boring.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import processing.opengl.*;
 
import processing.video.*;
 
import jp.nyatla.nyar4psg.*;
 
import java.io.*;
 
Capture cam;
MultiMarker nya;
float r = 10;
 
void setup()
{
  size(640, 480, OPENGL);
  cam = new Capture(this, 640, 480);
  cam.start();
  frameRate(15);
 
  nya = new MultiMarker(this, width, height, "camera_para.dat", NyAR4PsgConfig.CONFIG_DEFAULT);
 
  nya.addARMarker("patt.hiro", 80);
  nya.addARMarker("patt.kanji", 80);
}
 
void draw()
{
  background(0);
  cam.read();
  image(cam, 0, 0, width, height); // display the image at the width and height of the sketch window
  nya.detect(cam); // detect markers in the input image at the correct resolution (incorrect resolution will give assertion error)
  if (nya.isExistMarker(0))
  {
    setMatrix(nya.getMarkerMatrix(0));  //use this marker to translate and rotate the processing drawing
////    translate(0, 0, 70); //offset half the size of the cube.
    fill(0, 255, 0);
 
    beginShape(QUAD_STRIP);
    vertex(r*cos(0), r*sin(0), -20);
    vertex(r*cos(PI/4), r*sin(PI/4), -20);
    vertex(r*cos(PI/2), r*sin(PI/2), -20);
    vertex(r*cos(PI), r*sin(PI), -20);
    vertex(r*cos(5*PI/4), r*sin(5*PI/4), -20);
    vertex(r*cos(3*PI/2), r*sin(3*PI/2), -20);
    vertex(r*cos(7*PI/4), r*sin(7*PI/4), -20);
    vertex(r*cos(0), r*sin(0), 0);
    vertex(r*cos(PI/4), r*sin(PI/4), 0);
    vertex(r*cos(PI/2), r*sin(PI/2), 0);
    vertex(r*cos(PI), r*sin(PI), 0);
    vertex(r*cos(5*PI/4), r*sin(5*PI/4), 0);
    vertex(r*cos(3*PI/2), r*sin(3*PI/2), 0);
    vertex(r*cos(7*PI/4), r*sin(7*PI/4), 0);
    endShape(); 
 
  }
  perspective();
  if (nya.isExistMarker(1))
  {
    setMatrix(nya.getMarkerMatrix(1));
//    translate(0, 0, 70);
    fill(0, 255, 0);
    beginShape(QUAD_STRIP);
    vertex(r*cos(0), r*sin(0), -20);
    vertex(r*cos(PI/4), r*sin(PI/4), -20);
    vertex(r*cos(PI/2), r*sin(PI/2), -20);
    vertex(r*cos(PI), r*sin(PI), -20);
    vertex(r*cos(5*PI/4), r*sin(5*PI/4), -20);
    vertex(r*cos(3*PI/2), r*sin(3*PI/2), -20);
    vertex(r*cos(7*PI/4), r*sin(7*PI/4), -20);
    vertex(r*cos(0), r*sin(0), 0);
    vertex(r*cos(PI/4), r*sin(PI/4), 0);
    vertex(r*cos(PI/2), r*sin(PI/2), 0);
    vertex(r*cos(PI), r*sin(PI), 0);
    vertex(r*cos(5*PI/4), r*sin(5*PI/4), 0);
    vertex(r*cos(3*PI/2), r*sin(3*PI/2), 0);
    vertex(r*cos(7*PI/4), r*sin(7*PI/4), 0);
    endShape(); 
 
  }
}