Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 6aa6d1f

Browse files
committed
tidying up code in AR example
1 parent c597eea commit 6aa6d1f

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

ar/ex1_ar_intro/ex1_ar_intro.pde

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
import processing.ar.*;
22

33
ARTracker tracker;
4-
ARAnchor touchAnchor;
54
ArrayList<ARAnchor> trackAnchors = new ArrayList<ARAnchor>();
5+
ARAnchor touchAnchor;
66
PShape obj;
77

88
void setup() {
99
fullScreen(AR);
1010
obj = loadShape("model.obj");
11-
1211
tracker = new ARTracker(this);
1312
tracker.start();
14-
noStroke();
13+
noStroke();
1514
}
1615

1716
void draw() {
1817
lights();
19-
2018
drawObject(touchAnchor);
21-
for (ARAnchor anchor : trackAnchors) {
22-
if (anchor.isTracking()) drawSphere(anchor);
23-
if (anchor.isStopped()) anchor.dispose();
24-
}
25-
tracker.clearAnchors(trackAnchors);
26-
19+
drawAnchors();
2720
drawTrackables();
2821
}
2922

@@ -40,42 +33,51 @@ void trackableEvent(ARTrackable t) {
4033
}
4134
}
4235

43-
void drawObject(ARAnchor anchor) {
44-
if (anchor != null) {
45-
anchor.attach();
46-
shape(obj);
47-
anchor.detach();
36+
void drawAnchors() {
37+
for (ARAnchor anchor : trackAnchors) {
38+
if (anchor.isTracking()) drawSphere(anchor, 0.05);
39+
if (anchor.isStopped()) anchor.dispose();
4840
}
49-
}
50-
51-
void drawSphere(ARAnchor anchor) {
52-
anchor.attach();
53-
fill(#CF79F5);
54-
sphere(0.05);
55-
anchor.detach();
41+
tracker.clearAnchors(trackAnchors);
5642
}
5743

5844
void drawTrackables() {
5945
for (int i = 0; i < tracker.count(); i++) {
60-
ARTrackable trackable = tracker.get(i);
61-
if (!trackable.isTracking()) continue;
62-
46+
ARTrackable t = tracker.get(i);
6347
pushMatrix();
64-
trackable.transform();
65-
if (mousePressed && trackable.isSelected(mouseX, mouseY)) {
48+
t.transform();
49+
float lx = t.lengthX();
50+
float lz = t.lengthZ();
51+
if (mousePressed && t.isSelected(mouseX, mouseY)) {
6652
fill(255, 0, 0, 100);
6753
} else {
6854
fill(255, 100);
6955
}
70-
71-
beginShape(QUADS);
72-
float lx = trackable.lengthX();
73-
float lz = trackable.lengthZ();
74-
vertex(-lx/2, 0, -lz/2);
75-
vertex(-lx/2, 0, +lz/2);
76-
vertex(+lx/2, 0, +lz/2);
77-
vertex(+lx/2, 0, -lz/2);
78-
endShape();
79-
popMatrix();
56+
drawPlane(lx, lz);
57+
popMatrix();
8058
}
8159
}
60+
61+
void drawSphere(ARAnchor anchor, float r) {
62+
anchor.attach();
63+
fill(#CF79F5);
64+
sphere(r);
65+
anchor.detach();
66+
}
67+
68+
void drawPlane(float lx, float lz) {
69+
beginShape(QUADS);
70+
vertex(-lx/2, 0, -lz/2);
71+
vertex(-lx/2, 0, +lz/2);
72+
vertex(+lx/2, 0, +lz/2);
73+
vertex(+lx/2, 0, -lz/2);
74+
endShape();
75+
}
76+
77+
void drawObject(ARAnchor anchor) {
78+
if (anchor != null) {
79+
anchor.attach();
80+
shape(obj);
81+
anchor.detach();
82+
}
83+
}

0 commit comments

Comments
 (0)