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 = 7;
44 final int NumBackTowers = 18;
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 LXPattern restoreToPattern = null;
66 float[] hsb = new float[3];
68 // Handles to UI objects
70 UIPatternDeck uiPatternA;
71 UICrossfader uiCrossfader;
74 UIDebugText uiDebugText;
78 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
81 * Engine construction and initialization.
84 LXTransition _transition(GLucose glucose) {
85 return new DissolveTransition(glucose.lx).setDuration(1000);
88 LXPattern[] _leftPatterns(GLucose glucose) {
89 LXPattern[] patterns = patterns(glucose);
90 for (LXPattern p : patterns) {
91 p.setTransition(_transition(glucose));
96 LXPattern[] _rightPatterns(GLucose glucose) {
97 LXPattern[] patterns = _leftPatterns(glucose);
98 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
100 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
101 for (LXPattern p : patterns) {
102 rightPatterns[i++] = p;
104 return rightPatterns;
107 LXEffect[] _effectsArray(Effects effects) {
108 List<LXEffect> effectList = new ArrayList<LXEffect>();
109 for (Field f : effects.getClass().getDeclaredFields()) {
111 Object val = f.get(effects);
112 if (val instanceof LXEffect) {
113 effectList.add((LXEffect)val);
115 } catch (IllegalAccessException iax) {}
117 return effectList.toArray(new LXEffect[]{});
120 void logTime(String evt) {
122 println(evt + ": " + (now - lastMillis) + "ms");
127 startMillis = lastMillis = millis();
129 // Initialize the Processing graphics environment
130 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
131 frameRate(targetFramerate);
133 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
134 logTime("Created viewport");
136 // Create the GLucose engine to run the cubes
137 glucose = new GLucose(this, buildModel());
139 lx.enableKeyboardTempo();
140 logTime("Built GLucose engine");
143 LXEngine engine = lx.engine;
144 engine.setPatterns(patterns = _leftPatterns(glucose));
145 engine.addDeck(_rightPatterns(glucose));
146 logTime("Built patterns");
147 glucose.setTransitions(transitions(glucose));
148 logTime("Built transitions");
149 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
150 logTime("Built effects");
153 presetManager = new PresetManager();
154 logTime("Loaded presets");
157 midiEngine = new MidiEngine();
158 logTime("Setup MIDI devices");
160 // Build output driver
161 PandaMapping[] pandaMappings = buildPandaList();
162 pandaBoards = new PandaDriver[pandaMappings.length];
164 for (PandaMapping pm : pandaMappings) {
165 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
167 mappingTool = new MappingTool(glucose, pandaMappings);
168 logTime("Built PandaDriver");
171 debugUI = new DebugUI(pandaMappings);
172 overlays = new UIContext[] {
173 uiPatternA = new UIPatternDeck(lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
174 new UIBlendMode(4, 332, 140, 86),
175 new UIEffects(4, 422, 140, 144),
176 new UITempo(4, 570, 140, 50),
177 uiSpeed = new UISpeed(4, 624, 140, 50),
179 new UIPatternDeck(lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
180 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
181 new UIOutput(width-144, 494, 140, 106),
183 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
185 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
186 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
188 uiMapping.setVisible(false);
189 logTime("Built overlay UI");
192 logo = loadImage("data/logo.png");
195 midX = TRAILER_WIDTH/2.;
196 midY = glucose.model.yMax/2;
197 midZ = TRAILER_DEPTH/2.;
201 eyeX = midX + eyeR*sin(eyeA);
202 eyeZ = midZ + eyeR*cos(eyeA);
204 // Add mouse scrolling event support
205 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
206 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
207 mouseWheel(mwe.getWheelRotation());
210 println("Total setup: " + (millis() - startMillis) + "ms");
211 println("Hit the 'p' key to toggle Panda Board output");
215 * Core render loop and drawing functionality.
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);
245 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
250 vertex(TRAILER_WIDTH, 0, 0);
251 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
252 vertex(0, 0, TRAILER_DEPTH);
255 // Draw the logo on the front of platform
260 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
264 if (glucose.model.bassBox.exists) {
265 drawBassBox(glucose.model.bassBox, false);
267 for (Speaker speaker : glucose.model.speakers) {
268 drawSpeaker(speaker);
270 for (Cube c : glucose.model.cubes) {
277 for (Point p : glucose.model.points) {
278 stroke(simulationColors[p.index]);
279 vertex(p.x, p.y, p.z);
286 // Gamma correction here. Apply a cubic to the brightness
287 // for better representation of dynamic range
288 for (int i = 0; i < sendColors.length; ++i) {
289 lx.RGBtoHSB(sendColors[i], hsb);
291 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
294 // TODO(mcslee): move into GLucose engine
295 for (PandaDriver p : pandaBoards) {
300 void drawBassBox(BassBox b, boolean hasSub) {
308 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
309 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
315 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);
318 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
319 float lastOffset = 0;
320 for (float offset : BoothFloor.STRIP_OFFSETS) {
321 translate(offset - lastOffset, 0, 0);
322 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
328 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
329 for (int j = 0; j < 2; ++j) {
331 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
332 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
333 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
336 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
341 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
342 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
343 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
344 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
349 void drawCube(Cube c) {
353 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);
356 void drawSpeaker(Speaker s) {
362 translate(s.x, s.y, s.z);
363 rotate(s.ry / 180. * PI, 0, -1, 0);
364 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
365 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
366 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
369 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
374 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);
377 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
380 rotate(rx / 180. * PI, -1, 0, 0);
381 rotate(ry / 180. * PI, 0, -1, 0);
382 rotate(rz / 180. * PI, 0, 0, -1);
383 for (int i = 0; i < 4; ++i) {
384 float wid = (i % 2 == 0) ? xd : zd;
390 vertex(wid - sw, yd);
391 vertex(wid - sw, sw);
397 vertex(wid - sw, yd);
398 vertex(wid - sw, yd - sw);
403 translate(wid, 0, 0);
404 rotate(HALF_PI, 0, -1, 0);
411 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
412 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
413 ((PGraphicsOpenGL)g).endGL();
417 for (UIContext context : overlays) {
422 // Always draw FPS meter
425 textAlign(LEFT, BASELINE);
426 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
435 * Top-level keyboard event handling
439 mappingTool.keyPressed(uiMapping);
450 if (!midiEngine.isQwertyEnabled()) {
451 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
456 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
459 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
462 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
465 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
468 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
471 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
474 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
477 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
482 frameRate(--targetFramerate);
486 frameRate(++targetFramerate);
489 effects.boom.trigger();
492 if (!midiEngine.isQwertyEnabled()) {
493 debugMode = !debugMode;
494 println("Debug output: " + (debugMode ? "ON" : "OFF"));
498 if (!midiEngine.isQwertyEnabled()) {
499 mappingMode = !mappingMode;
500 uiPatternA.setVisible(!mappingMode);
501 uiMapping.setVisible(mappingMode);
503 restoreToPattern = lx.getPattern();
504 lx.setPatterns(new LXPattern[] { mappingTool });
506 lx.setPatterns(patterns);
507 LXTransition pop = restoreToPattern.getTransition();
508 restoreToPattern.setTransition(null);
509 lx.goPattern(restoreToPattern);
510 restoreToPattern.setTransition(pop);
515 if (!midiEngine.isQwertyEnabled()) {
516 lx.engine.setThreaded(!lx.engine.isThreaded());
520 for (PandaDriver p : pandaBoards) {
525 if (!midiEngine.isQwertyEnabled()) {
533 * Top-level mouse event handling
536 void mousePressed() {
537 boolean debugged = false;
539 debugged = debugUI.mousePressed();
542 for (UIContext context : overlays) {
543 context.mousePressed(mouseX, mouseY);
550 void mouseDragged() {
551 boolean dragged = false;
552 for (UIContext context : overlays) {
553 dragged |= context.mouseDragged(mouseX, mouseY);
556 int dx = mouseX - mx;
557 int dy = mouseY - my;
561 eyeX = midX + eyeR*sin(eyeA);
562 eyeZ = midZ + eyeR*cos(eyeA);
567 void mouseReleased() {
568 for (UIContext context : overlays) {
569 context.mouseReleased(mouseX, mouseY);
573 void mouseWheel(int delta) {
574 boolean wheeled = false;
575 for (UIContext context : overlays) {
576 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
580 eyeR = constrain(eyeR - delta, -500, -80);
581 eyeX = midX + eyeR*sin(eyeA);
582 eyeZ = midZ + eyeR*cos(eyeA);