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.*;
25 import heronarts.lx.ui.*;
26 import heronarts.lx.ui.component.*;
27 import heronarts.lx.ui.control.*;
29 import ddf.minim.analysis.*;
30 import processing.opengl.*;
32 import java.lang.reflect.*;
34 final int VIEWPORT_WIDTH = 900;
35 final int VIEWPORT_HEIGHT = 700;
37 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
38 final float TRAILER_WIDTH = 240;
39 final float TRAILER_DEPTH = 97;
40 final float TRAILER_HEIGHT = 33;
42 int targetFramerate = 60;
43 int startMillis, lastMillis;
45 // Core engine variables
51 MappingTool mappingTool;
52 PandaDriver[] pandaBoards;
53 PresetManager presetManager;
54 MidiEngine midiEngine;
56 // Display configuration mode
57 boolean mappingMode = false;
58 boolean debugMode = false;
61 boolean simulationOn = true;
62 boolean diagnosticsOn = false;
63 LXPattern restoreToPattern = null;
65 float[] hsb = new float[3];
67 // Handles to UI objects
68 UIPatternDeck uiPatternA;
69 UICrossfader uiCrossfader;
72 UIDebugText uiDebugText;
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, model = 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
157 GrizzlyOutput[] grizzlies = buildGrizzlies();
158 for (LXOutput output : grizzlies) {
159 lx.addOutput(output);
161 } catch (Exception x) {
165 PandaMapping[] pandaMappings = buildPandaList();
166 pandaBoards = new PandaDriver[pandaMappings.length];
168 for (PandaMapping pm : pandaMappings) {
169 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
171 mappingTool = new MappingTool(glucose, pandaMappings);
172 logTime("Built PandaDriver");
175 debugUI = new DebugUI(pandaMappings);
176 UIContext[] contexts = new UIContext[] {
177 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
178 new UIBlendMode(4, 332, 140, 86),
179 new UIEffects(4, 422, 140, 144),
180 new UITempo(4, 570, 140, 50),
181 uiSpeed = new UISpeed(4, 624, 140, 50),
183 new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
184 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
185 new UIOutput(width-144, 494, 140, 106),
187 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
189 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
190 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
192 uiMapping.setVisible(false);
193 lx.ui.addLayer(new UICameraLayer(lx.ui).setCenter(TRAILER_WIDTH/2., glucose.model.yMax/2, TRAILER_DEPTH/2.).setRadius(290).addComponent(new UICubesLayer()));
194 for (UIContext context : contexts) {
195 lx.ui.addLayer(context);
197 logTime("Built overlay UI");
200 logo = loadImage("data/logo.png");
202 println("Total setup: " + (millis() - startMillis) + "ms");
203 println("Hit the 'p' key to toggle Panda Board output");
207 * Core render loop and drawing functionality.
210 long drawStart = System.nanoTime();
216 color[] sendColors = glucose.getColors();
218 debugUI.maskColors(sendColors);
221 long gammaStart = System.nanoTime();
222 // Gamma correction here. Apply a cubic to the brightness
223 // for better representation of dynamic range
224 for (int i = 0; i < sendColors.length; ++i) {
225 lx.RGBtoHSB(sendColors[i], hsb);
227 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
229 long gammaNanos = System.nanoTime() - gammaStart;
231 long sendStart = System.nanoTime();
232 for (PandaDriver p : pandaBoards) {
235 long sendNanos = System.nanoTime() - sendStart;
243 long drawNanos = System.nanoTime() - drawStart;
245 long simulationNanos = 0, uiNanos = 0;
247 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos);
251 class UICubesLayer extends UICameraComponent {
253 color[] simulationColors = glucose.getColors();
254 String displayMode = uiCrossfader.getDisplayMode();
255 if (displayMode == "A") {
256 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
257 } else if (displayMode == "B") {
258 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
261 debugUI.maskColors(simulationColors);
264 long simulationStart = System.nanoTime();
266 drawSimulation(simulationColors);
268 long simulationNanos = System.nanoTime() - simulationStart;
271 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
272 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
273 ((PGraphicsOpenGL)g).endGL();
278 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos, long sendNanos) {
279 float ws = 4 / 1000000.;
280 int thirtyfps = 1000000000 / 30;
281 int sixtyfps = 1000000000 / 60;
287 rect(x, y, thirtyfps * ws, h);
291 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos }) {
292 fill(lx.hsb(hv % 360, 100, 80));
293 rect(xp, y, val * ws, h-1);
299 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
303 float tw = thirtyfps * ws;
309 for (long val : new long[] {
310 lx.engine.timer.deckNanos,
311 lx.engine.timer.copyNanos,
312 lx.engine.timer.fxNanos}) {
313 float amt = val / (float) lx.timer.drawNanos;
314 fill(lx.hsb(hv % 360, 100, 80));
315 rect(xp, y, amt * tw, h-1);
323 for (long val : new long[] {
324 lx.engine.getDeck(0).timer.runNanos,
325 lx.engine.getDeck(1).timer.runNanos,
326 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
327 float amt = val / (float) lx.timer.drawNanos;
328 fill(lx.hsb(hv % 360, 100, 80));
329 rect(xp, y, amt * tw, h-1);
335 void drawSimulation(color[] simulationColors) {
340 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
345 vertex(TRAILER_WIDTH, 0, 0);
346 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
347 vertex(0, 0, TRAILER_DEPTH);
350 // Draw the logo on the front of platform
355 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
359 if (glucose.model.bassBox.exists) {
360 drawBassBox(glucose.model.bassBox, false);
362 for (Speaker speaker : glucose.model.speakers) {
363 drawSpeaker(speaker);
365 for (Cube c : glucose.model.cubes) {
372 for (LXPoint p : glucose.model.points) {
373 stroke(simulationColors[p.index]);
374 vertex(p.x, p.y, p.z);
379 void drawBassBox(BassBox b, boolean hasSub) {
387 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
388 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
394 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);
397 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
398 float lastOffset = 0;
399 for (float offset : BoothFloor.STRIP_OFFSETS) {
400 translate(offset - lastOffset, 0, 0);
401 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
407 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
408 for (int j = 0; j < 2; ++j) {
410 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
411 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
412 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
415 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
420 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
421 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
422 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
423 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
428 void drawCube(Cube c) {
432 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);
435 void drawSpeaker(Speaker s) {
441 translate(s.x, s.y, s.z);
442 rotate(s.ry / 180. * PI, 0, -1, 0);
443 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
444 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
445 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
448 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
453 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);
456 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
459 rotate(rx / 180. * PI, -1, 0, 0);
460 rotate(ry / 180. * PI, 0, -1, 0);
461 rotate(rz / 180. * PI, 0, 0, -1);
462 for (int i = 0; i < 4; ++i) {
463 float wid = (i % 2 == 0) ? xd : zd;
469 vertex(wid - sw, yd);
470 vertex(wid - sw, sw);
476 vertex(wid - sw, yd);
477 vertex(wid - sw, yd - sw);
482 translate(wid, 0, 0);
483 rotate(HALF_PI, 0, -1, 0);
489 // Always draw FPS meter
492 textAlign(LEFT, BASELINE);
493 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
498 * Top-level keyboard event handling
502 mappingTool.keyPressed(uiMapping);
513 if (!midiEngine.isQwertyEnabled()) {
514 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
519 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
522 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
525 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
528 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
531 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
534 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
537 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
540 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
545 frameRate(--targetFramerate);
549 frameRate(++targetFramerate);
552 effects.boom.trigger();
555 if (!midiEngine.isQwertyEnabled()) {
556 debugMode = !debugMode;
557 println("Debug output: " + (debugMode ? "ON" : "OFF"));
561 if (!midiEngine.isQwertyEnabled()) {
562 mappingMode = !mappingMode;
563 uiPatternA.setVisible(!mappingMode);
564 uiMapping.setVisible(mappingMode);
566 restoreToPattern = lx.getPattern();
567 lx.setPatterns(new LXPattern[] { mappingTool });
569 lx.setPatterns(patterns);
570 LXTransition pop = restoreToPattern.getTransition();
571 restoreToPattern.setTransition(null);
572 lx.goPattern(restoreToPattern);
573 restoreToPattern.setTransition(pop);
578 if (!midiEngine.isQwertyEnabled()) {
579 lx.engine.setThreaded(!lx.engine.isThreaded());
583 for (PandaDriver p : pandaBoards) {
588 if (!midiEngine.isQwertyEnabled()) {
589 diagnosticsOn = !diagnosticsOn;
593 if (!midiEngine.isQwertyEnabled()) {
594 simulationOn = !simulationOn;
598 if (!midiEngine.isQwertyEnabled()) {
606 * Top-level mouse event handling
608 void mousePressed() {
610 debugUI.mousePressed();