Add speed slider, new HeronLX with deltaMs as double
[SugarCubes.git] / _Internals.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
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.
14 */
15
16 import glucose.*;
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.*;
29 import ddf.minim.*;
30 import ddf.minim.analysis.*;
31 import processing.opengl.*;
32 import rwmidi.*;
33
34 final int VIEWPORT_WIDTH = 900;
35 final int VIEWPORT_HEIGHT = 700;
36
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;
41
42 int targetFramerate = 60;
43 int startMillis, lastMillis;
44
45 // Core engine variables
46 GLucose glucose;
47 HeronLX lx;
48 LXPattern[] patterns;
49 MappingTool mappingTool;
50 PandaDriver[] pandaBoards;
51 final List<MidiListener> midiListeners = new ArrayList<MidiListener>();
52
53 // Display configuration mode
54 boolean mappingMode = false;
55 boolean debugMode = false;
56 DebugUI debugUI;
57 boolean uiOn = true;
58 LXPattern restoreToPattern = null;
59
60 // Handles to UI objects
61 UIContext[] overlays;
62 UIPatternDeck uiPatternA;
63 UICrossfader uiCrossfader;
64 UIMapping uiMapping;
65 UIDebugText uiDebugText;
66
67 // Camera variables
68 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
69
70 /**
71 * Engine construction and initialization.
72 */
73 LXPattern[] _patterns(GLucose glucose) {
74 LXPattern[] patterns = patterns(glucose);
75 for (LXPattern p : patterns) {
76 p.setTransition(new DissolveTransition(glucose.lx).setDuration(1000));
77 }
78 return patterns;
79 }
80
81 void logTime(String evt) {
82 int now = millis();
83 println(evt + ": " + (now - lastMillis) + "ms");
84 lastMillis = now;
85 }
86
87 void setup() {
88 startMillis = lastMillis = millis();
89
90 // Initialize the Processing graphics environment
91 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
92 frameRate(targetFramerate);
93 noSmooth();
94 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
95 logTime("Created viewport");
96
97 // Create the GLucose engine to run the cubes
98 glucose = new GLucose(this, buildModel());
99 lx = glucose.lx;
100 lx.enableKeyboardTempo();
101 logTime("Built GLucose engine");
102
103 // Set the patterns
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");
112
113 // Build output driver
114 PandaMapping[] pandaMappings = buildPandaList();
115 pandaBoards = new PandaDriver[pandaMappings.length];
116 int pbi = 0;
117 for (PandaMapping pm : pandaMappings) {
118 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
119 }
120 mappingTool = new MappingTool(glucose, pandaMappings);
121 logTime("Built PandaDriver");
122
123 // Build overlay UI
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),
129
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),
134
135 uiDebugText = new UIDebugText(4, height-64, width-8, 44),
136 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 344),
137 };
138 uiMapping.setVisible(false);
139 logTime("Built overlay UI");
140
141 // MIDI devices
142 midiListeners.add(new MidiListener().setEnabled(true));
143 for (MidiInputDevice d : RWMidi.getInputDevices()) {
144 midiListeners.add(new MidiListener(d));
145 }
146 SCMidiDevices.initializeStandardDevices(glucose);
147 logTime("Setup MIDI devices");
148
149 // Setup camera
150 midX = TRAILER_WIDTH/2.;
151 midY = glucose.model.yMax/2;
152 midZ = TRAILER_DEPTH/2.;
153 eyeR = -290;
154 eyeA = .15;
155 eyeY = midY + 70;
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());
161 }});
162
163 println("Total setup: " + (millis() - startMillis) + "ms");
164 println("Hit the 'p' key to toggle Panda Board output");
165 }
166
167 public class MidiListener {
168 private boolean enabled = false;
169 private final String name;
170
171 MidiListener(MidiInputDevice d) {
172 d.createInput(this);
173 name = d.getName();
174 }
175
176 MidiListener() {
177 registerKeyEvent(this);
178 name = "Keyboard";
179 }
180
181 public String getName() {
182 return name;
183 }
184
185 public void keyEvent(KeyEvent e) {
186 if (e.getID() == KeyEvent.KEY_PRESSED) {
187 switch (e.getKeyChar()) {
188 case 'q':
189 noteOnReceived(new Note(60, 127));
190 break;
191 }
192 } else if (e.getID() == KeyEvent.KEY_RELEASED) {
193 switch (e.getKeyChar()) {
194 case 'q':
195 noteOffReceived(new Note(60, 0));
196 break;
197 }
198 }
199 }
200
201 public MidiListener setEnabled(boolean enabled) {
202 if (enabled != this.enabled) {
203 this.enabled = enabled;
204 // notify midi UI to update
205 }
206 return this;
207 }
208
209 void controllerChangeReceived(rwmidi.Controller cc) {
210 if (!enabled) {
211 return;
212 }
213 println("CC: " + cc.toString());
214 }
215
216 void noteOnReceived(Note note) {
217 if (!enabled) {
218 return;
219 }
220 println("Note On: " + note.toString());
221 }
222
223 void noteOffReceived(Note note) {
224 if (!enabled) {
225 return;
226 }
227 println("Note Off: " + note.toString());
228 }
229
230 }
231
232 /**
233 * Core render loop and drawing functionality.
234 */
235 void draw() {
236 // Draws the simulation and the 2D UI overlay
237 background(40);
238 color[] colors = glucose.getColors();
239
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();
245 }
246 if (debugMode) {
247 debugUI.maskColors(colors);
248 }
249
250 camera(
251 eyeX, eyeY, eyeZ,
252 midX, midY, midZ,
253 0, -1, 0
254 );
255
256 translate(0, 10, 0);
257
258 noStroke();
259 fill(#141414);
260 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
261 fill(#070707);
262 stroke(#222222);
263 beginShape();
264 vertex(0, 0, 0);
265 vertex(TRAILER_WIDTH, 0, 0);
266 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
267 vertex(0, 0, TRAILER_DEPTH);
268 endShape();
269
270 noStroke();
271 // drawBassBox(glucose.model.bassBox);
272 // for (Speaker s : glucose.model.speakers) {
273 // drawSpeaker(s);
274 // }
275 for (Cube c : glucose.model.cubes) {
276 drawCube(c);
277 }
278
279 noFill();
280 strokeWeight(2);
281 beginShape(POINTS);
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);
288 }
289 }
290 endShape();
291
292 // 2D Overlay UI
293 drawUI();
294
295 // Send output colors
296 color[] sendColors = glucose.getColors();
297 if (debugMode) {
298 debugUI.maskColors(colors);
299 }
300
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;
305 colors[i] = color(
306 hue(colors[i]),
307 saturation(colors[i]),
308 (b*b*b) * 100.
309 );
310 }
311
312 // TODO(mcslee): move into GLucose engine
313 for (PandaDriver p : pandaBoards) {
314 p.send(colors);
315 }
316 }
317
318 void drawBassBox(BassBox b) {
319 float in = .15;
320
321 noStroke();
322 fill(#191919);
323 pushMatrix();
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);
326 popMatrix();
327
328 noStroke();
329 fill(#393939);
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);
331
332 pushMatrix();
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);
338 lastOffset = offset;
339 }
340 popMatrix();
341
342 pushMatrix();
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) {
345 pushMatrix();
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);
349 }
350 popMatrix();
351 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
352 }
353 popMatrix();
354
355 pushMatrix();
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);
360 popMatrix();
361
362 }
363
364 void drawCube(Cube c) {
365 float in = .15;
366 noStroke();
367 fill(#393939);
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);
369 }
370
371 void drawSpeaker(Speaker s) {
372 float in = .15;
373
374 noStroke();
375 fill(#191919);
376 pushMatrix();
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);
382
383 fill(#222222);
384 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
385 popMatrix();
386
387 noStroke();
388 fill(#393939);
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);
390 }
391
392 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
393 pushMatrix();
394 translate(x, y, z);
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;
400
401 beginShape();
402 vertex(0, 0);
403 vertex(wid, 0);
404 vertex(wid, yd);
405 vertex(wid - sw, yd);
406 vertex(wid - sw, sw);
407 vertex(0, sw);
408 endShape();
409 beginShape();
410 vertex(0, sw);
411 vertex(0, yd);
412 vertex(wid - sw, yd);
413 vertex(wid - sw, yd - sw);
414 vertex(sw, yd - sw);
415 vertex(sw, sw);
416 endShape();
417
418 translate(wid, 0, 0);
419 rotate(HALF_PI, 0, -1, 0);
420 }
421 popMatrix();
422 }
423
424 void drawUI() {
425 camera();
426 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
427 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
428 ((PGraphicsOpenGL)g).endGL();
429 strokeWeight(1);
430
431 if (uiOn) {
432 for (UIContext context : overlays) {
433 context.draw();
434 }
435 }
436
437 // Always draw FPS meter
438 fill(#555555);
439 textSize(9);
440 textAlign(LEFT, BASELINE);
441 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
442
443 if (debugMode) {
444 debugUI.draw();
445 }
446 }
447
448 /**
449 * Top-level keyboard event handling
450 */
451 void keyPressed() {
452 if (mappingMode) {
453 mappingTool.keyPressed(uiMapping);
454 }
455 switch (key) {
456 case '-':
457 case '_':
458 frameRate(--targetFramerate);
459 break;
460 case '=':
461 case '+':
462 frameRate(++targetFramerate);
463 break;
464 case 'd':
465 debugMode = !debugMode;
466 println("Debug output: " + (debugMode ? "ON" : "OFF"));
467 break;
468 case 'm':
469 mappingMode = !mappingMode;
470 uiPatternA.setVisible(!mappingMode);
471 uiMapping.setVisible(mappingMode);
472 if (mappingMode) {
473 restoreToPattern = lx.getPattern();
474 lx.setPatterns(new LXPattern[] { mappingTool });
475 } else {
476 lx.setPatterns(patterns);
477 LXTransition pop = restoreToPattern.getTransition();
478 restoreToPattern.setTransition(null);
479 lx.goPattern(restoreToPattern);
480 restoreToPattern.setTransition(pop);
481 }
482 break;
483 case 'p':
484 for (PandaDriver p : pandaBoards) {
485 p.toggle();
486 }
487 break;
488 case 'u':
489 uiOn = !uiOn;
490 break;
491 }
492 }
493
494 /**
495 * Top-level mouse event handling
496 */
497 int mx, my;
498 void mousePressed() {
499 boolean debugged = false;
500 if (debugMode) {
501 debugged = debugUI.mousePressed();
502 }
503 if (!debugged) {
504 for (UIContext context : overlays) {
505 context.mousePressed(mouseX, mouseY);
506 }
507 }
508 mx = mouseX;
509 my = mouseY;
510 }
511
512 void mouseDragged() {
513 boolean dragged = false;
514 for (UIContext context : overlays) {
515 dragged |= context.mouseDragged(mouseX, mouseY);
516 }
517 if (!dragged) {
518 int dx = mouseX - mx;
519 int dy = mouseY - my;
520 mx = mouseX;
521 my = mouseY;
522 eyeA += dx*.003;
523 eyeX = midX + eyeR*sin(eyeA);
524 eyeZ = midZ + eyeR*cos(eyeA);
525 eyeY += dy;
526 }
527 }
528
529 void mouseReleased() {
530 for (UIContext context : overlays) {
531 context.mouseReleased(mouseX, mouseY);
532 }
533 }
534
535 void mouseWheel(int delta) {
536 boolean wheeled = false;
537 for (UIContext context : overlays) {
538 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
539 }
540
541 if (!wheeled) {
542 eyeR = constrain(eyeR - delta, -500, -80);
543 eyeX = midX + eyeR*sin(eyeA);
544 eyeZ = midZ + eyeR*cos(eyeA);
545 }
546 }