Add speed slider, new HeronLX with deltaMs as double
[SugarCubes.git] / _Internals.pde
CommitLineData
49815cc0 1/**
1ecdb44a
MS
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
49815cc0
MS
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
16import glucose.*;
17import glucose.control.*;
3f8be614 18import glucose.effect.*;
f3f5a876 19import glucose.model.*;
49815cc0 20import glucose.pattern.*;
f3f5a876 21import glucose.transform.*;
49815cc0 22import glucose.transition.*;
49815cc0 23import heronarts.lx.*;
3f8be614 24import heronarts.lx.control.*;
49815cc0 25import heronarts.lx.effect.*;
49815cc0 26import heronarts.lx.modulator.*;
f3f5a876 27import heronarts.lx.pattern.*;
49815cc0
MS
28import heronarts.lx.transition.*;
29import ddf.minim.*;
30import ddf.minim.analysis.*;
31import processing.opengl.*;
5d70e4d7 32import rwmidi.*;
49815cc0
MS
33
34final int VIEWPORT_WIDTH = 900;
35final int VIEWPORT_HEIGHT = 700;
0e3c5542 36
92c06c97 37// The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
87998ff3
MS
38final float TRAILER_WIDTH = 240;
39final float TRAILER_DEPTH = 97;
40final float TRAILER_HEIGHT = 33;
41
51d0d59a 42int targetFramerate = 60;
49815cc0 43int startMillis, lastMillis;
a898d79b
MS
44
45// Core engine variables
49815cc0
MS
46GLucose glucose;
47HeronLX lx;
48LXPattern[] patterns;
a898d79b 49MappingTool mappingTool;
79ae8245 50PandaDriver[] pandaBoards;
34327c96 51final List<MidiListener> midiListeners = new ArrayList<MidiListener>();
a898d79b
MS
52
53// Display configuration mode
bf551144 54boolean mappingMode = false;
cc9fcf4b 55boolean debugMode = false;
554e38ff 56DebugUI debugUI;
34327c96
MS
57boolean uiOn = true;
58LXPattern restoreToPattern = null;
d626bc9b 59
a898d79b 60// Handles to UI objects
d626bc9b
MS
61UIContext[] overlays;
62UIPatternDeck uiPatternA;
a898d79b 63UICrossfader uiCrossfader;
d626bc9b
MS
64UIMapping uiMapping;
65UIDebugText uiDebugText;
cc9fcf4b 66
0a9f99cc 67// Camera variables
bda5421d 68float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 69
34327c96
MS
70/**
71 * Engine construction and initialization.
72 */
d626bc9b
MS
73LXPattern[] _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
34327c96
MS
81void logTime(String evt) {
82 int now = millis();
83 println(evt + ": " + (now - lastMillis) + "ms");
84 lastMillis = now;
85}
86
49815cc0
MS
87void setup() {
88 startMillis = lastMillis = millis();
89
90 // Initialize the Processing graphics environment
91 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 92 frameRate(targetFramerate);
3f8be614 93 noSmooth();
49815cc0
MS
94 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
95 logTime("Created viewport");
96
97 // Create the GLucose engine to run the cubes
186bc4d3 98 glucose = new GLucose(this, buildModel());
49815cc0 99 lx = glucose.lx;
cc9fcf4b 100 lx.enableKeyboardTempo();
49815cc0
MS
101 logTime("Built GLucose engine");
102
103 // Set the patterns
d626bc9b 104 Engine engine = lx.engine;
d626bc9b
MS
105 engine.setPatterns(patterns = _patterns(glucose));
106 engine.addDeck(_patterns(glucose));
49815cc0 107 logTime("Built patterns");
a898d79b
MS
108 glucose.setTransitions(transitions(glucose));
109 logTime("Built transitions");
110 glucose.lx.addEffects(effects(glucose));
49815cc0 111 logTime("Built effects");
e73ef85d
MS
112
113 // Build output driver
186bc4d3 114 PandaMapping[] pandaMappings = buildPandaList();
45f43cc2
MS
115 pandaBoards = new PandaDriver[pandaMappings.length];
116 int pbi = 0;
117 for (PandaMapping pm : pandaMappings) {
44b8de9c 118 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
45f43cc2
MS
119 }
120 mappingTool = new MappingTool(glucose, pandaMappings);
121 logTime("Built PandaDriver");
49815cc0
MS
122
123 // Build overlay UI
45f43cc2 124 debugUI = new DebugUI(pandaMappings);
d626bc9b
MS
125 overlays = new UIContext[] {
126 uiPatternA = new UIPatternDeck(lx.engine.getDeck(0), "PATTERN A", 4, 4, 140, 344),
a898d79b 127 uiCrossfader = new UICrossfader(4, 352, 140, 212),
34327c96 128 new UIOutput(4, 568, 140, 106),
d626bc9b
MS
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),
34327c96 133 new UISpeed(width-144, 552, 140, 50),
d626bc9b
MS
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);
49815cc0 139 logTime("Built overlay UI");
1ecdb44a 140
49815cc0 141 // MIDI devices
34327c96 142 midiListeners.add(new MidiListener().setEnabled(true));
cc9fcf4b 143 for (MidiInputDevice d : RWMidi.getInputDevices()) {
34327c96 144 midiListeners.add(new MidiListener(d));
cc9fcf4b
MS
145 }
146 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 147 logTime("Setup MIDI devices");
554e38ff 148
0a9f99cc 149 // Setup camera
d626bc9b 150 midX = TRAILER_WIDTH/2.;
0a9f99cc 151 midY = glucose.model.yMax/2;
e0cea600
MS
152 midZ = TRAILER_DEPTH/2.;
153 eyeR = -290;
0a9f99cc 154 eyeA = .15;
d626bc9b 155 eyeY = midY + 70;
0a9f99cc
MS
156 eyeX = midX + eyeR*sin(eyeA);
157 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
158 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
159 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
160 mouseWheel(mwe.getWheelRotation());
161 }});
162
49815cc0 163 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 164 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
165}
166
34327c96
MS
167public 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 }
cc9fcf4b
MS
213 println("CC: " + cc.toString());
214 }
cc9fcf4b 215
34327c96
MS
216 void noteOnReceived(Note note) {
217 if (!enabled) {
218 return;
219 }
cc9fcf4b
MS
220 println("Note On: " + note.toString());
221 }
cc9fcf4b 222
34327c96
MS
223 void noteOffReceived(Note note) {
224 if (!enabled) {
225 return;
226 }
cc9fcf4b
MS
227 println("Note Off: " + note.toString());
228 }
cc9fcf4b 229
49815cc0
MS
230}
231
34327c96
MS
232/**
233 * Core render loop and drawing functionality.
234 */
49815cc0 235void draw() {
0a9f99cc
MS
236 // Draws the simulation and the 2D UI overlay
237 background(40);
a898d79b
MS
238 color[] colors = glucose.getColors();
239
240 String displayMode = uiCrossfader.getDisplayMode();
d626bc9b
MS
241 if (displayMode == "A") {
242 colors = lx.engine.getDeck(0).getColors();
243 } else if (displayMode == "B") {
244 colors = lx.engine.getDeck(1).getColors();
245 }
554e38ff
MS
246 if (debugMode) {
247 debugUI.maskColors(colors);
248 }
51d0d59a 249
0a9f99cc
MS
250 camera(
251 eyeX, eyeY, eyeZ,
252 midX, midY, midZ,
253 0, -1, 0
254 );
51d0d59a 255
d626bc9b
MS
256 translate(0, 10, 0);
257
51d0d59a
MS
258 noStroke();
259 fill(#141414);
87998ff3 260 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
261 fill(#070707);
262 stroke(#222222);
0a9f99cc 263 beginShape();
51d0d59a 264 vertex(0, 0, 0);
87998ff3
MS
265 vertex(TRAILER_WIDTH, 0, 0);
266 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
267 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 268 endShape();
0a9f99cc 269
51d0d59a 270 noStroke();
ab77005f
MS
271// drawBassBox(glucose.model.bassBox);
272// for (Speaker s : glucose.model.speakers) {
273// drawSpeaker(s);
274// }
51d0d59a
MS
275 for (Cube c : glucose.model.cubes) {
276 drawCube(c);
277 }
278
0a9f99cc
MS
279 noFill();
280 strokeWeight(2);
281 beginShape(POINTS);
922def98
MS
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 }
0a9f99cc
MS
289 }
290 endShape();
291
d626bc9b 292 // 2D Overlay UI
0a9f99cc 293 drawUI();
d626bc9b
MS
294
295 // Send output colors
296 color[] sendColors = glucose.getColors();
554e38ff 297 if (debugMode) {
d626bc9b 298 debugUI.maskColors(colors);
554e38ff 299 }
a797d019 300
6702151a
MS
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 }
e7f14d4d 311
e73ef85d 312 // TODO(mcslee): move into GLucose engine
79ae8245 313 for (PandaDriver p : pandaBoards) {
b58e5a1d 314 p.send(colors);
e73ef85d 315 }
49815cc0
MS
316}
317
92c06c97
MS
318void drawBassBox(BassBox b) {
319 float in = .15;
e76480d4
MS
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);
92c06c97
MS
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
39011e7e
MS
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
92c06c97
MS
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);
ab77005f 360 popMatrix();
e76480d4 361
92c06c97
MS
362}
363
51d0d59a 364void drawCube(Cube c) {
254d34c0 365 float in = .15;
e76480d4
MS
366 noStroke();
367 fill(#393939);
254d34c0 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);
51d0d59a
MS
369}
370
e76480d4
MS
371void 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);
254fbb68
MS
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);
e76480d4
MS
385 popMatrix();
386
e76480d4
MS
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);
ab77005f 390}
e76480d4 391
51d0d59a
MS
392void 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);
e0cea600 395 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 396 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 397 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
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
49815cc0 424void drawUI() {
d626bc9b
MS
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
49815cc0 431 if (uiOn) {
d626bc9b
MS
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();
49815cc0 445 }
49815cc0
MS
446}
447
34327c96
MS
448/**
449 * Top-level keyboard event handling
450 */
49815cc0 451void keyPressed() {
bf551144 452 if (mappingMode) {
d626bc9b 453 mappingTool.keyPressed(uiMapping);
bf551144 454 }
3f8be614 455 switch (key) {
0e3c5542
MS
456 case '-':
457 case '_':
458 frameRate(--targetFramerate);
459 break;
460 case '=':
461 case '+':
462 frameRate(++targetFramerate);
463 break;
cc9fcf4b
MS
464 case 'd':
465 debugMode = !debugMode;
466 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 467 break;
bf551144
MS
468 case 'm':
469 mappingMode = !mappingMode;
d626bc9b
MS
470 uiPatternA.setVisible(!mappingMode);
471 uiMapping.setVisible(mappingMode);
bf551144 472 if (mappingMode) {
d626bc9b 473 restoreToPattern = lx.getPattern();
bf551144
MS
474 lx.setPatterns(new LXPattern[] { mappingTool });
475 } else {
bf551144 476 lx.setPatterns(patterns);
a898d79b
MS
477 LXTransition pop = restoreToPattern.getTransition();
478 restoreToPattern.setTransition(null);
d626bc9b 479 lx.goPattern(restoreToPattern);
a898d79b 480 restoreToPattern.setTransition(pop);
bf551144
MS
481 }
482 break;
e73ef85d 483 case 'p':
79ae8245
MS
484 for (PandaDriver p : pandaBoards) {
485 p.toggle();
486 }
cc9fcf4b 487 break;
3f8be614
MS
488 case 'u':
489 uiOn = !uiOn;
490 break;
49815cc0
MS
491 }
492}
493
34327c96
MS
494/**
495 * Top-level mouse event handling
496 */
0a9f99cc 497int mx, my;
0a9f99cc 498void mousePressed() {
d626bc9b
MS
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 }
0a9f99cc 507 }
d626bc9b
MS
508 mx = mouseX;
509 my = mouseY;
0a9f99cc
MS
510}
511
512void mouseDragged() {
d626bc9b
MS
513 boolean dragged = false;
514 for (UIContext context : overlays) {
515 dragged |= context.mouseDragged(mouseX, mouseY);
516 }
517 if (!dragged) {
0a9f99cc
MS
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
529void mouseReleased() {
d626bc9b
MS
530 for (UIContext context : overlays) {
531 context.mouseReleased(mouseX, mouseY);
532 }
0a9f99cc 533}
bda5421d
MS
534
535void mouseWheel(int delta) {
d626bc9b
MS
536 boolean wheeled = false;
537 for (UIContext context : overlays) {
538 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
539 }
540
541 if (!wheeled) {
0ba6ac44
MS
542 eyeR = constrain(eyeR - delta, -500, -80);
543 eyeX = midX + eyeR*sin(eyeA);
544 eyeZ = midZ + eyeR*cos(eyeA);
545 }
bda5421d 546}