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.transform.*;
22 import glucose.transition.*;
23 import heronarts.lx.*;
24 import heronarts.lx.control.*;
25 import heronarts.lx.effect.*;
26 import heronarts.lx.modulator.*;
27 import heronarts.lx.pattern.*;
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 LXPattern restoreToPattern = null;
67 float[] hsb = new float[3];
69 // Handles to UI objects
71 UIPatternDeck uiPatternA;
72 UICrossfader uiCrossfader;
75 UIDebugText uiDebugText;
79 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
82 * Engine construction and initialization.
85 LXTransition _transition(GLucose glucose) {
86 return new DissolveTransition(glucose.lx).setDuration(1000);
89 LXPattern[] _leftPatterns(GLucose glucose) {
90 LXPattern[] patterns = patterns(glucose);
91 for (LXPattern p : patterns) {
92 p.setTransition(_transition(glucose));
97 LXPattern[] _rightPatterns(GLucose glucose) {
98 LXPattern[] patterns = _leftPatterns(glucose);
99 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
101 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
102 for (LXPattern p : patterns) {
103 rightPatterns[i++] = p;
105 return rightPatterns;
108 LXEffect[] _effectsArray(Effects effects) {
109 List<LXEffect> effectList = new ArrayList<LXEffect>();
110 for (Field f : effects.getClass().getDeclaredFields()) {
112 Object val = f.get(effects);
113 if (val instanceof LXEffect) {
114 effectList.add((LXEffect)val);
116 } catch (IllegalAccessException iax) {}
118 return effectList.toArray(new LXEffect[]{});
121 void logTime(String evt) {
123 println(evt + ": " + (now - lastMillis) + "ms");
128 startMillis = lastMillis = millis();
130 // Initialize the Processing graphics environment
131 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
132 frameRate(targetFramerate);
134 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
135 logTime("Created viewport");
137 // Create the GLucose engine to run the cubes
138 glucose = new GLucose(this, buildModel());
140 lx.enableKeyboardTempo();
141 logTime("Built GLucose engine");
144 LXEngine engine = lx.engine;
145 engine.setPatterns(patterns = _leftPatterns(glucose));
146 engine.addDeck(_rightPatterns(glucose));
147 logTime("Built patterns");
148 glucose.setTransitions(transitions(glucose));
149 logTime("Built transitions");
150 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
151 logTime("Built effects");
154 presetManager = new PresetManager();
155 logTime("Loaded presets");
158 midiEngine = new MidiEngine();
159 logTime("Setup MIDI devices");
161 // Build output driver
162 PandaMapping[] pandaMappings = buildPandaList();
163 pandaBoards = new PandaDriver[pandaMappings.length];
165 for (PandaMapping pm : pandaMappings) {
166 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
168 mappingTool = new MappingTool(glucose, pandaMappings);
169 logTime("Built PandaDriver");
172 debugUI = new DebugUI(pandaMappings);
173 overlays = new UIContext[] {
174 uiPatternA = new UIPatternDeck(lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
175 new UIBlendMode(4, 332, 140, 86),
176 new UIEffects(4, 422, 140, 144),
177 new UITempo(4, 570, 140, 50),
178 uiSpeed = new UISpeed(4, 624, 140, 50),
180 new UIPatternDeck(lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
181 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
182 new UIOutput(width-144, 494, 140, 106),
184 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
186 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
187 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
189 uiMapping.setVisible(false);
190 logTime("Built overlay UI");
193 logo = loadImage("data/logo.png");
196 midX = TRAILER_WIDTH/2.;
197 midY = glucose.model.yMax/2;
198 midZ = TRAILER_DEPTH/2.;
202 eyeX = midX + eyeR*sin(eyeA);
203 eyeZ = midZ + eyeR*cos(eyeA);
205 // Add mouse scrolling event support
206 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
207 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
208 mouseWheel(mwe.getWheelRotation());
211 println("Total setup: " + (millis() - startMillis) + "ms");
212 println("Hit the 'p' key to toggle Panda Board output");
216 * Core render loop and drawing functionality.
219 // Draws the simulation and the 2D UI overlay
222 color[] simulationColors;
224 simulationColors = sendColors = glucose.getColors();
225 String displayMode = uiCrossfader.getDisplayMode();
226 if (displayMode == "A") {
227 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
228 } else if (displayMode == "B") {
229 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
232 debugUI.maskColors(simulationColors);
233 debugUI.maskColors(sendColors);
237 drawSimulation(simulationColors);
243 // Gamma correction here. Apply a cubic to the brightness
244 // for better representation of dynamic range
245 for (int i = 0; i < sendColors.length; ++i) {
246 lx.RGBtoHSB(sendColors[i], hsb);
248 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
251 // TODO(mcslee): move into GLucose engine
252 for (PandaDriver p : pandaBoards) {
257 void drawSimulation(color[] simulationColors) {
268 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
273 vertex(TRAILER_WIDTH, 0, 0);
274 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
275 vertex(0, 0, TRAILER_DEPTH);
278 // Draw the logo on the front of platform
283 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
287 if (glucose.model.bassBox.exists) {
288 drawBassBox(glucose.model.bassBox, false);
290 for (Speaker speaker : glucose.model.speakers) {
291 drawSpeaker(speaker);
293 for (Cube c : glucose.model.cubes) {
300 for (Point p : glucose.model.points) {
301 stroke(simulationColors[p.index]);
302 vertex(p.x, p.y, p.z);
307 void drawBassBox(BassBox b, boolean hasSub) {
315 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
316 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
322 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);
325 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
326 float lastOffset = 0;
327 for (float offset : BoothFloor.STRIP_OFFSETS) {
328 translate(offset - lastOffset, 0, 0);
329 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
335 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
336 for (int j = 0; j < 2; ++j) {
338 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
339 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
340 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
343 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
348 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
349 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
350 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
351 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
356 void drawCube(Cube c) {
360 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);
363 void drawSpeaker(Speaker s) {
369 translate(s.x, s.y, s.z);
370 rotate(s.ry / 180. * PI, 0, -1, 0);
371 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
372 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
373 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
376 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
381 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);
384 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
387 rotate(rx / 180. * PI, -1, 0, 0);
388 rotate(ry / 180. * PI, 0, -1, 0);
389 rotate(rz / 180. * PI, 0, 0, -1);
390 for (int i = 0; i < 4; ++i) {
391 float wid = (i % 2 == 0) ? xd : zd;
397 vertex(wid - sw, yd);
398 vertex(wid - sw, sw);
404 vertex(wid - sw, yd);
405 vertex(wid - sw, yd - sw);
410 translate(wid, 0, 0);
411 rotate(HALF_PI, 0, -1, 0);
418 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
419 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
420 ((PGraphicsOpenGL)g).endGL();
424 for (UIContext context : overlays) {
429 // Always draw FPS meter
432 textAlign(LEFT, BASELINE);
433 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
442 * Top-level keyboard event handling
446 mappingTool.keyPressed(uiMapping);
457 if (!midiEngine.isQwertyEnabled()) {
458 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
463 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
466 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
469 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
472 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
475 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
478 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
481 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
484 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
489 frameRate(--targetFramerate);
493 frameRate(++targetFramerate);
496 effects.boom.trigger();
499 if (!midiEngine.isQwertyEnabled()) {
500 debugMode = !debugMode;
501 println("Debug output: " + (debugMode ? "ON" : "OFF"));
505 if (!midiEngine.isQwertyEnabled()) {
506 mappingMode = !mappingMode;
507 uiPatternA.setVisible(!mappingMode);
508 uiMapping.setVisible(mappingMode);
510 restoreToPattern = lx.getPattern();
511 lx.setPatterns(new LXPattern[] { mappingTool });
513 lx.setPatterns(patterns);
514 LXTransition pop = restoreToPattern.getTransition();
515 restoreToPattern.setTransition(null);
516 lx.goPattern(restoreToPattern);
517 restoreToPattern.setTransition(pop);
522 if (!midiEngine.isQwertyEnabled()) {
523 lx.engine.setThreaded(!lx.engine.isThreaded());
527 for (PandaDriver p : pandaBoards) {
532 if (!midiEngine.isQwertyEnabled()) {
533 simulationOn = !simulationOn;
537 if (!midiEngine.isQwertyEnabled()) {
545 * Top-level mouse event handling
548 void mousePressed() {
549 boolean debugged = false;
551 debugged = debugUI.mousePressed();
554 for (UIContext context : overlays) {
555 context.mousePressed(mouseX, mouseY);
562 void mouseDragged() {
563 boolean dragged = false;
564 for (UIContext context : overlays) {
565 dragged |= context.mouseDragged(mouseX, mouseY);
568 int dx = mouseX - mx;
569 int dy = mouseY - my;
573 eyeX = midX + eyeR*sin(eyeA);
574 eyeZ = midZ + eyeR*cos(eyeA);
579 void mouseReleased() {
580 for (UIContext context : overlays) {
581 context.mouseReleased(mouseX, mouseY);
585 void mouseWheel(int delta) {
586 boolean wheeled = false;
587 for (UIContext context : overlays) {
588 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
592 eyeR = constrain(eyeR - delta, -500, -80);
593 eyeX = midX + eyeR*sin(eyeA);
594 eyeZ = midZ + eyeR*cos(eyeA);