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.*;
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
49 MappingTool mappingTool;
50 PandaDriver[] pandaBoards;
51 final List<MidiListener> midiListeners = new ArrayList<MidiListener>();
53 // Display configuration mode
54 boolean mappingMode = false;
55 boolean debugMode = false;
58 LXPattern restoreToPattern = null;
60 // Handles to UI objects
62 UIPatternDeck uiPatternA;
63 UICrossfader uiCrossfader;
65 UIDebugText uiDebugText;
68 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
71 * Engine construction and initialization.
73 LXPattern[] _patterns(GLucose glucose) {
74 LXPattern[] patterns = patterns(glucose);
75 for (LXPattern p : patterns) {
76 p.setTransition(new DissolveTransition(glucose.lx).setDuration(1000));
81 void logTime(String evt) {
83 println(evt + ": " + (now - lastMillis) + "ms");
88 startMillis = lastMillis = millis();
90 // Initialize the Processing graphics environment
91 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
92 frameRate(targetFramerate);
94 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
95 logTime("Created viewport");
97 // Create the GLucose engine to run the cubes
98 glucose = new GLucose(this, buildModel());
100 lx.enableKeyboardTempo();
101 logTime("Built GLucose engine");
104 Engine engine = lx.engine;
105 engine.setPatterns(patterns = _patterns(glucose));
106 engine.addDeck(_patterns(glucose));
107 logTime("Built patterns");
108 glucose.setTransitions(transitions(glucose));
109 logTime("Built transitions");
110 glucose.lx.addEffects(effects(glucose));
111 logTime("Built effects");
113 // Build output driver
114 PandaMapping[] pandaMappings = buildPandaList();
115 pandaBoards = new PandaDriver[pandaMappings.length];
117 for (PandaMapping pm : pandaMappings) {
118 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
120 mappingTool = new MappingTool(glucose, pandaMappings);
121 logTime("Built PandaDriver");
124 debugUI = new DebugUI(pandaMappings);
125 overlays = new UIContext[] {
126 uiPatternA = new UIPatternDeck(lx.engine.getDeck(0), "PATTERN A", 4, 4, 140, 344),
127 uiCrossfader = new UICrossfader(4, 352, 140, 212),
128 new UIOutput(4, 568, 140, 106),
130 new UIPatternDeck(lx.engine.getDeck(1), "PATTERN B", width-144, 4, 140, 344),
131 new UIEffects(width-144, 352, 140, 144),
132 new UITempo(width-144, 498, 140, 50),
133 new UISpeed(width-144, 552, 140, 50),
135 uiDebugText = new UIDebugText(4, height-64, width-8, 44),
136 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 344),
138 uiMapping.setVisible(false);
139 logTime("Built overlay UI");
142 midiListeners.add(new MidiListener().setEnabled(true));
143 for (MidiInputDevice d : RWMidi.getInputDevices()) {
144 midiListeners.add(new MidiListener(d));
146 SCMidiDevices.initializeStandardDevices(glucose);
147 logTime("Setup MIDI devices");
150 midX = TRAILER_WIDTH/2.;
151 midY = glucose.model.yMax/2;
152 midZ = TRAILER_DEPTH/2.;
156 eyeX = midX + eyeR*sin(eyeA);
157 eyeZ = midZ + eyeR*cos(eyeA);
158 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
159 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
160 mouseWheel(mwe.getWheelRotation());
163 println("Total setup: " + (millis() - startMillis) + "ms");
164 println("Hit the 'p' key to toggle Panda Board output");
167 public class MidiListener {
168 private boolean enabled = false;
169 private final String name;
171 MidiListener(MidiInputDevice d) {
177 registerKeyEvent(this);
181 public String getName() {
185 public void keyEvent(KeyEvent e) {
186 if (e.getID() == KeyEvent.KEY_PRESSED) {
187 switch (e.getKeyChar()) {
189 noteOnReceived(new Note(60, 127));
192 } else if (e.getID() == KeyEvent.KEY_RELEASED) {
193 switch (e.getKeyChar()) {
195 noteOffReceived(new Note(60, 0));
201 public MidiListener setEnabled(boolean enabled) {
202 if (enabled != this.enabled) {
203 this.enabled = enabled;
204 // notify midi UI to update
209 void controllerChangeReceived(rwmidi.Controller cc) {
213 println("CC: " + cc.toString());
216 void noteOnReceived(Note note) {
220 println("Note On: " + note.toString());
223 void noteOffReceived(Note note) {
227 println("Note Off: " + note.toString());
233 * Core render loop and drawing functionality.
236 // Draws the simulation and the 2D UI overlay
238 color[] colors = glucose.getColors();
240 String displayMode = uiCrossfader.getDisplayMode();
241 if (displayMode == "A") {
242 colors = lx.engine.getDeck(0).getColors();
243 } else if (displayMode == "B") {
244 colors = lx.engine.getDeck(1).getColors();
247 debugUI.maskColors(colors);
260 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
265 vertex(TRAILER_WIDTH, 0, 0);
266 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
267 vertex(0, 0, TRAILER_DEPTH);
271 // drawBassBox(glucose.model.bassBox);
272 // for (Speaker s : glucose.model.speakers) {
275 for (Cube c : glucose.model.cubes) {
282 // TODO(mcslee): restore when bassBox/speakers are right again
283 // for (Point p : glucose.model.points) {
284 for (Cube cube : glucose.model.cubes) {
285 for (Point p : cube.points) {
286 stroke(colors[p.index]);
287 vertex(p.fx, p.fy, p.fz);
295 // Send output colors
296 color[] sendColors = glucose.getColors();
298 debugUI.maskColors(colors);
301 // Gamma correction here. Apply a cubic to the brightness
302 // for better representation of dynamic range
303 for (int i = 0; i < colors.length; ++i) {
304 float b = brightness(colors[i]) / 100.f;
307 saturation(colors[i]),
312 // TODO(mcslee): move into GLucose engine
313 for (PandaDriver p : pandaBoards) {
318 void drawBassBox(BassBox b) {
324 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
325 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
330 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);
333 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
334 float lastOffset = 0;
335 for (float offset : BoothFloor.STRIP_OFFSETS) {
336 translate(offset - lastOffset, 0, 0);
337 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
343 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
344 for (int j = 0; j < 2; ++j) {
346 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
347 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
348 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
351 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
356 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
357 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
358 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
359 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
364 void drawCube(Cube c) {
368 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);
371 void drawSpeaker(Speaker s) {
377 translate(s.x, s.y, s.z);
378 rotate(s.ry / 180. * PI, 0, -1, 0);
379 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
380 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
381 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
384 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
389 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);
392 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
395 rotate(rx / 180. * PI, -1, 0, 0);
396 rotate(ry / 180. * PI, 0, -1, 0);
397 rotate(rz / 180. * PI, 0, 0, -1);
398 for (int i = 0; i < 4; ++i) {
399 float wid = (i % 2 == 0) ? xd : zd;
405 vertex(wid - sw, yd);
406 vertex(wid - sw, sw);
412 vertex(wid - sw, yd);
413 vertex(wid - sw, yd - sw);
418 translate(wid, 0, 0);
419 rotate(HALF_PI, 0, -1, 0);
426 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
427 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
428 ((PGraphicsOpenGL)g).endGL();
432 for (UIContext context : overlays) {
437 // Always draw FPS meter
440 textAlign(LEFT, BASELINE);
441 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
449 * Top-level keyboard event handling
453 mappingTool.keyPressed(uiMapping);
458 frameRate(--targetFramerate);
462 frameRate(++targetFramerate);
465 debugMode = !debugMode;
466 println("Debug output: " + (debugMode ? "ON" : "OFF"));
469 mappingMode = !mappingMode;
470 uiPatternA.setVisible(!mappingMode);
471 uiMapping.setVisible(mappingMode);
473 restoreToPattern = lx.getPattern();
474 lx.setPatterns(new LXPattern[] { mappingTool });
476 lx.setPatterns(patterns);
477 LXTransition pop = restoreToPattern.getTransition();
478 restoreToPattern.setTransition(null);
479 lx.goPattern(restoreToPattern);
480 restoreToPattern.setTransition(pop);
484 for (PandaDriver p : pandaBoards) {
495 * Top-level mouse event handling
498 void mousePressed() {
499 boolean debugged = false;
501 debugged = debugUI.mousePressed();
504 for (UIContext context : overlays) {
505 context.mousePressed(mouseX, mouseY);
512 void mouseDragged() {
513 boolean dragged = false;
514 for (UIContext context : overlays) {
515 dragged |= context.mouseDragged(mouseX, mouseY);
518 int dx = mouseX - mx;
519 int dy = mouseY - my;
523 eyeX = midX + eyeR*sin(eyeA);
524 eyeZ = midZ + eyeR*cos(eyeA);
529 void mouseReleased() {
530 for (UIContext context : overlays) {
531 context.mouseReleased(mouseX, mouseY);
535 void mouseWheel(int delta) {
536 boolean wheeled = false;
537 for (UIContext context : overlays) {
538 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
542 eyeR = constrain(eyeR - delta, -500, -80);
543 eyeX = midX + eyeR*sin(eyeA);
544 eyeZ = midZ + eyeR*cos(eyeA);