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.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.*;
26 import ddf.minim.analysis.*;
27 import processing.opengl.*;
29 import java.lang.reflect.*;
31 final int VIEWPORT_WIDTH = 900;
32 final int VIEWPORT_HEIGHT = 700;
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;
39 int targetFramerate = 60;
40 int startMillis, lastMillis;
42 // Core engine variables
47 MappingTool mappingTool;
48 PandaDriver[] pandaBoards;
49 PresetManager presetManager;
50 MidiEngine midiEngine;
52 // Display configuration mode
53 boolean mappingMode = false;
54 boolean debugMode = false;
57 boolean simulationOn = true;
58 boolean diagnosticsOn = false;
59 LXPattern restoreToPattern = null;
61 float[] hsb = new float[3];
63 // Handles to UI objects
65 UIPatternDeck uiPatternA;
66 UICrossfader uiCrossfader;
69 UIDebugText uiDebugText;
73 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
76 * Engine construction and initialization.
79 LXTransition _transition(GLucose glucose) {
80 return new DissolveTransition(glucose.lx).setDuration(1000);
83 LXPattern[] _leftPatterns(GLucose glucose) {
84 LXPattern[] patterns = patterns(glucose);
85 for (LXPattern p : patterns) {
86 p.setTransition(_transition(glucose));
91 LXPattern[] _rightPatterns(GLucose glucose) {
92 LXPattern[] patterns = _leftPatterns(glucose);
93 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
95 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
96 for (LXPattern p : patterns) {
97 rightPatterns[i++] = p;
102 LXEffect[] _effectsArray(Effects effects) {
103 List<LXEffect> effectList = new ArrayList<LXEffect>();
104 for (Field f : effects.getClass().getDeclaredFields()) {
106 Object val = f.get(effects);
107 if (val instanceof LXEffect) {
108 effectList.add((LXEffect)val);
110 } catch (IllegalAccessException iax) {}
112 return effectList.toArray(new LXEffect[]{});
115 void logTime(String evt) {
117 println(evt + ": " + (now - lastMillis) + "ms");
122 startMillis = lastMillis = millis();
124 // Initialize the Processing graphics environment
125 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
126 frameRate(targetFramerate);
128 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
129 logTime("Created viewport");
131 // Create the GLucose engine to run the cubes
132 glucose = new GLucose(this, buildModel());
134 lx.enableKeyboardTempo();
135 logTime("Built GLucose engine");
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");
148 presetManager = new PresetManager();
149 logTime("Loaded presets");
152 midiEngine = new MidiEngine();
153 logTime("Setup MIDI devices");
155 // Build output driver
156 PandaMapping[] pandaMappings = buildPandaList();
157 pandaBoards = new PandaDriver[pandaMappings.length];
159 for (PandaMapping pm : pandaMappings) {
160 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
162 mappingTool = new MappingTool(glucose, pandaMappings);
163 logTime("Built PandaDriver");
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),
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),
178 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
180 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
181 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
183 uiMapping.setVisible(false);
184 logTime("Built overlay UI");
187 logo = loadImage("data/logo.png");
190 midX = TRAILER_WIDTH/2.;
191 midY = glucose.model.yMax/2;
192 midZ = TRAILER_DEPTH/2.;
196 eyeX = midX + eyeR*sin(eyeA);
197 eyeZ = midZ + eyeR*cos(eyeA);
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());
205 println("Total setup: " + (millis() - startMillis) + "ms");
206 println("Hit the 'p' key to toggle Panda Board output");
210 * Core render loop and drawing functionality.
213 long drawStart = System.nanoTime();
215 // Draws the simulation and the 2D UI overlay
218 color[] simulationColors;
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();
228 debugUI.maskColors(simulationColors);
229 debugUI.maskColors(sendColors);
232 long simulationStart = System.nanoTime();
234 drawSimulation(simulationColors);
236 long simulationNanos = System.nanoTime() - simulationStart;
239 long uiStart = System.nanoTime();
241 long uiNanos = System.nanoTime() - uiStart;
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);
249 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
251 long gammaNanos = System.nanoTime() - gammaStart;
253 long sendStart = System.nanoTime();
254 for (PandaDriver p : pandaBoards) {
257 long sendNanos = System.nanoTime() - sendStart;
259 long drawNanos = System.nanoTime() - drawStart;
262 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos);
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;
275 rect(x, y, thirtyfps * ws, h);
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);
287 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
291 float tw = thirtyfps * ws;
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);
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);
323 void drawSimulation(color[] simulationColors) {
334 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
339 vertex(TRAILER_WIDTH, 0, 0);
340 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
341 vertex(0, 0, TRAILER_DEPTH);
344 // Draw the logo on the front of platform
349 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
353 if (glucose.model.bassBox.exists) {
354 drawBassBox(glucose.model.bassBox, false);
356 for (Speaker speaker : glucose.model.speakers) {
357 drawSpeaker(speaker);
359 for (Cube c : glucose.model.cubes) {
366 for (LXPoint p : glucose.model.points) {
367 stroke(simulationColors[p.index]);
368 vertex(p.x, p.y, p.z);
373 void drawBassBox(BassBox b, boolean hasSub) {
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);
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);
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);
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) {
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);
409 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
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);
422 void drawCube(Cube c) {
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);
429 void drawSpeaker(Speaker s) {
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);
442 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
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);
450 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
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;
463 vertex(wid - sw, yd);
464 vertex(wid - sw, sw);
470 vertex(wid - sw, yd);
471 vertex(wid - sw, yd - sw);
476 translate(wid, 0, 0);
477 rotate(HALF_PI, 0, -1, 0);
484 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
485 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
486 ((PGraphicsOpenGL)g).endGL();
490 for (UIContext context : overlays) {
495 // Always draw FPS meter
498 textAlign(LEFT, BASELINE);
499 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
508 * Top-level keyboard event handling
512 mappingTool.keyPressed(uiMapping);
523 if (!midiEngine.isQwertyEnabled()) {
524 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
529 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
532 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
535 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
538 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
541 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
544 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
547 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
550 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
555 frameRate(--targetFramerate);
559 frameRate(++targetFramerate);
562 effects.boom.trigger();
565 if (!midiEngine.isQwertyEnabled()) {
566 debugMode = !debugMode;
567 println("Debug output: " + (debugMode ? "ON" : "OFF"));
571 if (!midiEngine.isQwertyEnabled()) {
572 mappingMode = !mappingMode;
573 uiPatternA.setVisible(!mappingMode);
574 uiMapping.setVisible(mappingMode);
576 restoreToPattern = lx.getPattern();
577 lx.setPatterns(new LXPattern[] { mappingTool });
579 lx.setPatterns(patterns);
580 LXTransition pop = restoreToPattern.getTransition();
581 restoreToPattern.setTransition(null);
582 lx.goPattern(restoreToPattern);
583 restoreToPattern.setTransition(pop);
588 if (!midiEngine.isQwertyEnabled()) {
589 lx.engine.setThreaded(!lx.engine.isThreaded());
593 for (PandaDriver p : pandaBoards) {
598 if (!midiEngine.isQwertyEnabled()) {
599 diagnosticsOn = !diagnosticsOn;
603 if (!midiEngine.isQwertyEnabled()) {
604 simulationOn = !simulationOn;
608 if (!midiEngine.isQwertyEnabled()) {
616 * Top-level mouse event handling
619 void mousePressed() {
620 boolean debugged = false;
622 debugged = debugUI.mousePressed();
625 for (UIContext context : overlays) {
626 context.mousePressed(mouseX, mouseY);
633 void mouseDragged() {
634 boolean dragged = false;
635 for (UIContext context : overlays) {
636 dragged |= context.mouseDragged(mouseX, mouseY);
639 int dx = mouseX - mx;
640 int dy = mouseY - my;
644 eyeX = midX + eyeR*sin(eyeA);
645 eyeZ = midZ + eyeR*cos(eyeA);
650 void mouseReleased() {
651 for (UIContext context : overlays) {
652 context.mouseReleased(mouseX, mouseY);
656 void mouseWheel(int delta) {
657 boolean wheeled = false;
658 for (UIContext context : overlays) {
659 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
663 eyeR = constrain(eyeR - delta, -500, -80);
664 eyeX = midX + eyeR*sin(eyeA);
665 eyeZ = midZ + eyeR*cos(eyeA);