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.control.*;
20 import heronarts.lx.effect.*;
21 import heronarts.lx.modulator.*;
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 final int MaxCubeHeight = 5;
40 final int NumBackTowers = 11;
42 int targetFramerate = 60;
43 int startMillis, lastMillis;
45 // Core engine variables
50 MappingTool mappingTool;
51 PandaDriver[] pandaBoards;
52 PresetManager presetManager;
53 MidiEngine midiEngine;
55 // Display configuration mode
56 boolean mappingMode = false;
57 boolean debugMode = false;
60 boolean simulationOn = true;
61 boolean diagnosticsOn = false;
62 LXPattern restoreToPattern = null;
64 float[] hsb = new float[3];
66 // Handles to UI objects
68 UIPatternDeck uiPatternA;
69 UICrossfader uiCrossfader;
72 UIDebugText uiDebugText;
76 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
79 * Engine construction and initialization.
82 LXTransition _transition(GLucose glucose) {
83 return new DissolveTransition(glucose.lx).setDuration(1000);
86 LXPattern[] _leftPatterns(GLucose glucose) {
87 LXPattern[] patterns = patterns(glucose);
88 for (LXPattern p : patterns) {
89 p.setTransition(_transition(glucose));
94 LXPattern[] _rightPatterns(GLucose glucose) {
95 LXPattern[] patterns = _leftPatterns(glucose);
96 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
98 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
99 for (LXPattern p : patterns) {
100 rightPatterns[i++] = p;
102 return rightPatterns;
105 LXEffect[] _effectsArray(Effects effects) {
106 List<LXEffect> effectList = new ArrayList<LXEffect>();
107 for (Field f : effects.getClass().getDeclaredFields()) {
109 Object val = f.get(effects);
110 if (val instanceof LXEffect) {
111 effectList.add((LXEffect)val);
113 } catch (IllegalAccessException iax) {}
115 return effectList.toArray(new LXEffect[]{});
118 void logTime(String evt) {
120 println(evt + ": " + (now - lastMillis) + "ms");
125 startMillis = lastMillis = millis();
127 // Initialize the Processing graphics environment
128 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
129 frameRate(targetFramerate);
131 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
132 logTime("Created viewport");
134 // Create the GLucose engine to run the cubes
135 glucose = new GLucose(this, buildModel());
137 lx.enableKeyboardTempo();
138 logTime("Built GLucose engine");
141 LXEngine engine = lx.engine;
142 engine.setPatterns(patterns = _leftPatterns(glucose));
143 engine.addDeck(_rightPatterns(glucose));
144 logTime("Built patterns");
145 glucose.setTransitions(transitions(glucose));
146 logTime("Built transitions");
147 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
148 logTime("Built effects");
151 presetManager = new PresetManager();
152 logTime("Loaded presets");
155 midiEngine = new MidiEngine();
156 logTime("Setup MIDI devices");
158 // Build output driver
159 PandaMapping[] pandaMappings = buildPandaList();
160 pandaBoards = new PandaDriver[pandaMappings.length];
162 for (PandaMapping pm : pandaMappings) {
163 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
165 mappingTool = new MappingTool(glucose, pandaMappings);
166 logTime("Built PandaDriver");
169 debugUI = new DebugUI(pandaMappings);
170 overlays = new UIContext[] {
171 uiPatternA = new UIPatternDeck(lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
172 new UIBlendMode(4, 332, 140, 86),
173 new UIEffects(4, 422, 140, 144),
174 new UITempo(4, 570, 140, 50),
175 uiSpeed = new UISpeed(4, 624, 140, 50),
177 new UIPatternDeck(lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
178 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
179 new UIOutput(width-144, 494, 140, 106),
181 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
183 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
184 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
186 uiMapping.setVisible(false);
187 logTime("Built overlay UI");
190 logo = loadImage("data/logo.png");
193 midX = TRAILER_WIDTH/2.;
194 midY = glucose.model.yMax/2;
195 midZ = TRAILER_DEPTH/2.;
199 eyeX = midX + eyeR*sin(eyeA);
200 eyeZ = midZ + eyeR*cos(eyeA);
202 // Add mouse scrolling event support
203 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
204 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
205 mouseWheel(mwe.getWheelRotation());
208 println("Total setup: " + (millis() - startMillis) + "ms");
209 println("Hit the 'p' key to toggle Panda Board output");
213 * Core render loop and drawing functionality.
216 long drawStart = System.nanoTime();
218 // Draws the simulation and the 2D UI overlay
221 color[] simulationColors;
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();
231 debugUI.maskColors(simulationColors);
232 debugUI.maskColors(sendColors);
235 long simulationStart = System.nanoTime();
237 drawSimulation(simulationColors);
239 long simulationNanos = System.nanoTime() - simulationStart;
242 long uiStart = System.nanoTime();
244 long uiNanos = System.nanoTime() - uiStart;
246 long gammaStart = System.nanoTime();
247 // Gamma correction here. Apply a cubic to the brightness
248 // for better representation of dynamic range
249 for (int i = 0; i < sendColors.length; ++i) {
250 lx.RGBtoHSB(sendColors[i], hsb);
252 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
254 long gammaNanos = System.nanoTime() - gammaStart;
256 long sendStart = System.nanoTime();
257 for (PandaDriver p : pandaBoards) {
260 long sendNanos = System.nanoTime() - sendStart;
262 long drawNanos = System.nanoTime() - drawStart;
265 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos);
269 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos, long sendNanos) {
270 float ws = 4 / 1000000.;
271 int thirtyfps = 1000000000 / 30;
272 int sixtyfps = 1000000000 / 60;
278 rect(x, y, thirtyfps * ws, h);
282 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos }) {
283 fill(lx.hsb(hv % 360, 100, 80));
284 rect(xp, y, val * ws, h-1);
290 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
294 float tw = thirtyfps * ws;
300 for (long val : new long[] {
301 lx.engine.timer.deckNanos,
302 lx.engine.timer.copyNanos,
303 lx.engine.timer.fxNanos}) {
304 float amt = val / (float) lx.timer.drawNanos;
305 fill(lx.hsb(hv % 360, 100, 80));
306 rect(xp, y, amt * tw, h-1);
314 for (long val : new long[] {
315 lx.engine.getDeck(0).timer.runNanos,
316 lx.engine.getDeck(1).timer.runNanos,
317 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
318 float amt = val / (float) lx.timer.drawNanos;
319 fill(lx.hsb(hv % 360, 100, 80));
320 rect(xp, y, amt * tw, h-1);
326 void drawSimulation(color[] simulationColors) {
337 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
342 vertex(TRAILER_WIDTH, 0, 0);
343 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
344 vertex(0, 0, TRAILER_DEPTH);
347 // Draw the logo on the front of platform
352 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
356 if (glucose.model.bassBox.exists) {
357 drawBassBox(glucose.model.bassBox, false);
359 for (Speaker speaker : glucose.model.speakers) {
360 drawSpeaker(speaker);
362 for (Cube c : glucose.model.cubes) {
369 for (LXPoint p : glucose.model.points) {
370 stroke(simulationColors[p.index]);
371 vertex(p.x, p.y, p.z);
376 void drawBassBox(BassBox b, boolean hasSub) {
384 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
385 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
391 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);
394 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
395 float lastOffset = 0;
396 for (float offset : BoothFloor.STRIP_OFFSETS) {
397 translate(offset - lastOffset, 0, 0);
398 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
404 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
405 for (int j = 0; j < 2; ++j) {
407 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
408 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
409 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
412 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
417 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
418 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
419 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
420 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
425 void drawCube(Cube c) {
429 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);
432 void drawSpeaker(Speaker s) {
438 translate(s.x, s.y, s.z);
439 rotate(s.ry / 180. * PI, 0, -1, 0);
440 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
441 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
442 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
445 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
450 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);
453 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
456 rotate(rx / 180. * PI, -1, 0, 0);
457 rotate(ry / 180. * PI, 0, -1, 0);
458 rotate(rz / 180. * PI, 0, 0, -1);
459 for (int i = 0; i < 4; ++i) {
460 float wid = (i % 2 == 0) ? xd : zd;
466 vertex(wid - sw, yd);
467 vertex(wid - sw, sw);
473 vertex(wid - sw, yd);
474 vertex(wid - sw, yd - sw);
479 translate(wid, 0, 0);
480 rotate(HALF_PI, 0, -1, 0);
487 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
488 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
489 ((PGraphicsOpenGL)g).endGL();
493 for (UIContext context : overlays) {
498 // Always draw FPS meter
501 textAlign(LEFT, BASELINE);
502 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
511 * Top-level keyboard event handling
515 mappingTool.keyPressed(uiMapping);
526 if (!midiEngine.isQwertyEnabled()) {
527 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
532 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
535 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
538 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
541 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
544 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
547 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
550 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
553 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
558 frameRate(--targetFramerate);
562 frameRate(++targetFramerate);
565 effects.boom.trigger();
568 if (!midiEngine.isQwertyEnabled()) {
569 debugMode = !debugMode;
570 println("Debug output: " + (debugMode ? "ON" : "OFF"));
574 if (!midiEngine.isQwertyEnabled()) {
575 mappingMode = !mappingMode;
576 uiPatternA.setVisible(!mappingMode);
577 uiMapping.setVisible(mappingMode);
579 restoreToPattern = lx.getPattern();
580 lx.setPatterns(new LXPattern[] { mappingTool });
582 lx.setPatterns(patterns);
583 LXTransition pop = restoreToPattern.getTransition();
584 restoreToPattern.setTransition(null);
585 lx.goPattern(restoreToPattern);
586 restoreToPattern.setTransition(pop);
591 if (!midiEngine.isQwertyEnabled()) {
592 lx.engine.setThreaded(!lx.engine.isThreaded());
596 for (PandaDriver p : pandaBoards) {
601 if (!midiEngine.isQwertyEnabled()) {
602 diagnosticsOn = !diagnosticsOn;
606 if (!midiEngine.isQwertyEnabled()) {
607 simulationOn = !simulationOn;
611 if (!midiEngine.isQwertyEnabled()) {
619 * Top-level mouse event handling
622 void mousePressed() {
623 boolean debugged = false;
625 debugged = debugUI.mousePressed();
628 for (UIContext context : overlays) {
629 context.mousePressed(mouseX, mouseY);
636 void mouseDragged() {
637 boolean dragged = false;
638 for (UIContext context : overlays) {
639 dragged |= context.mouseDragged(mouseX, mouseY);
642 int dx = mouseX - mx;
643 int dy = mouseY - my;
647 eyeX = midX + eyeR*sin(eyeA);
648 eyeZ = midZ + eyeR*cos(eyeA);
653 void mouseReleased() {
654 for (UIContext context : overlays) {
655 context.mouseReleased(mouseX, mouseY);
659 void mouseWheel(int delta) {
660 boolean wheeled = false;
661 for (UIContext context : overlays) {
662 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
666 eyeR = constrain(eyeR - delta, -500, -80);
667 eyeX = midX + eyeR*sin(eyeA);
668 eyeZ = midZ + eyeR*cos(eyeA);