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.*;
30 import javax.media.opengl.*;
32 final int VIEWPORT_WIDTH = 900;
33 final int VIEWPORT_HEIGHT = 700;
35 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
36 final float TRAILER_WIDTH = 240;
37 final float TRAILER_DEPTH = 97;
38 final float TRAILER_HEIGHT = 33;
40 int targetFramerate = 60;
41 int startMillis, lastMillis;
43 // Core engine variables
48 MappingTool mappingTool;
49 PandaDriver[] pandaBoards;
50 PresetManager presetManager;
51 MidiEngine midiEngine;
53 // Display configuration mode
54 boolean mappingMode = false;
55 boolean debugMode = false;
58 boolean simulationOn = true;
59 boolean diagnosticsOn = false;
60 LXPattern restoreToPattern = null;
62 float[] hsb = new float[3];
64 // Handles to UI objects
66 UIPatternDeck uiPatternA;
67 UICrossfader uiCrossfader;
70 UIDebugText uiDebugText;
74 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
77 * Engine construction and initialization.
80 LXTransition _transition(GLucose glucose) {
81 return new DissolveTransition(glucose.lx).setDuration(1000);
84 LXPattern[] _leftPatterns(GLucose glucose) {
85 LXPattern[] patterns = patterns(glucose);
86 for (LXPattern p : patterns) {
87 p.setTransition(_transition(glucose));
92 LXPattern[] _rightPatterns(GLucose glucose) {
93 LXPattern[] patterns = _leftPatterns(glucose);
94 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
96 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
97 for (LXPattern p : patterns) {
98 rightPatterns[i++] = p;
100 return rightPatterns;
103 LXEffect[] _effectsArray(Effects effects) {
104 List<LXEffect> effectList = new ArrayList<LXEffect>();
105 for (Field f : effects.getClass().getDeclaredFields()) {
107 Object val = f.get(effects);
108 if (val instanceof LXEffect) {
109 effectList.add((LXEffect)val);
111 } catch (IllegalAccessException iax) {}
113 return effectList.toArray(new LXEffect[]{});
116 void logTime(String evt) {
118 println(evt + ": " + (now - lastMillis) + "ms");
123 startMillis = lastMillis = millis();
125 // Initialize the Processing graphics environment
126 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
127 frameRate(targetFramerate);
129 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
130 logTime("Created viewport");
132 // Create the GLucose engine to run the cubes
133 glucose = new GLucose(this, buildModel());
135 lx.enableKeyboardTempo();
136 logTime("Built GLucose engine");
139 LXEngine engine = lx.engine;
140 engine.setPatterns(patterns = _leftPatterns(glucose));
141 engine.addDeck(_rightPatterns(glucose));
142 logTime("Built patterns");
143 glucose.setTransitions(transitions(glucose));
144 logTime("Built transitions");
145 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
146 logTime("Built effects");
149 presetManager = new PresetManager();
150 logTime("Loaded presets");
153 midiEngine = new MidiEngine();
154 logTime("Setup MIDI devices");
156 // Build output driver
157 PandaMapping[] pandaMappings = buildPandaList();
158 pandaBoards = new PandaDriver[pandaMappings.length];
160 for (PandaMapping pm : pandaMappings) {
161 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
163 mappingTool = new MappingTool(glucose, pandaMappings);
164 logTime("Built PandaDriver");
167 debugUI = new DebugUI(pandaMappings);
168 overlays = new UIContext[] {
169 uiPatternA = new UIPatternDeck(lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
170 new UIBlendMode(4, 332, 140, 86),
171 new UIEffects(4, 422, 140, 144),
172 new UITempo(4, 570, 140, 50),
173 uiSpeed = new UISpeed(4, 624, 140, 50),
175 new UIPatternDeck(lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
176 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
177 new UIOutput(width-144, 494, 140, 106),
179 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
181 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
182 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
184 uiMapping.setVisible(false);
185 logTime("Built overlay UI");
188 logo = loadImage("data/logo.png");
191 midX = TRAILER_WIDTH/2.;
192 midY = glucose.model.yMax/2;
193 midZ = TRAILER_DEPTH/2.;
197 eyeX = midX + eyeR*sin(eyeA);
198 eyeZ = midZ + eyeR*cos(eyeA);
200 // Add mouse scrolling event support
201 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
202 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
203 mouseWheel(mwe.getWheelRotation());
206 println("Total setup: " + (millis() - startMillis) + "ms");
207 println("Hit the 'p' key to toggle Panda Board output");
211 * Core render loop and drawing functionality.
214 long drawStart = System.nanoTime();
216 // Draws the simulation and the 2D UI overlay
219 color[] simulationColors;
221 simulationColors = sendColors = glucose.getColors();
222 String displayMode = uiCrossfader.getDisplayMode();
223 if (displayMode == "A") {
224 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
225 } else if (displayMode == "B") {
226 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
229 debugUI.maskColors(simulationColors);
230 debugUI.maskColors(sendColors);
233 long simulationStart = System.nanoTime();
235 drawSimulation(simulationColors);
237 long simulationNanos = System.nanoTime() - simulationStart;
240 long uiStart = System.nanoTime();
242 long uiNanos = System.nanoTime() - uiStart;
244 long gammaStart = System.nanoTime();
245 // Gamma correction here. Apply a cubic to the brightness
246 // for better representation of dynamic range
247 for (int i = 0; i < sendColors.length; ++i) {
248 lx.RGBtoHSB(sendColors[i], hsb);
250 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
252 long gammaNanos = System.nanoTime() - gammaStart;
254 long sendStart = System.nanoTime();
255 for (PandaDriver p : pandaBoards) {
258 long sendNanos = System.nanoTime() - sendStart;
260 long drawNanos = System.nanoTime() - drawStart;
263 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos);
267 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos, long sendNanos) {
268 float ws = 4 / 1000000.;
269 int thirtyfps = 1000000000 / 30;
270 int sixtyfps = 1000000000 / 60;
276 rect(x, y, thirtyfps * ws, h);
280 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos }) {
281 fill(lx.hsb(hv % 360, 100, 80));
282 rect(xp, y, val * ws, h-1);
288 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
292 float tw = thirtyfps * ws;
298 for (long val : new long[] {
299 lx.engine.timer.deckNanos,
300 lx.engine.timer.copyNanos,
301 lx.engine.timer.fxNanos}) {
302 float amt = val / (float) lx.timer.drawNanos;
303 fill(lx.hsb(hv % 360, 100, 80));
304 rect(xp, y, amt * tw, h-1);
312 for (long val : new long[] {
313 lx.engine.getDeck(0).timer.runNanos,
314 lx.engine.getDeck(1).timer.runNanos,
315 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
316 float amt = val / (float) lx.timer.drawNanos;
317 fill(lx.hsb(hv % 360, 100, 80));
318 rect(xp, y, amt * tw, h-1);
324 void drawSimulation(color[] simulationColors) {
335 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
340 vertex(TRAILER_WIDTH, 0, 0);
341 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
342 vertex(0, 0, TRAILER_DEPTH);
345 // Draw the logo on the front of platform
350 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
354 if (glucose.model.bassBox.exists) {
355 drawBassBox(glucose.model.bassBox, false);
357 for (Speaker speaker : glucose.model.speakers) {
358 drawSpeaker(speaker);
360 for (Cube c : glucose.model.cubes) {
367 for (LXPoint p : glucose.model.points) {
368 stroke(simulationColors[p.index]);
369 vertex(p.x, p.y, p.z);
374 void drawBassBox(BassBox b, boolean hasSub) {
382 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
383 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
389 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);
392 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
393 float lastOffset = 0;
394 for (float offset : BoothFloor.STRIP_OFFSETS) {
395 translate(offset - lastOffset, 0, 0);
396 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
402 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
403 for (int j = 0; j < 2; ++j) {
405 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
406 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
407 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
410 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
415 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
416 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
417 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
418 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
423 void drawCube(Cube c) {
427 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);
430 void drawSpeaker(Speaker s) {
436 translate(s.x, s.y, s.z);
437 rotate(s.ry / 180. * PI, 0, -1, 0);
438 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
439 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
440 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
443 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
448 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);
451 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
454 rotate(rx / 180. * PI, -1, 0, 0);
455 rotate(ry / 180. * PI, 0, -1, 0);
456 rotate(rz / 180. * PI, 0, 0, -1);
457 for (int i = 0; i < 4; ++i) {
458 float wid = (i % 2 == 0) ? xd : zd;
464 vertex(wid - sw, yd);
465 vertex(wid - sw, sw);
471 vertex(wid - sw, yd);
472 vertex(wid - sw, yd - sw);
477 translate(wid, 0, 0);
478 rotate(HALF_PI, 0, -1, 0);
487 PGraphicsOpenGL gl = (PGraphicsOpenGL) g;
489 //gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
490 //((PGraphicsOpenGL)g).endGL();
494 for (UIContext context : overlays) {
499 // Always draw FPS meter
502 textAlign(LEFT, BASELINE);
503 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
512 * Top-level keyboard event handling
516 mappingTool.keyPressed(uiMapping);
527 if (!midiEngine.isQwertyEnabled()) {
528 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
533 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
536 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
539 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
542 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
545 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
548 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
551 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
554 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
559 frameRate(--targetFramerate);
563 frameRate(++targetFramerate);
566 effects.boom.trigger();
569 if (!midiEngine.isQwertyEnabled()) {
570 debugMode = !debugMode;
571 println("Debug output: " + (debugMode ? "ON" : "OFF"));
575 if (!midiEngine.isQwertyEnabled()) {
576 mappingMode = !mappingMode;
577 uiPatternA.setVisible(!mappingMode);
578 uiMapping.setVisible(mappingMode);
580 restoreToPattern = lx.getPattern();
581 lx.setPatterns(new LXPattern[] { mappingTool });
583 lx.setPatterns(patterns);
584 LXTransition pop = restoreToPattern.getTransition();
585 restoreToPattern.setTransition(null);
586 lx.goPattern(restoreToPattern);
587 restoreToPattern.setTransition(pop);
592 if (!midiEngine.isQwertyEnabled()) {
593 lx.engine.setThreaded(!lx.engine.isThreaded());
597 for (PandaDriver p : pandaBoards) {
602 if (!midiEngine.isQwertyEnabled()) {
603 diagnosticsOn = !diagnosticsOn;
607 if (!midiEngine.isQwertyEnabled()) {
608 simulationOn = !simulationOn;
612 if (!midiEngine.isQwertyEnabled()) {
620 * Top-level mouse event handling
623 void mousePressed() {
624 boolean debugged = false;
626 debugged = debugUI.mousePressed();
629 for (UIContext context : overlays) {
630 context.mousePressed(mouseX, mouseY);
637 void mouseDragged() {
638 boolean dragged = false;
639 for (UIContext context : overlays) {
640 dragged |= context.mouseDragged(mouseX, mouseY);
643 int dx = mouseX - mx;
644 int dy = mouseY - my;
648 eyeX = midX + eyeR*sin(eyeA);
649 eyeZ = midZ + eyeR*cos(eyeA);
654 void mouseReleased() {
655 for (UIContext context : overlays) {
656 context.mouseReleased(mouseX, mouseY);
660 void mouseWheel(int delta) {
661 boolean wheeled = false;
662 for (UIContext context : overlays) {
663 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
667 eyeR = constrain(eyeR - delta, -500, -80);
668 eyeX = midX + eyeR*sin(eyeA);
669 eyeZ = midZ + eyeR*cos(eyeA);