2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
9 * EXPERTS ONLY!! EXPERTS ONLY!!
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.
17 import glucose.control.*;
18 import glucose.effect.*;
19 import glucose.model.*;
20 import glucose.pattern.*;
21 import glucose.transition.*;
22 import heronarts.lx.*;
23 import heronarts.lx.control.*;
24 import heronarts.lx.effect.*;
25 import heronarts.lx.modulator.*;
26 import heronarts.lx.pattern.*;
27 import heronarts.lx.transform.*;
28 import heronarts.lx.transition.*;
30 import ddf.minim.analysis.*;
31 import processing.opengl.*;
33 import java.lang.reflect.*;
35 final int VIEWPORT_WIDTH = 900;
36 final int VIEWPORT_HEIGHT = 700;
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;
43 final int MaxCubeHeight = 5;
44 final int NumBackTowers = 11;
46 int targetFramerate = 60;
47 int startMillis, lastMillis;
49 // Core engine variables
54 MappingTool mappingTool;
55 PandaDriver[] pandaBoards;
56 PresetManager presetManager;
57 MidiEngine midiEngine;
59 // Display configuration mode
60 boolean mappingMode = false;
61 boolean debugMode = false;
64 boolean simulationOn = true;
65 boolean diagnosticsOn = false;
66 LXPattern restoreToPattern = null;
68 float[] hsb = new float[3];
70 // Handles to UI objects
72 UIPatternDeck uiPatternA;
73 UICrossfader uiCrossfader;
76 UIDebugText uiDebugText;
80 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
83 * Engine construction and initialization.
86 LXTransition _transition(GLucose glucose) {
87 return new DissolveTransition(glucose.lx).setDuration(1000);
90 LXPattern[] _leftPatterns(GLucose glucose) {
91 LXPattern[] patterns = patterns(glucose);
92 for (LXPattern p : patterns) {
93 p.setTransition(_transition(glucose));
98 LXPattern[] _rightPatterns(GLucose glucose) {
99 LXPattern[] patterns = _leftPatterns(glucose);
100 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
102 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
103 for (LXPattern p : patterns) {
104 rightPatterns[i++] = p;
106 return rightPatterns;
109 LXEffect[] _effectsArray(Effects effects) {
110 List<LXEffect> effectList = new ArrayList<LXEffect>();
111 for (Field f : effects.getClass().getDeclaredFields()) {
113 Object val = f.get(effects);
114 if (val instanceof LXEffect) {
115 effectList.add((LXEffect)val);
117 } catch (IllegalAccessException iax) {}
119 return effectList.toArray(new LXEffect[]{});
122 void logTime(String evt) {
124 println(evt + ": " + (now - lastMillis) + "ms");
129 startMillis = lastMillis = millis();
131 // Initialize the Processing graphics environment
132 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
133 frameRate(targetFramerate);
135 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
136 logTime("Created viewport");
138 // Create the GLucose engine to run the cubes
139 glucose = new GLucose(this, buildModel());
141 lx.enableKeyboardTempo();
142 logTime("Built GLucose engine");
145 LXEngine engine = lx.engine;
146 engine.setPatterns(patterns = _leftPatterns(glucose));
147 engine.addDeck(_rightPatterns(glucose));
148 logTime("Built patterns");
149 glucose.setTransitions(transitions(glucose));
150 logTime("Built transitions");
151 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
152 logTime("Built effects");
155 presetManager = new PresetManager();
156 logTime("Loaded presets");
159 midiEngine = new MidiEngine();
160 logTime("Setup MIDI devices");
162 // Build output driver
163 PandaMapping[] pandaMappings = buildPandaList();
164 pandaBoards = new PandaDriver[pandaMappings.length];
166 for (PandaMapping pm : pandaMappings) {
167 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
169 mappingTool = new MappingTool(glucose, pandaMappings);
170 logTime("Built PandaDriver");
173 debugUI = new DebugUI(pandaMappings);
174 overlays = new UIContext[] {
175 uiPatternA = new UIPatternDeck(lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
176 new UIBlendMode(4, 332, 140, 86),
177 new UIEffects(4, 422, 140, 144),
178 new UITempo(4, 570, 140, 50),
179 uiSpeed = new UISpeed(4, 624, 140, 50),
181 new UIPatternDeck(lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
182 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
183 new UIOutput(width-144, 494, 140, 106),
185 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
187 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
188 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
190 uiMapping.setVisible(false);
191 logTime("Built overlay UI");
194 logo = loadImage("data/logo.png");
197 midX = TRAILER_WIDTH/2.;
198 midY = glucose.model.yMax/2;
199 midZ = TRAILER_DEPTH/2.;
203 eyeX = midX + eyeR*sin(eyeA);
204 eyeZ = midZ + eyeR*cos(eyeA);
206 // Add mouse scrolling event support
207 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
208 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
209 mouseWheel(mwe.getWheelRotation());
212 println("Total setup: " + (millis() - startMillis) + "ms");
213 println("Hit the 'p' key to toggle Panda Board output");
217 * Core render loop and drawing functionality.
220 long drawStart = System.nanoTime();
222 // Draws the simulation and the 2D UI overlay
225 color[] simulationColors;
227 simulationColors = sendColors = glucose.getColors();
228 String displayMode = uiCrossfader.getDisplayMode();
229 if (displayMode == "A") {
230 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
231 } else if (displayMode == "B") {
232 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
235 debugUI.maskColors(simulationColors);
236 debugUI.maskColors(sendColors);
239 long simulationStart = System.nanoTime();
241 drawSimulation(simulationColors);
243 long simulationNanos = System.nanoTime() - simulationStart;
246 long uiStart = System.nanoTime();
248 long uiNanos = System.nanoTime() - uiStart;
250 long gammaStart = System.nanoTime();
251 // Gamma correction here. Apply a cubic to the brightness
252 // for better representation of dynamic range
253 for (int i = 0; i < sendColors.length; ++i) {
254 lx.RGBtoHSB(sendColors[i], hsb);
256 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
258 long gammaNanos = System.nanoTime() - gammaStart;
260 long sendStart = System.nanoTime();
261 for (PandaDriver p : pandaBoards) {
264 long sendNanos = System.nanoTime() - sendStart;
266 long drawNanos = System.nanoTime() - drawStart;
269 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos);
273 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos, long sendNanos) {
274 float ws = 4 / 1000000.;
275 int thirtyfps = 1000000000 / 30;
276 int sixtyfps = 1000000000 / 60;
282 rect(x, y, thirtyfps * ws, h);
286 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos }) {
287 fill(lx.hsb(hv % 360, 100, 80));
288 rect(xp, y, val * ws, h-1);
294 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
298 float tw = thirtyfps * ws;
304 for (long val : new long[] {
305 lx.engine.timer.deckNanos,
306 lx.engine.timer.copyNanos,
307 lx.engine.timer.fxNanos}) {
308 float amt = val / (float) lx.timer.drawNanos;
309 fill(lx.hsb(hv % 360, 100, 80));
310 rect(xp, y, amt * tw, h-1);
318 for (long val : new long[] {
319 lx.engine.getDeck(0).timer.runNanos,
320 lx.engine.getDeck(1).timer.runNanos,
321 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
322 float amt = val / (float) lx.timer.drawNanos;
323 fill(lx.hsb(hv % 360, 100, 80));
324 rect(xp, y, amt * tw, h-1);
330 void drawSimulation(color[] simulationColors) {
341 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
346 vertex(TRAILER_WIDTH, 0, 0);
347 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
348 vertex(0, 0, TRAILER_DEPTH);
351 // Draw the logo on the front of platform
356 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
360 if (glucose.model.bassBox.exists) {
361 drawBassBox(glucose.model.bassBox, false);
363 for (Speaker speaker : glucose.model.speakers) {
364 drawSpeaker(speaker);
366 for (Cube c : glucose.model.cubes) {
373 for (LXPoint p : glucose.model.points) {
374 stroke(simulationColors[p.index]);
375 vertex(p.x, p.y, p.z);
380 void drawBassBox(BassBox b, boolean hasSub) {
388 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
389 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
395 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);
398 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
399 float lastOffset = 0;
400 for (float offset : BoothFloor.STRIP_OFFSETS) {
401 translate(offset - lastOffset, 0, 0);
402 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
408 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
409 for (int j = 0; j < 2; ++j) {
411 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
412 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
413 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
416 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
421 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
422 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
423 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
424 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
429 void drawCube(Cube c) {
433 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);
436 void drawSpeaker(Speaker s) {
442 translate(s.x, s.y, s.z);
443 rotate(s.ry / 180. * PI, 0, -1, 0);
444 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
445 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
446 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
449 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
454 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);
457 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
460 rotate(rx / 180. * PI, -1, 0, 0);
461 rotate(ry / 180. * PI, 0, -1, 0);
462 rotate(rz / 180. * PI, 0, 0, -1);
463 for (int i = 0; i < 4; ++i) {
464 float wid = (i % 2 == 0) ? xd : zd;
470 vertex(wid - sw, yd);
471 vertex(wid - sw, sw);
477 vertex(wid - sw, yd);
478 vertex(wid - sw, yd - sw);
483 translate(wid, 0, 0);
484 rotate(HALF_PI, 0, -1, 0);
491 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
492 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
493 ((PGraphicsOpenGL)g).endGL();
497 for (UIContext context : overlays) {
502 // Always draw FPS meter
505 textAlign(LEFT, BASELINE);
506 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
515 * Top-level keyboard event handling
519 mappingTool.keyPressed(uiMapping);
530 if (!midiEngine.isQwertyEnabled()) {
531 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
536 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
539 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
542 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
545 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
548 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
551 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
554 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
557 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
562 frameRate(--targetFramerate);
566 frameRate(++targetFramerate);
569 effects.boom.trigger();
572 if (!midiEngine.isQwertyEnabled()) {
573 debugMode = !debugMode;
574 println("Debug output: " + (debugMode ? "ON" : "OFF"));
578 if (!midiEngine.isQwertyEnabled()) {
579 mappingMode = !mappingMode;
580 uiPatternA.setVisible(!mappingMode);
581 uiMapping.setVisible(mappingMode);
583 restoreToPattern = lx.getPattern();
584 lx.setPatterns(new LXPattern[] { mappingTool });
586 lx.setPatterns(patterns);
587 LXTransition pop = restoreToPattern.getTransition();
588 restoreToPattern.setTransition(null);
589 lx.goPattern(restoreToPattern);
590 restoreToPattern.setTransition(pop);
595 if (!midiEngine.isQwertyEnabled()) {
596 lx.engine.setThreaded(!lx.engine.isThreaded());
600 for (PandaDriver p : pandaBoards) {
605 if (!midiEngine.isQwertyEnabled()) {
606 diagnosticsOn = !diagnosticsOn;
610 if (!midiEngine.isQwertyEnabled()) {
611 simulationOn = !simulationOn;
615 if (!midiEngine.isQwertyEnabled()) {
623 * Top-level mouse event handling
626 void mousePressed() {
627 boolean debugged = false;
629 debugged = debugUI.mousePressed();
632 for (UIContext context : overlays) {
633 context.mousePressed(mouseX, mouseY);
640 void mouseDragged() {
641 boolean dragged = false;
642 for (UIContext context : overlays) {
643 dragged |= context.mouseDragged(mouseX, mouseY);
646 int dx = mouseX - mx;
647 int dy = mouseY - my;
651 eyeX = midX + eyeR*sin(eyeA);
652 eyeZ = midZ + eyeR*cos(eyeA);
657 void mouseReleased() {
658 for (UIContext context : overlays) {
659 context.mouseReleased(mouseX, mouseY);
663 void mouseWheel(int delta) {
664 boolean wheeled = false;
665 for (UIContext context : overlays) {
666 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
670 eyeR = constrain(eyeR - delta, -500, -80);
671 eyeX = midX + eyeR*sin(eyeA);
672 eyeZ = midZ + eyeR*cos(eyeA);