-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtableOutput.pde
218 lines (183 loc) · 5.7 KB
/
tableOutput.pde
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// This is a script that allows one to open a new canvas for the purpose
// of simple 2D projection mapping, such as on a flat table surface
//
// Right now, only appears to work in windows...
//
// To use this example in the real world, you need a projector
// and a surface you want to project your Processing sketch onto.
//
// Simply press the 'c' key and drag the corners of the
// CornerPinSurface so that they
// match the physical surface's corners. The result will be an
// undistorted projection, regardless of projector position or
// orientation.
//
// You can also create more than one Surface object, and project
// onto multiple flat surfaces using a single projector.
//
// This extra flexbility can comes at the sacrifice of more or
// less pixel resolution, depending on your projector and how
// many surfaces you want to map.
//
int projectorWidth = 1920;
int projectorHeight = 1200;
int projectorOffset = 1920;
int screenWidth = 1920;
int screenHeight = 1080;
// How big your table is, in pixels
int tableWidth = 450;
int tableHeight = 550;
PGraphics table;
import javax.swing.JFrame;
import deadpixel.keystone.*;
// Visualization may show 2D projection visualization, or not
boolean displayProjection2D = false;
//int projectorOffset = screenWidth;
boolean testProjectorOnMac = false;
// defines Keystone settings from xml file in parent folder
Keystone ks;
// defines various drawing surfaces, all pre-calibrated, to project
CornerPinSurface surface;
PGraphics offscreen;
PImage projector;
// New Application Window Parameters
PFrame proj2D = null; // f defines window to open new applet in
projApplet applet; // applet acts as new set of setup() and draw() functions that operate in parallel
// Run Anything Needed to have Projection mapping work
void initializeProjection2D() {
println("Projector Info: " + projectorWidth + ", " + projectorHeight + ", " + projectorOffset);
loadProjectorLocation();
}
public class PFrame extends JFrame {
public PFrame() {
setBounds(0, 0, projectorWidth, projectorHeight );
setLocation(projectorOffset, 0);
applet = new projApplet();
setResizable(false);
setUndecorated(true);
setAlwaysOnTop(true);
add(applet);
applet.init();
show();
setTitle("Projection2D");
}
}
public void showProjection2D() {
if (proj2D == null) {
proj2D = new PFrame();
}
proj2D.setVisible(true);
}
public void closeProjection2D() {
proj2D.setVisible(false);
}
public void resetProjection2D() {
initializeProjection2D();
if (proj2D != null) {
proj2D.dispose();
proj2D = new PFrame();
if (displayProjection2D) {
showProjection2D();
} else {
closeProjection2D();
}
}
}
public class projApplet extends PApplet {
public void setup() {
// Keystone will only work with P3D or OPENGL renderers,
// since it relies on texture mapping to deform
size(projectorWidth, projectorHeight, P2D);
ks = new Keystone(this);;
reset();
}
public void reset() {
surface = ks.createCornerPinSurface(tableWidth, tableHeight, 20);
offscreen = createGraphics(tableWidth, tableHeight);
try{
ks.load();
} catch(RuntimeException e){
println("No Keystone.xml. Save one first if you want to load one.");
}
}
public void draw() {
// Convert the mouse coordinate into surface coordinates
// this will allow you to use mouse events inside the
// surface from your screen.
PVector surfaceMouse = surface.getTransformedMouse();
// most likely, you'll want a black background to minimize
// bleeding around your projection area
background(0);
// Draw the scene, offscreen
renderCanvas(offscreen);
surface.render(offscreen);
}
void renderCanvas(PGraphics p) {
// Draw the scene, offscreen
p.beginDraw();
p.clear();
p.image(projector, 0, 0);
p.endDraw();
}
void keyPressed() {
switch(key) {
case 'c':
// enter/leave calibration mode, where surfaces can be warped
// and moved
ks.toggleCalibration();
break;
case 'l':
// loads the saved layout
ks.load();
break;
case 's':
// saves the layout
ks.save();
break;
case '`':
if (displayProjection2D) {
displayProjection2D = false;
closeProjection2D();
} else {
displayProjection2D = true;
showProjection2D();
}
break;
}
}
}
void toggle2DProjection() {
if (System.getProperty("os.name").substring(0,3).equals("Mac")) {
testProjectorOnMac = !testProjectorOnMac;
println("Test on Mac = " + testProjectorOnMac);
println("Projection Mapping Currently not Supported for MacOS");
} else {
if (displayProjection2D) {
displayProjection2D = false;
closeProjection2D();
} else {
displayProjection2D = true;
showProjection2D();
}
}
}
// Simulated Projector Position
// These default values are overridden by projector.txt
float projU;
float projV;
float projH;
Table projectorLocation;
void loadProjectorLocation() {
projectorLocation = loadTable("settings/projector.tsv", "header");
//Projector location (relative to table grid origin)
projU = projectorLocation.getInt(0, "U"); // Projector U Location
projV = projectorLocation.getInt(0, "V"); // Projector V Location
projH = projectorLocation.getInt(0, "H"); // Projector Height
}
void saveProjectorLocation() {
projectorLocation.setInt(0, "U", (int)projU); // Projector U Location
projectorLocation.setInt(0, "V", (int)projV); // Projector V Location
projectorLocation.setInt(0, "H", (int)projH); // Projector Height
saveTable(projectorLocation, "settings/projector.tsv");
}