Some cleanups and listen to deck for transition changes
[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
52 // Display configuration mode
53 boolean mappingMode = false;
54 boolean debugMode = false;
55 DebugUI debugUI;
56
57 // Handles to UI objects
58 UIContext[] overlays;
59 UIPatternDeck uiPatternA;
60 UICrossfader uiCrossfader;
61 UIMapping uiMapping;
62 UIDebugText uiDebugText;
63
64 // Camera variables
65 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
66
67 LXPattern[] _patterns(GLucose glucose) {
68 LXPattern[] patterns = patterns(glucose);
69 for (LXPattern p : patterns) {
70 p.setTransition(new DissolveTransition(glucose.lx).setDuration(1000));
71 }
72 return patterns;
73 }
74
75 void setup() {
76 startMillis = lastMillis = millis();
77
78 // Initialize the Processing graphics environment
79 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
80 frameRate(targetFramerate);
81 noSmooth();
82 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
83 logTime("Created viewport");
84
85 // Create the GLucose engine to run the cubes
86 glucose = new GLucose(this, buildModel());
87 lx = glucose.lx;
88 lx.enableKeyboardTempo();
89 logTime("Built GLucose engine");
90
91 // Set the patterns
92 Engine engine = lx.engine;
93 engine.setPatterns(patterns = _patterns(glucose));
94 engine.addDeck(_patterns(glucose));
95 logTime("Built patterns");
96 glucose.setTransitions(transitions(glucose));
97 logTime("Built transitions");
98 glucose.lx.addEffects(effects(glucose));
99 logTime("Built effects");
100
101 // Build output driver
102 PandaMapping[] pandaMappings = buildPandaList();
103 pandaBoards = new PandaDriver[pandaMappings.length];
104 int pbi = 0;
105 for (PandaMapping pm : pandaMappings) {
106 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
107 }
108 mappingTool = new MappingTool(glucose, pandaMappings);
109 logTime("Built PandaDriver");
110
111 // Build overlay UI
112 debugUI = new DebugUI(pandaMappings);
113 overlays = new UIContext[] {
114 uiPatternA = new UIPatternDeck(lx.engine.getDeck(0), "PATTERN A", 4, 4, 140, 344),
115 uiCrossfader = new UICrossfader(4, 352, 140, 212),
116
117 new UIPatternDeck(lx.engine.getDeck(1), "PATTERN B", width-144, 4, 140, 344),
118 new UIEffects(width-144, 352, 140, 144),
119 new UITempo(width-144, 498, 140, 50),
120 new UIOutput(width-144, 552, 140, 106),
121
122 uiDebugText = new UIDebugText(4, height-64, width-8, 44),
123 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 344),
124 };
125 uiMapping.setVisible(false);
126 logTime("Built overlay UI");
127
128 // MIDI devices
129 for (MidiInputDevice d : RWMidi.getInputDevices()) {
130 d.createInput(this);
131 }
132 SCMidiDevices.initializeStandardDevices(glucose);
133 logTime("Setup MIDI devices");
134
135 // Setup camera
136 midX = TRAILER_WIDTH/2.;
137 midY = glucose.model.yMax/2;
138 midZ = TRAILER_DEPTH/2.;
139 eyeR = -290;
140 eyeA = .15;
141 eyeY = midY + 70;
142 eyeX = midX + eyeR*sin(eyeA);
143 eyeZ = midZ + eyeR*cos(eyeA);
144 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
145 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
146 mouseWheel(mwe.getWheelRotation());
147 }});
148
149 println("Total setup: " + (millis() - startMillis) + "ms");
150 println("Hit the 'p' key to toggle Panda Board output");
151 }
152
153
154 void controllerChangeReceived(rwmidi.Controller cc) {
155 if (debugMode) {
156 println("CC: " + cc.toString());
157 }
158 }
159
160 void noteOnReceived(Note note) {
161 if (debugMode) {
162 println("Note On: " + note.toString());
163 }
164 }
165
166 void noteOffReceived(Note note) {
167 if (debugMode) {
168 println("Note Off: " + note.toString());
169 }
170 }
171
172 void logTime(String evt) {
173 int now = millis();
174 println(evt + ": " + (now - lastMillis) + "ms");
175 lastMillis = now;
176 }
177
178 void draw() {
179 // Draws the simulation and the 2D UI overlay
180 background(40);
181 color[] colors = glucose.getColors();
182
183 String displayMode = uiCrossfader.getDisplayMode();
184 if (displayMode == "A") {
185 colors = lx.engine.getDeck(0).getColors();
186 } else if (displayMode == "B") {
187 colors = lx.engine.getDeck(1).getColors();
188 }
189 if (debugMode) {
190 debugUI.maskColors(colors);
191 }
192
193 camera(
194 eyeX, eyeY, eyeZ,
195 midX, midY, midZ,
196 0, -1, 0
197 );
198
199 translate(0, 10, 0);
200
201 noStroke();
202 fill(#141414);
203 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
204 fill(#070707);
205 stroke(#222222);
206 beginShape();
207 vertex(0, 0, 0);
208 vertex(TRAILER_WIDTH, 0, 0);
209 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
210 vertex(0, 0, TRAILER_DEPTH);
211 endShape();
212
213 noStroke();
214 // drawBassBox(glucose.model.bassBox);
215 // for (Speaker s : glucose.model.speakers) {
216 // drawSpeaker(s);
217 // }
218 for (Cube c : glucose.model.cubes) {
219 drawCube(c);
220 }
221
222 noFill();
223 strokeWeight(2);
224 beginShape(POINTS);
225 // TODO(mcslee): restore when bassBox/speakers are right again
226 // for (Point p : glucose.model.points) {
227 for (Cube cube : glucose.model.cubes) {
228 for (Point p : cube.points) {
229 stroke(colors[p.index]);
230 vertex(p.fx, p.fy, p.fz);
231 }
232 }
233 endShape();
234
235 // 2D Overlay UI
236 drawUI();
237
238 // Send output colors
239 color[] sendColors = glucose.getColors();
240 if (debugMode) {
241 debugUI.maskColors(colors);
242 }
243
244 // Gamma correction here. Apply a cubic to the brightness
245 // for better representation of dynamic range
246 for (int i = 0; i < colors.length; ++i) {
247 float b = brightness(colors[i]) / 100.f;
248 colors[i] = color(
249 hue(colors[i]),
250 saturation(colors[i]),
251 (b*b*b) * 100.
252 );
253 }
254
255 // TODO(mcslee): move into GLucose engine
256 for (PandaDriver p : pandaBoards) {
257 p.send(colors);
258 }
259 }
260
261 void drawBassBox(BassBox b) {
262 float in = .15;
263
264 noStroke();
265 fill(#191919);
266 pushMatrix();
267 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
268 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
269 popMatrix();
270
271 noStroke();
272 fill(#393939);
273 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);
274
275 pushMatrix();
276 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
277 float lastOffset = 0;
278 for (float offset : BoothFloor.STRIP_OFFSETS) {
279 translate(offset - lastOffset, 0, 0);
280 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
281 lastOffset = offset;
282 }
283 popMatrix();
284
285 pushMatrix();
286 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
287 for (int j = 0; j < 2; ++j) {
288 pushMatrix();
289 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
290 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
291 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
292 }
293 popMatrix();
294 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
295 }
296 popMatrix();
297
298 pushMatrix();
299 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
300 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
301 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
302 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
303 popMatrix();
304
305 }
306
307 void drawCube(Cube c) {
308 float in = .15;
309 noStroke();
310 fill(#393939);
311 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);
312 }
313
314 void drawSpeaker(Speaker s) {
315 float in = .15;
316
317 noStroke();
318 fill(#191919);
319 pushMatrix();
320 translate(s.x, s.y, s.z);
321 rotate(s.ry / 180. * PI, 0, -1, 0);
322 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
323 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
324 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
325
326 fill(#222222);
327 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
328 popMatrix();
329
330 noStroke();
331 fill(#393939);
332 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);
333 }
334
335 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
336 pushMatrix();
337 translate(x, y, z);
338 rotate(rx / 180. * PI, -1, 0, 0);
339 rotate(ry / 180. * PI, 0, -1, 0);
340 rotate(rz / 180. * PI, 0, 0, -1);
341 for (int i = 0; i < 4; ++i) {
342 float wid = (i % 2 == 0) ? xd : zd;
343
344 beginShape();
345 vertex(0, 0);
346 vertex(wid, 0);
347 vertex(wid, yd);
348 vertex(wid - sw, yd);
349 vertex(wid - sw, sw);
350 vertex(0, sw);
351 endShape();
352 beginShape();
353 vertex(0, sw);
354 vertex(0, yd);
355 vertex(wid - sw, yd);
356 vertex(wid - sw, yd - sw);
357 vertex(sw, yd - sw);
358 vertex(sw, sw);
359 endShape();
360
361 translate(wid, 0, 0);
362 rotate(HALF_PI, 0, -1, 0);
363 }
364 popMatrix();
365 }
366
367 void drawUI() {
368 camera();
369 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
370 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
371 ((PGraphicsOpenGL)g).endGL();
372 strokeWeight(1);
373
374 if (uiOn) {
375 for (UIContext context : overlays) {
376 context.draw();
377 }
378 }
379
380 // Always draw FPS meter
381 fill(#555555);
382 textSize(9);
383 textAlign(LEFT, BASELINE);
384 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
385
386 if (debugMode) {
387 debugUI.draw();
388 }
389 }
390
391 boolean uiOn = true;
392 LXPattern restoreToPattern = null;
393
394 void keyPressed() {
395 if (mappingMode) {
396 mappingTool.keyPressed(uiMapping);
397 }
398 switch (key) {
399 case '-':
400 case '_':
401 frameRate(--targetFramerate);
402 break;
403 case '=':
404 case '+':
405 frameRate(++targetFramerate);
406 break;
407 case 'd':
408 debugMode = !debugMode;
409 println("Debug output: " + (debugMode ? "ON" : "OFF"));
410 break;
411 case 'm':
412 mappingMode = !mappingMode;
413 uiPatternA.setVisible(!mappingMode);
414 uiMapping.setVisible(mappingMode);
415 if (mappingMode) {
416 restoreToPattern = lx.getPattern();
417 lx.setPatterns(new LXPattern[] { mappingTool });
418 } else {
419 lx.setPatterns(patterns);
420 LXTransition pop = restoreToPattern.getTransition();
421 restoreToPattern.setTransition(null);
422 lx.goPattern(restoreToPattern);
423 restoreToPattern.setTransition(pop);
424 }
425 break;
426 case 'p':
427 for (PandaDriver p : pandaBoards) {
428 p.toggle();
429 }
430 break;
431 case 'u':
432 uiOn = !uiOn;
433 break;
434 }
435 }
436
437 int mx, my;
438 void mousePressed() {
439 boolean debugged = false;
440 if (debugMode) {
441 debugged = debugUI.mousePressed();
442 }
443 if (!debugged) {
444 for (UIContext context : overlays) {
445 context.mousePressed(mouseX, mouseY);
446 }
447 }
448 mx = mouseX;
449 my = mouseY;
450 }
451
452 void mouseDragged() {
453 boolean dragged = false;
454 for (UIContext context : overlays) {
455 dragged |= context.mouseDragged(mouseX, mouseY);
456 }
457 if (!dragged) {
458 int dx = mouseX - mx;
459 int dy = mouseY - my;
460 mx = mouseX;
461 my = mouseY;
462 eyeA += dx*.003;
463 eyeX = midX + eyeR*sin(eyeA);
464 eyeZ = midZ + eyeR*cos(eyeA);
465 eyeY += dy;
466 }
467 }
468
469 void mouseReleased() {
470 for (UIContext context : overlays) {
471 context.mouseReleased(mouseX, mouseY);
472 }
473
474 // ui.mouseReleased();
475 }
476
477 void mouseWheel(int delta) {
478 boolean wheeled = false;
479 for (UIContext context : overlays) {
480 wheeled |= context.mouseWheel(mouseX, mouseY, delta);
481 }
482
483 if (!wheeled) {
484 eyeR = constrain(eyeR - delta, -500, -80);
485 eyeX = midX + eyeR*sin(eyeA);
486 eyeZ = midZ + eyeR*cos(eyeA);
487 }
488 }