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