Performance improvements to SineSpheres and 's' to toggle simulation
[SugarCubes.git] / _Internals.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//H
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 import java.lang.reflect.*;
34
35 final int VIEWPORT_WIDTH = 900;
36 final int VIEWPORT_HEIGHT = 700;
37
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;
42
43 final int MaxCubeHeight = 5;
44 final int NumBackTowers = 11;
45
46 int targetFramerate = 60;
47 int startMillis, lastMillis;
48
49 // Core engine variables
50 GLucose glucose;
51 LX lx;
52 LXPattern[] patterns;
53 Effects effects;
54 MappingTool mappingTool;
55 PandaDriver[] pandaBoards;
56 PresetManager presetManager;
57 MidiEngine midiEngine;
58
59 // Display configuration mode
60 boolean mappingMode = false;
61 boolean debugMode = false;
62 DebugUI debugUI;
63 boolean uiOn = true;
64 boolean simulationOn = true;
65 LXPattern restoreToPattern = null;
66 PImage logo;
67 float[] hsb = new float[3];
68
69 // Handles to UI objects
70 UIContext[] overlays;
71 UIPatternDeck uiPatternA;
72 UICrossfader uiCrossfader;
73 UIMidi uiMidi;
74 UIMapping uiMapping;
75 UIDebugText uiDebugText;
76 UISpeed uiSpeed;
77
78 // Camera variables
79 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
80
81 /**
82 * Engine construction and initialization.
83 */
84
85 LXTransition _transition(GLucose glucose) {
86 return new DissolveTransition(glucose.lx).setDuration(1000);
87 }
88
89 LXPattern[] _leftPatterns(GLucose glucose) {
90 LXPattern[] patterns = patterns(glucose);
91 for (LXPattern p : patterns) {
92 p.setTransition(_transition(glucose));
93 }
94 return patterns;
95 }
96
97 LXPattern[] _rightPatterns(GLucose glucose) {
98 LXPattern[] patterns = _leftPatterns(glucose);
99 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
100 int i = 0;
101 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
102 for (LXPattern p : patterns) {
103 rightPatterns[i++] = p;
104 }
105 return rightPatterns;
106 }
107
108 LXEffect[] _effectsArray(Effects effects) {
109 List<LXEffect> effectList = new ArrayList<LXEffect>();
110 for (Field f : effects.getClass().getDeclaredFields()) {
111 try {
112 Object val = f.get(effects);
113 if (val instanceof LXEffect) {
114 effectList.add((LXEffect)val);
115 }
116 } catch (IllegalAccessException iax) {}
117 }
118 return effectList.toArray(new LXEffect[]{});
119 }
120
121 void logTime(String evt) {
122 int now = millis();
123 println(evt + ": " + (now - lastMillis) + "ms");
124 lastMillis = now;
125 }
126
127 void setup() {
128 startMillis = lastMillis = millis();
129
130 // Initialize the Processing graphics environment
131 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
132 frameRate(targetFramerate);
133 noSmooth();
134 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
135 logTime("Created viewport");
136
137 // Create the GLucose engine to run the cubes
138 glucose = new GLucose(this, buildModel());
139 lx = glucose.lx;
140 lx.enableKeyboardTempo();
141 logTime("Built GLucose engine");
142
143 // Set the patterns
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");
152
153 // Preset manager
154 presetManager = new PresetManager();
155 logTime("Loaded presets");
156
157 // MIDI devices
158 midiEngine = new MidiEngine();
159 logTime("Setup MIDI devices");
160
161 // Build output driver
162 PandaMapping[] pandaMappings = buildPandaList();
163 pandaBoards = new PandaDriver[pandaMappings.length];
164 int pbi = 0;
165 for (PandaMapping pm : pandaMappings) {
166 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
167 }
168 mappingTool = new MappingTool(glucose, pandaMappings);
169 logTime("Built PandaDriver");
170
171 // Build overlay UI
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),
179
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),
183
184 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
185
186 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
187 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324),
188 };
189 uiMapping.setVisible(false);
190 logTime("Built overlay UI");
191
192 // Load logo image
193 logo = loadImage("data/logo.png");
194
195 // Setup camera
196 midX = TRAILER_WIDTH/2.;
197 midY = glucose.model.yMax/2;
198 midZ = TRAILER_DEPTH/2.;
199 eyeR = -290;
200 eyeA = .15;
201 eyeY = midY + 70;
202 eyeX = midX + eyeR*sin(eyeA);
203 eyeZ = midZ + eyeR*cos(eyeA);
204
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());
209 }});
210
211 println("Total setup: " + (millis() - startMillis) + "ms");
212 println("Hit the 'p' key to toggle Panda Board output");
213 }
214
215 /**
216 * Core render loop and drawing functionality.
217 */
218 void draw() {
219 // Draws the simulation and the 2D UI overlay
220 background(40);
221
222 color[] simulationColors;
223 color[] sendColors;
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();
230 }
231 if (debugMode) {
232 debugUI.maskColors(simulationColors);
233 debugUI.maskColors(sendColors);
234 }
235
236 if (simulationOn) {
237 drawSimulation(simulationColors);
238 }
239
240 // 2D Overlay UI
241 drawUI();
242
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);
247 float b = hsb[2];
248 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
249 }
250
251 // TODO(mcslee): move into GLucose engine
252 for (PandaDriver p : pandaBoards) {
253 p.send(sendColors);
254 }
255 }
256
257 void drawSimulation(color[] simulationColors) {
258 camera(
259 eyeX, eyeY, eyeZ,
260 midX, midY, midZ,
261 0, -1, 0
262 );
263
264 translate(0, 40, 0);
265
266 noStroke();
267 fill(#141414);
268 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
269 fill(#070707);
270 stroke(#222222);
271 beginShape();
272 vertex(0, 0, 0);
273 vertex(TRAILER_WIDTH, 0, 0);
274 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
275 vertex(0, 0, TRAILER_DEPTH);
276 endShape();
277
278 // Draw the logo on the front of platform
279 pushMatrix();
280 translate(0, 0, -1);
281 float s = .07;
282 scale(s, -s, s);
283 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
284 popMatrix();
285
286 noStroke();
287 if (glucose.model.bassBox.exists) {
288 drawBassBox(glucose.model.bassBox, false);
289 }
290 for (Speaker speaker : glucose.model.speakers) {
291 drawSpeaker(speaker);
292 }
293 for (Cube c : glucose.model.cubes) {
294 drawCube(c);
295 }
296
297 noFill();
298 strokeWeight(2);
299 beginShape(POINTS);
300 for (Point p : glucose.model.points) {
301 stroke(simulationColors[p.index]);
302 vertex(p.x, p.y, p.z);
303 }
304 endShape();
305 }
306
307 void drawBassBox(BassBox b, boolean hasSub) {
308
309 float in = .15;
310
311 if (hasSub) {
312 noStroke();
313 fill(#191919);
314 pushMatrix();
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);
317 popMatrix();
318 }
319
320 noStroke();
321 fill(#393939);
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);
323
324 pushMatrix();
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);
330 lastOffset = offset;
331 }
332 popMatrix();
333
334 pushMatrix();
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) {
337 pushMatrix();
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);
341 }
342 popMatrix();
343 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
344 }
345 popMatrix();
346
347 pushMatrix();
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);
352 popMatrix();
353
354 }
355
356 void drawCube(Cube c) {
357 float in = .15;
358 noStroke();
359 fill(#393939);
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);
361 }
362
363 void drawSpeaker(Speaker s) {
364 float in = .15;
365
366 noStroke();
367 fill(#191919);
368 pushMatrix();
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);
374
375 fill(#222222);
376 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
377 popMatrix();
378
379 noStroke();
380 fill(#393939);
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);
382 }
383
384 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
385 pushMatrix();
386 translate(x, y, z);
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;
392
393 beginShape();
394 vertex(0, 0);
395 vertex(wid, 0);
396 vertex(wid, yd);
397 vertex(wid - sw, yd);
398 vertex(wid - sw, sw);
399 vertex(0, sw);
400 endShape();
401 beginShape();
402 vertex(0, sw);
403 vertex(0, yd);
404 vertex(wid - sw, yd);
405 vertex(wid - sw, yd - sw);
406 vertex(sw, yd - sw);
407 vertex(sw, sw);
408 endShape();
409
410 translate(wid, 0, 0);
411 rotate(HALF_PI, 0, -1, 0);
412 }
413 popMatrix();
414 }
415
416 void drawUI() {
417 camera();
418 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
419 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
420 ((PGraphicsOpenGL)g).endGL();
421 strokeWeight(1);
422
423 if (uiOn) {
424 for (UIContext context : overlays) {
425 context.draw();
426 }
427 }
428
429 // Always draw FPS meter
430 fill(#555555);
431 textSize(9);
432 textAlign(LEFT, BASELINE);
433 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
434
435 if (debugMode) {
436 debugUI.draw();
437 }
438 }
439
440
441 /**
442 * Top-level keyboard event handling
443 */
444 void keyPressed() {
445 if (mappingMode) {
446 mappingTool.keyPressed(uiMapping);
447 }
448 switch (key) {
449 case '1':
450 case '2':
451 case '3':
452 case '4':
453 case '5':
454 case '6':
455 case '7':
456 case '8':
457 if (!midiEngine.isQwertyEnabled()) {
458 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
459 }
460 break;
461
462 case '!':
463 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
464 break;
465 case '@':
466 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
467 break;
468 case '#':
469 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
470 break;
471 case '$':
472 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
473 break;
474 case '%':
475 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
476 break;
477 case '^':
478 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
479 break;
480 case '&':
481 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
482 break;
483 case '*':
484 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
485 break;
486
487 case '-':
488 case '_':
489 frameRate(--targetFramerate);
490 break;
491 case '=':
492 case '+':
493 frameRate(++targetFramerate);
494 break;
495 case 'b':
496 effects.boom.trigger();
497 break;
498 case 'd':
499 if (!midiEngine.isQwertyEnabled()) {
500 debugMode = !debugMode;
501 println("Debug output: " + (debugMode ? "ON" : "OFF"));
502 }
503 break;
504 case 'm':
505 if (!midiEngine.isQwertyEnabled()) {
506 mappingMode = !mappingMode;
507 uiPatternA.setVisible(!mappingMode);
508 uiMapping.setVisible(mappingMode);
509 if (mappingMode) {
510 restoreToPattern = lx.getPattern();
511 lx.setPatterns(new LXPattern[] { mappingTool });
512 } else {
513 lx.setPatterns(patterns);
514 LXTransition pop = restoreToPattern.getTransition();
515 restoreToPattern.setTransition(null);
516 lx.goPattern(restoreToPattern);
517 restoreToPattern.setTransition(pop);
518 }
519 }
520 break;
521 case 't':
522 if (!midiEngine.isQwertyEnabled()) {
523 lx.engine.setThreaded(!lx.engine.isThreaded());
524 }
525 break;
526 case 'p':
527 for (PandaDriver p : pandaBoards) {
528 p.toggle();
529 }
530 break;
531 case 's':
532 if (!midiEngine.isQwertyEnabled()) {
533 simulationOn = !simulationOn;
534 }
535 break;
536 case 'u':
537 if (!midiEngine.isQwertyEnabled()) {
538 uiOn = !uiOn;
539 }
540 break;
541 }
542 }
543
544 /**
545 * Top-level mouse event handling
546 */
547 int mx, my;
548 void mousePressed() {
549 boolean debugged = false;
550 if (debugMode) {
551 debugged = debugUI.mousePressed();
552 }
553 if (!debugged) {
554 for (UIContext context : overlays) {
555 context.mousePressed(mouseX, mouseY);
556 }
557 }
558 mx = mouseX;
559 my = mouseY;
560 }
561
562 void mouseDragged() {
563 boolean dragged = false;
564 for (UIContext context : overlays) {
565 dragged |= context.mouseDragged(mouseX, mouseY);
566 }
567 if (!dragged) {
568 int dx = mouseX - mx;
569 int dy = mouseY - my;
570 mx = mouseX;
571 my = mouseY;
572 eyeA += dx*.003;
573 eyeX = midX + eyeR*sin(eyeA);
574 eyeZ = midZ + eyeR*cos(eyeA);
575 eyeY += dy;
576 }
577 }
578
579 void mouseReleased() {
580 for (UIContext context : overlays) {
581 context.mouseReleased(mouseX, mouseY);
582 }
583 }
584
585 void mouseWheel(int delta) {
586 boolean wheeled = false;
587 for (UIContext context : overlays) {
588 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
589 }
590
591 if (!wheeled) {
592 eyeR = constrain(eyeR - delta, -500, -80);
593 eyeX = midX + eyeR*sin(eyeA);
594 eyeZ = midZ + eyeR*cos(eyeA);
595 }
596 }