Update mapping and debugging tools to support bassbin, speakers, and booth floor
[SugarCubes.git] / _Internals.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
11 * If you are an artist, you may ignore this file! It just sets
12 * up the framework to run the patterns. Should not need modification
13 * for general animation work.
14 */
15
16 import glucose.*;
17 import glucose.control.*;
18 import glucose.effect.*;
19 import glucose.model.*;
20 import glucose.pattern.*;
21 import glucose.transform.*;
22 import glucose.transition.*;
23 import heronarts.lx.*;
24 import heronarts.lx.control.*;
25 import heronarts.lx.effect.*;
26 import heronarts.lx.modulator.*;
27 import heronarts.lx.pattern.*;
28 import heronarts.lx.transition.*;
29 import ddf.minim.*;
30 import ddf.minim.analysis.*;
31 import processing.opengl.*;
32 import rwmidi.*;
33
34 final int VIEWPORT_WIDTH = 900;
35 final int VIEWPORT_HEIGHT = 700;
36
37 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
38 final float TRAILER_WIDTH = 240;
39 final float TRAILER_DEPTH = 97;
40 final float TRAILER_HEIGHT = 33;
41
42 int targetFramerate = 60;
43
44 int startMillis, lastMillis;
45 GLucose glucose;
46 HeronLX lx;
47 MappingTool mappingTool;
48 LXPattern[] patterns;
49 LXTransition[] transitions;
50 LXEffect[] effects;
51 OverlayUI ui;
52 ControlUI controlUI;
53 MappingUI mappingUI;
54 PandaDriver[] pandaBoards;
55 boolean mappingMode = false;
56 boolean debugMode = false;
57 DebugUI debugUI;
58
59 // Camera variables
60 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
61
62 void setup() {
63 startMillis = lastMillis = millis();
64
65 // Initialize the Processing graphics environment
66 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
67 frameRate(targetFramerate);
68 noSmooth();
69 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
70 logTime("Created viewport");
71
72 // Create the GLucose engine to run the cubes
73 glucose = new GLucose(this, buildModel());
74 lx = glucose.lx;
75 lx.enableKeyboardTempo();
76 logTime("Built GLucose engine");
77
78 // Set the patterns
79 glucose.lx.setPatterns(patterns = patterns(glucose));
80 logTime("Built patterns");
81 glucose.lx.addEffects(effects = effects(glucose));
82 logTime("Built effects");
83 glucose.setTransitions(transitions = transitions(glucose));
84 logTime("Built transitions");
85
86 // Build output driver
87 PandaMapping[] pandaMappings = buildPandaList();
88 pandaBoards = new PandaDriver[pandaMappings.length];
89 int pbi = 0;
90 for (PandaMapping pm : pandaMappings) {
91 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
92 }
93 mappingTool = new MappingTool(glucose, pandaMappings);
94 logTime("Built PandaDriver");
95
96 // Build overlay UI
97 ui = controlUI = new ControlUI();
98 mappingUI = new MappingUI(mappingTool);
99 debugUI = new DebugUI(pandaMappings);
100 logTime("Built overlay UI");
101
102 // MIDI devices
103 for (MidiInputDevice d : RWMidi.getInputDevices()) {
104 d.createInput(this);
105 }
106 SCMidiDevices.initializeStandardDevices(glucose);
107 logTime("Setup MIDI devices");
108
109 // Setup camera
110 midX = TRAILER_WIDTH/2. + 20;
111 midY = glucose.model.yMax/2;
112 midZ = TRAILER_DEPTH/2.;
113 eyeR = -290;
114 eyeA = .15;
115 eyeY = midY + 20;
116 eyeX = midX + eyeR*sin(eyeA);
117 eyeZ = midZ + eyeR*cos(eyeA);
118 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
119 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
120 mouseWheel(mwe.getWheelRotation());
121 }});
122
123 println("Total setup: " + (millis() - startMillis) + "ms");
124 println("Hit the 'p' key to toggle Panda Board output");
125 }
126
127 void controllerChangeReceived(rwmidi.Controller cc) {
128 if (debugMode) {
129 println("CC: " + cc.toString());
130 }
131 }
132
133 void noteOnReceived(Note note) {
134 if (debugMode) {
135 println("Note On: " + note.toString());
136 }
137 }
138
139 void noteOffReceived(Note note) {
140 if (debugMode) {
141 println("Note Off: " + note.toString());
142 }
143 }
144
145 void logTime(String evt) {
146 int now = millis();
147 println(evt + ": " + (now - lastMillis) + "ms");
148 lastMillis = now;
149 }
150
151 void draw() {
152 // Draws the simulation and the 2D UI overlay
153 background(40);
154 color[] colors = glucose.getColors();
155 if (debugMode) {
156 debugUI.maskColors(colors);
157 }
158
159 camera(
160 eyeX, eyeY, eyeZ,
161 midX, midY, midZ,
162 0, -1, 0
163 );
164
165 noStroke();
166 fill(#141414);
167 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
168 fill(#070707);
169 stroke(#222222);
170 beginShape();
171 vertex(0, 0, 0);
172 vertex(TRAILER_WIDTH, 0, 0);
173 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
174 vertex(0, 0, TRAILER_DEPTH);
175 endShape();
176
177 noStroke();
178 drawBassBox(glucose.model.bassBox);
179 for (Speaker s : glucose.model.speakers) {
180 drawSpeaker(s);
181 }
182 for (Cube c : glucose.model.cubes) {
183 drawCube(c);
184 }
185
186 noFill();
187 strokeWeight(2);
188 beginShape(POINTS);
189 for (Point p : glucose.model.points) {
190 stroke(colors[p.index]);
191 vertex(p.fx, p.fy, p.fz);
192 }
193 endShape();
194
195 // 2D Overlay
196 camera();
197 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
198 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
199 ((PGraphicsOpenGL)g).endGL();
200 strokeWeight(1);
201 drawUI();
202
203 if (debugMode) {
204 debugUI.draw();
205 }
206
207 // TODO(mcslee): move into GLucose engine
208 for (PandaDriver p : pandaBoards) {
209 p.send(colors);
210 }
211 }
212
213 void drawBassBox(BassBox b) {
214 float in = .15;
215
216 noStroke();
217 fill(#191919);
218 pushMatrix();
219 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
220 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
221 popMatrix();
222
223 noStroke();
224 fill(#393939);
225 drawBox(b.x+in, b.y+in, b.z+in, 0, 0, 0, BassBox.EDGE_WIDTH-in*2, BassBox.EDGE_HEIGHT-in*2, BassBox.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
226
227 pushMatrix();
228 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
229 float lastOffset = 0;
230 for (float offset : BoothFloor.STRIP_OFFSETS) {
231 translate(offset - lastOffset, 0, 0);
232 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
233 lastOffset = offset;
234 }
235 popMatrix();
236
237 pushMatrix();
238 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
239 for (int j = 0; j < 2; ++j) {
240 pushMatrix();
241 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
242 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
243 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
244 }
245 popMatrix();
246 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
247 }
248 popMatrix();
249
250 pushMatrix();
251 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
252 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
253 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
254 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
255 popMatrix();
256
257 }
258
259 void drawCube(Cube c) {
260 float in = .15;
261 noStroke();
262 fill(#393939);
263 drawBox(c.x+in, c.y+in, c.z+in, c.rx, c.ry, c.rz, Cube.EDGE_WIDTH-in*2, Cube.EDGE_HEIGHT-in*2, Cube.EDGE_WIDTH-in*2, Cube.CHANNEL_WIDTH-in);
264 }
265
266 void drawSpeaker(Speaker s) {
267 float in = .15;
268
269 noStroke();
270 fill(#191919);
271 pushMatrix();
272 translate(s.x, s.y, s.z);
273 rotate(s.ry / 180. * PI, 0, -1, 0);
274 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
275 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
276 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
277
278 fill(#222222);
279 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
280 popMatrix();
281
282 noStroke();
283 fill(#393939);
284 drawBox(s.x+in, s.y+in, s.z+in, 0, s.ry, 0, Speaker.EDGE_WIDTH-in*2, Speaker.EDGE_HEIGHT-in*2, Speaker.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
285 }
286
287
288 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
289 pushMatrix();
290 translate(x, y, z);
291 rotate(rx / 180. * PI, -1, 0, 0);
292 rotate(ry / 180. * PI, 0, -1, 0);
293 rotate(rz / 180. * PI, 0, 0, -1);
294 for (int i = 0; i < 4; ++i) {
295 float wid = (i % 2 == 0) ? xd : zd;
296
297 beginShape();
298 vertex(0, 0);
299 vertex(wid, 0);
300 vertex(wid, yd);
301 vertex(wid - sw, yd);
302 vertex(wid - sw, sw);
303 vertex(0, sw);
304 endShape();
305 beginShape();
306 vertex(0, sw);
307 vertex(0, yd);
308 vertex(wid - sw, yd);
309 vertex(wid - sw, yd - sw);
310 vertex(sw, yd - sw);
311 vertex(sw, sw);
312 endShape();
313
314 translate(wid, 0, 0);
315 rotate(HALF_PI, 0, -1, 0);
316 }
317 popMatrix();
318 }
319
320 void drawUI() {
321 if (uiOn) {
322 ui.draw();
323 } else {
324 ui.drawHelpTip();
325 }
326 ui.drawFPS();
327 }
328
329 boolean uiOn = true;
330 int restoreToIndex = -1;
331
332 void keyPressed() {
333 if (mappingMode) {
334 mappingTool.keyPressed();
335 }
336 switch (key) {
337 case '-':
338 case '_':
339 frameRate(--targetFramerate);
340 break;
341 case '=':
342 case '+':
343 frameRate(++targetFramerate);
344 break;
345 case 'd':
346 debugMode = !debugMode;
347 println("Debug output: " + (debugMode ? "ON" : "OFF"));
348 break;
349 case 'm':
350 mappingMode = !mappingMode;
351 if (mappingMode) {
352 LXPattern pattern = lx.getPattern();
353 for (int i = 0; i < patterns.length; ++i) {
354 if (pattern == patterns[i]) {
355 restoreToIndex = i;
356 break;
357 }
358 }
359 ui = mappingUI;
360 lx.setPatterns(new LXPattern[] { mappingTool });
361 } else {
362 ui = controlUI;
363 lx.setPatterns(patterns);
364 lx.goIndex(restoreToIndex);
365 }
366 break;
367 case 'p':
368 for (PandaDriver p : pandaBoards) {
369 p.toggle();
370 }
371 break;
372 case 'u':
373 uiOn = !uiOn;
374 break;
375 }
376 }
377
378 int mx, my;
379 void mousePressed() {
380 ui.mousePressed();
381 if (mouseX < ui.leftPos) {
382 if (debugMode) {
383 debugUI.mousePressed();
384 }
385 mx = mouseX;
386 my = mouseY;
387 }
388 }
389
390 void mouseDragged() {
391 if (mouseX > ui.leftPos) {
392 ui.mouseDragged();
393 } else {
394 int dx = mouseX - mx;
395 int dy = mouseY - my;
396 mx = mouseX;
397 my = mouseY;
398 eyeA += dx*.003;
399 eyeX = midX + eyeR*sin(eyeA);
400 eyeZ = midZ + eyeR*cos(eyeA);
401 eyeY += dy;
402 }
403 }
404
405 void mouseReleased() {
406 ui.mouseReleased();
407 }
408
409 void mouseWheel(int delta) {
410 if (mouseX > ui.leftPos) {
411 ui.mouseWheel(delta);
412 } else {
413 eyeR = constrain(eyeR - delta, -500, -80);
414 eyeX = midX + eyeR*sin(eyeA);
415 eyeZ = midZ + eyeR*cos(eyeA);
416 }
417 }
418