Map first APC slider to overall speed
[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 import java.lang.reflect.*;
34
35 final int VIEWPORT_WIDTH = 900;
36 final int VIEWPORT_HEIGHT = 700;
37
38 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
39 final float TRAILER_WIDTH = 240;
40 final float TRAILER_DEPTH = 97;
41 final float TRAILER_HEIGHT = 33;
42
43 final int MaxCubeHeight = 5;
44 final int NumBackTowers = 11;
45
46 int targetFramerate = 60;
47 int startMillis, lastMillis;
48
49 // Core engine variables
50 GLucose glucose;
51 HeronLX lx;
52 LXPattern[] patterns;
53 Effects effects;
54 MappingTool mappingTool;
55 PandaDriver[] pandaBoards;
56 PresetManager presetManager;
57 MidiEngine midiEngine;
58
59 // Display configuration mode
60 boolean mappingMode = false;
61 boolean debugMode = false;
62 DebugUI debugUI;
63 boolean uiOn = true;
64 LXPattern restoreToPattern = null;
65 PImage logo;
66 float[] hsb = new float[3];
67
68 // Handles to UI objects
69 UIContext[] overlays;
70 UIPatternDeck uiPatternA;
71 UICrossfader uiCrossfader;
72 UIMidi uiMidi;
73 UIMapping uiMapping;
74 UIDebugText uiDebugText;
75 UISpeed uiSpeed;
76
77 // Camera variables
78 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
79
80 /**
81 * Engine construction and initialization.
82 */
83
84 LXTransition _transition(GLucose glucose) {
85 return new DissolveTransition(glucose.lx).setDuration(1000);
86 }
87
88 LXPattern[] _leftPatterns(GLucose glucose) {
89 LXPattern[] patterns = patterns(glucose);
90 for (LXPattern p : patterns) {
91 p.setTransition(_transition(glucose));
92 }
93 return patterns;
94 }
95
96 LXPattern[] _rightPatterns(GLucose glucose) {
97 LXPattern[] patterns = _leftPatterns(glucose);
98 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
99 int i = 0;
100 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
101 for (LXPattern p : patterns) {
102 rightPatterns[i++] = p;
103 }
104 return rightPatterns;
105 }
106
107 LXEffect[] _effectsArray(Effects effects) {
108 List<LXEffect> effectList = new ArrayList<LXEffect>();
109 for (Field f : effects.getClass().getDeclaredFields()) {
110 try {
111 Object val = f.get(effects);
112 if (val instanceof LXEffect) {
113 effectList.add((LXEffect)val);
114 }
115 } catch (IllegalAccessException iax) {}
116 }
117 return effectList.toArray(new LXEffect[]{});
118 }
119
120 void logTime(String evt) {
121 int now = millis();
122 println(evt + ": " + (now - lastMillis) + "ms");
123 lastMillis = now;
124 }
125
126 void setup() {
127 startMillis = lastMillis = millis();
128
129 // Initialize the Processing graphics environment
130 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
131 frameRate(targetFramerate);
132 noSmooth();
133 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
134 logTime("Created viewport");
135
136 // Create the GLucose engine to run the cubes
137 glucose = new GLucose(this, buildModel());
138 lx = glucose.lx;
139 lx.enableKeyboardTempo();
140 logTime("Built GLucose engine");
141
142 // Set the patterns
143 LXEngine engine = lx.engine;
144 engine.setPatterns(patterns = _leftPatterns(glucose));
145 engine.addDeck(_rightPatterns(glucose));
146 logTime("Built patterns");
147 glucose.setTransitions(transitions(glucose));
148 logTime("Built transitions");
149 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
150 logTime("Built effects");
151
152 // Preset manager
153 presetManager = new PresetManager();
154 logTime("Loaded presets");
155
156 // MIDI devices
157 midiEngine = new MidiEngine();
158 logTime("Setup MIDI devices");
159
160 // Build output driver
161 PandaMapping[] pandaMappings = buildPandaList();
162 pandaBoards = new PandaDriver[pandaMappings.length];
163 int pbi = 0;
164 for (PandaMapping pm : pandaMappings) {
165 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
166 }
167 mappingTool = new MappingTool(glucose, pandaMappings);
168 logTime("Built PandaDriver");
169
170 // Build overlay UI
171 debugUI = new DebugUI(pandaMappings);
172 overlays = new UIContext[] {
173 uiPatternA = new UIPatternDeck(lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
174 new UIBlendMode(4, 332, 140, 86),
175 new UIEffects(4, 422, 140, 144),
176 new UITempo(4, 570, 140, 50),
177 uiSpeed = new UISpeed(4, 624, 140, 50),
178
179 new UIPatternDeck(lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
180 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
181 new UIOutput(width-144, 494, 140, 106),
182
183 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
184
185 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
186 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
187 };
188 uiMapping.setVisible(false);
189 logTime("Built overlay UI");
190
191 // Load logo image
192 logo = loadImage("data/logo.png");
193
194 // Setup camera
195 midX = TRAILER_WIDTH/2.;
196 midY = glucose.model.yMax/2;
197 midZ = TRAILER_DEPTH/2.;
198 eyeR = -290;
199 eyeA = .15;
200 eyeY = midY + 70;
201 eyeX = midX + eyeR*sin(eyeA);
202 eyeZ = midZ + eyeR*cos(eyeA);
203
204 // Add mouse scrolling event support
205 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
206 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
207 mouseWheel(mwe.getWheelRotation());
208 }});
209
210 println("Total setup: " + (millis() - startMillis) + "ms");
211 println("Hit the 'p' key to toggle Panda Board output");
212 }
213
214 /**
215 * Core render loop and drawing functionality.
216 */
217 void draw() {
218 // Draws the simulation and the 2D UI overlay
219 background(40);
220
221 color[] simulationColors;
222 color[] sendColors;
223 simulationColors = sendColors = glucose.getColors();
224 String displayMode = uiCrossfader.getDisplayMode();
225 if (displayMode == "A") {
226 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
227 } else if (displayMode == "B") {
228 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
229 }
230 if (debugMode) {
231 debugUI.maskColors(simulationColors);
232 debugUI.maskColors(sendColors);
233 }
234
235 camera(
236 eyeX, eyeY, eyeZ,
237 midX, midY, midZ,
238 0, -1, 0
239 );
240
241 translate(0, 40, 0);
242
243 noStroke();
244 fill(#141414);
245 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
246 fill(#070707);
247 stroke(#222222);
248 beginShape();
249 vertex(0, 0, 0);
250 vertex(TRAILER_WIDTH, 0, 0);
251 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
252 vertex(0, 0, TRAILER_DEPTH);
253 endShape();
254
255 // Draw the logo on the front of platform
256 pushMatrix();
257 translate(0, 0, -1);
258 float s = .07;
259 scale(s, -s, s);
260 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
261 popMatrix();
262
263 noStroke();
264 if (glucose.model.bassBox.exists) {
265 drawBassBox(glucose.model.bassBox, false);
266 }
267 for (Speaker speaker : glucose.model.speakers) {
268 drawSpeaker(speaker);
269 }
270 for (Cube c : glucose.model.cubes) {
271 drawCube(c);
272 }
273
274 noFill();
275 strokeWeight(2);
276 beginShape(POINTS);
277 for (Point p : glucose.model.points) {
278 stroke(simulationColors[p.index]);
279 vertex(p.x, p.y, p.z);
280 }
281 endShape();
282
283 // 2D Overlay UI
284 drawUI();
285
286 // Gamma correction here. Apply a cubic to the brightness
287 // for better representation of dynamic range
288 for (int i = 0; i < sendColors.length; ++i) {
289 lx.RGBtoHSB(sendColors[i], hsb);
290 float b = hsb[2];
291 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
292 }
293
294 // TODO(mcslee): move into GLucose engine
295 for (PandaDriver p : pandaBoards) {
296 p.send(sendColors);
297 }
298 }
299
300 void drawBassBox(BassBox b, boolean hasSub) {
301
302 float in = .15;
303
304 if (hasSub) {
305 noStroke();
306 fill(#191919);
307 pushMatrix();
308 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
309 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
310 popMatrix();
311 }
312
313 noStroke();
314 fill(#393939);
315 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);
316
317 pushMatrix();
318 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
319 float lastOffset = 0;
320 for (float offset : BoothFloor.STRIP_OFFSETS) {
321 translate(offset - lastOffset, 0, 0);
322 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
323 lastOffset = offset;
324 }
325 popMatrix();
326
327 pushMatrix();
328 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
329 for (int j = 0; j < 2; ++j) {
330 pushMatrix();
331 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
332 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
333 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
334 }
335 popMatrix();
336 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
337 }
338 popMatrix();
339
340 pushMatrix();
341 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
342 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
343 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
344 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
345 popMatrix();
346
347 }
348
349 void drawCube(Cube c) {
350 float in = .15;
351 noStroke();
352 fill(#393939);
353 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);
354 }
355
356 void drawSpeaker(Speaker s) {
357 float in = .15;
358
359 noStroke();
360 fill(#191919);
361 pushMatrix();
362 translate(s.x, s.y, s.z);
363 rotate(s.ry / 180. * PI, 0, -1, 0);
364 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
365 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
366 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
367
368 fill(#222222);
369 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
370 popMatrix();
371
372 noStroke();
373 fill(#393939);
374 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);
375 }
376
377 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
378 pushMatrix();
379 translate(x, y, z);
380 rotate(rx / 180. * PI, -1, 0, 0);
381 rotate(ry / 180. * PI, 0, -1, 0);
382 rotate(rz / 180. * PI, 0, 0, -1);
383 for (int i = 0; i < 4; ++i) {
384 float wid = (i % 2 == 0) ? xd : zd;
385
386 beginShape();
387 vertex(0, 0);
388 vertex(wid, 0);
389 vertex(wid, yd);
390 vertex(wid - sw, yd);
391 vertex(wid - sw, sw);
392 vertex(0, sw);
393 endShape();
394 beginShape();
395 vertex(0, sw);
396 vertex(0, yd);
397 vertex(wid - sw, yd);
398 vertex(wid - sw, yd - sw);
399 vertex(sw, yd - sw);
400 vertex(sw, sw);
401 endShape();
402
403 translate(wid, 0, 0);
404 rotate(HALF_PI, 0, -1, 0);
405 }
406 popMatrix();
407 }
408
409 void drawUI() {
410 camera();
411 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
412 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
413 ((PGraphicsOpenGL)g).endGL();
414 strokeWeight(1);
415
416 if (uiOn) {
417 for (UIContext context : overlays) {
418 context.draw();
419 }
420 }
421
422 // Always draw FPS meter
423 fill(#555555);
424 textSize(9);
425 textAlign(LEFT, BASELINE);
426 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
427
428 if (debugMode) {
429 debugUI.draw();
430 }
431 }
432
433
434 /**
435 * Top-level keyboard event handling
436 */
437 void keyPressed() {
438 if (mappingMode) {
439 mappingTool.keyPressed(uiMapping);
440 }
441 switch (key) {
442 case '1':
443 case '2':
444 case '3':
445 case '4':
446 case '5':
447 case '6':
448 case '7':
449 case '8':
450 if (!midiEngine.isQwertyEnabled()) {
451 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
452 }
453 break;
454
455 case '!':
456 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
457 break;
458 case '@':
459 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
460 break;
461 case '#':
462 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
463 break;
464 case '$':
465 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
466 break;
467 case '%':
468 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
469 break;
470 case '^':
471 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
472 break;
473 case '&':
474 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
475 break;
476 case '*':
477 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
478 break;
479
480 case '-':
481 case '_':
482 frameRate(--targetFramerate);
483 break;
484 case '=':
485 case '+':
486 frameRate(++targetFramerate);
487 break;
488 case 'b':
489 effects.boom.trigger();
490 break;
491 case 'd':
492 if (!midiEngine.isQwertyEnabled()) {
493 debugMode = !debugMode;
494 println("Debug output: " + (debugMode ? "ON" : "OFF"));
495 }
496 break;
497 case 'm':
498 if (!midiEngine.isQwertyEnabled()) {
499 mappingMode = !mappingMode;
500 uiPatternA.setVisible(!mappingMode);
501 uiMapping.setVisible(mappingMode);
502 if (mappingMode) {
503 restoreToPattern = lx.getPattern();
504 lx.setPatterns(new LXPattern[] { mappingTool });
505 } else {
506 lx.setPatterns(patterns);
507 LXTransition pop = restoreToPattern.getTransition();
508 restoreToPattern.setTransition(null);
509 lx.goPattern(restoreToPattern);
510 restoreToPattern.setTransition(pop);
511 }
512 }
513 break;
514 case 't':
515 if (!midiEngine.isQwertyEnabled()) {
516 lx.engine.setThreaded(!lx.engine.isThreaded());
517 }
518 break;
519 case 'p':
520 for (PandaDriver p : pandaBoards) {
521 p.toggle();
522 }
523 break;
524 case 'u':
525 if (!midiEngine.isQwertyEnabled()) {
526 uiOn = !uiOn;
527 }
528 break;
529 }
530 }
531
532 /**
533 * Top-level mouse event handling
534 */
535 int mx, my;
536 void mousePressed() {
537 boolean debugged = false;
538 if (debugMode) {
539 debugged = debugUI.mousePressed();
540 }
541 if (!debugged) {
542 for (UIContext context : overlays) {
543 context.mousePressed(mouseX, mouseY);
544 }
545 }
546 mx = mouseX;
547 my = mouseY;
548 }
549
550 void mouseDragged() {
551 boolean dragged = false;
552 for (UIContext context : overlays) {
553 dragged |= context.mouseDragged(mouseX, mouseY);
554 }
555 if (!dragged) {
556 int dx = mouseX - mx;
557 int dy = mouseY - my;
558 mx = mouseX;
559 my = mouseY;
560 eyeA += dx*.003;
561 eyeX = midX + eyeR*sin(eyeA);
562 eyeZ = midZ + eyeR*cos(eyeA);
563 eyeY += dy;
564 }
565 }
566
567 void mouseReleased() {
568 for (UIContext context : overlays) {
569 context.mouseReleased(mouseX, mouseY);
570 }
571 }
572
573 void mouseWheel(int delta) {
574 boolean wheeled = false;
575 for (UIContext context : overlays) {
576 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
577 }
578
579 if (!wheeled) {
580 eyeR = constrain(eyeR - delta, -500, -80);
581 eyeX = midX + eyeR*sin(eyeA);
582 eyeZ = midZ + eyeR*cos(eyeA);
583 }
584 }