New strips on the DJ booth floor
[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
MS
43
44int startMillis, lastMillis;
45GLucose glucose;
46HeronLX lx;
bf551144 47MappingTool mappingTool;
49815cc0
MS
48LXPattern[] patterns;
49LXTransition[] transitions;
50LXEffect[] effects;
51OverlayUI ui;
bf551144
MS
52ControlUI controlUI;
53MappingUI mappingUI;
79ae8245 54PandaDriver[] pandaBoards;
bf551144 55boolean mappingMode = false;
cc9fcf4b 56boolean debugMode = false;
554e38ff 57DebugUI debugUI;
cc9fcf4b 58
0a9f99cc 59// Camera variables
bda5421d 60float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 61
49815cc0
MS
62void setup() {
63 startMillis = lastMillis = millis();
64
65 // Initialize the Processing graphics environment
66 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 67 frameRate(targetFramerate);
3f8be614 68 noSmooth();
49815cc0
MS
69 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
70 logTime("Created viewport");
71
72 // Create the GLucose engine to run the cubes
186bc4d3 73 glucose = new GLucose(this, buildModel());
49815cc0 74 lx = glucose.lx;
cc9fcf4b 75 lx.enableKeyboardTempo();
49815cc0
MS
76 logTime("Built GLucose engine");
77
78 // Set the patterns
79 glucose.lx.setPatterns(patterns = patterns(glucose));
80 logTime("Built patterns");
81 glucose.lx.addEffects(effects = effects(glucose));
82 logTime("Built effects");
cc9fcf4b 83 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 84 logTime("Built transitions");
e73ef85d
MS
85
86 // Build output driver
186bc4d3 87 PandaMapping[] pandaMappings = buildPandaList();
45f43cc2
MS
88 pandaBoards = new PandaDriver[pandaMappings.length];
89 int pbi = 0;
90 for (PandaMapping pm : pandaMappings) {
44b8de9c 91 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
45f43cc2
MS
92 }
93 mappingTool = new MappingTool(glucose, pandaMappings);
94 logTime("Built PandaDriver");
49815cc0
MS
95
96 // Build overlay UI
bf551144
MS
97 ui = controlUI = new ControlUI();
98 mappingUI = new MappingUI(mappingTool);
45f43cc2 99 debugUI = new DebugUI(pandaMappings);
49815cc0 100 logTime("Built overlay UI");
1ecdb44a 101
49815cc0 102 // MIDI devices
cc9fcf4b
MS
103 for (MidiInputDevice d : RWMidi.getInputDevices()) {
104 d.createInput(this);
105 }
106 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 107 logTime("Setup MIDI devices");
554e38ff 108
0a9f99cc 109 // Setup camera
e0cea600 110 midX = TRAILER_WIDTH/2. + 20;
0a9f99cc 111 midY = glucose.model.yMax/2;
e0cea600
MS
112 midZ = TRAILER_DEPTH/2.;
113 eyeR = -290;
0a9f99cc
MS
114 eyeA = .15;
115 eyeY = midY + 20;
116 eyeX = midX + eyeR*sin(eyeA);
117 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
118 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
119 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
120 mouseWheel(mwe.getWheelRotation());
121 }});
122
49815cc0 123 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 124 println("Hit the 'p' key to toggle Panda Board output");
39011e7e
MS
125
126 println(glucose.model.bassBox.points.size());
49815cc0
MS
127}
128
cc9fcf4b
MS
129void controllerChangeReceived(rwmidi.Controller cc) {
130 if (debugMode) {
131 println("CC: " + cc.toString());
132 }
133}
134
135void noteOnReceived(Note note) {
136 if (debugMode) {
137 println("Note On: " + note.toString());
138 }
139}
140
141void noteOffReceived(Note note) {
142 if (debugMode) {
143 println("Note Off: " + note.toString());
144 }
145}
146
49815cc0
MS
147void logTime(String evt) {
148 int now = millis();
149 println(evt + ": " + (now - lastMillis) + "ms");
150 lastMillis = now;
151}
152
153void draw() {
0a9f99cc
MS
154 // Draws the simulation and the 2D UI overlay
155 background(40);
156 color[] colors = glucose.getColors();
554e38ff
MS
157 if (debugMode) {
158 debugUI.maskColors(colors);
159 }
51d0d59a 160
0a9f99cc
MS
161 camera(
162 eyeX, eyeY, eyeZ,
163 midX, midY, midZ,
164 0, -1, 0
165 );
51d0d59a 166
51d0d59a
MS
167 noStroke();
168 fill(#141414);
87998ff3 169 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
170 fill(#070707);
171 stroke(#222222);
0a9f99cc 172 beginShape();
51d0d59a 173 vertex(0, 0, 0);
87998ff3
MS
174 vertex(TRAILER_WIDTH, 0, 0);
175 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
176 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 177 endShape();
0a9f99cc 178
51d0d59a 179 noStroke();
92c06c97 180 drawBassBox(glucose.model.bassBox);
e76480d4
MS
181 for (Speaker s : glucose.model.speakers) {
182 drawSpeaker(s);
183 }
51d0d59a
MS
184 for (Cube c : glucose.model.cubes) {
185 drawCube(c);
186 }
187
0a9f99cc
MS
188 noFill();
189 strokeWeight(2);
190 beginShape(POINTS);
191 for (Point p : glucose.model.points) {
192 stroke(colors[p.index]);
193 vertex(p.fx, p.fy, p.fz);
194 }
195 endShape();
196
197 // 2D Overlay
198 camera();
e0cea600 199 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
0a9f99cc
MS
200 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
201 ((PGraphicsOpenGL)g).endGL();
202 strokeWeight(1);
203 drawUI();
e73ef85d 204
554e38ff
MS
205 if (debugMode) {
206 debugUI.draw();
207 }
a797d019 208
e73ef85d 209 // TODO(mcslee): move into GLucose engine
79ae8245
MS
210 for (PandaDriver p : pandaBoards) {
211 p.send(colors);
e73ef85d 212 }
49815cc0
MS
213}
214
92c06c97
MS
215void drawBassBox(BassBox b) {
216 float in = .15;
e76480d4
MS
217
218 noStroke();
219 fill(#191919);
220 pushMatrix();
221 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
222 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
223 popMatrix();
224
225 noStroke();
226 fill(#393939);
92c06c97
MS
227 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);
228
39011e7e
MS
229 pushMatrix();
230 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
231 float lastOffset = 0;
232 for (float offset : BoothFloor.STRIP_OFFSETS) {
233 translate(offset - lastOffset, 0, 0);
234 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
235 lastOffset = offset;
236 }
237 popMatrix();
238
92c06c97
MS
239 pushMatrix();
240 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
241 for (int j = 0; j < 2; ++j) {
242 pushMatrix();
243 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
244 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
245 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
246 }
247 popMatrix();
248 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
249 }
250 popMatrix();
251
252 pushMatrix();
253 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
254 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
255 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
256 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
257 popMatrix();
e76480d4 258
92c06c97
MS
259}
260
51d0d59a 261void drawCube(Cube c) {
254d34c0 262 float in = .15;
e76480d4
MS
263 noStroke();
264 fill(#393939);
254d34c0 265 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
266}
267
e76480d4
MS
268void drawSpeaker(Speaker s) {
269 float in = .15;
270
271 noStroke();
272 fill(#191919);
273 pushMatrix();
274 translate(s.x, s.y, s.z);
275 rotate(s.ry / 180. * PI, 0, -1, 0);
276 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
277 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
254fbb68
MS
278 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
279
280 fill(#222222);
281 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
e76480d4
MS
282 popMatrix();
283
e76480d4
MS
284 noStroke();
285 fill(#393939);
286 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);
287}
288
289
51d0d59a
MS
290void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
291 pushMatrix();
292 translate(x, y, z);
e0cea600 293 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 294 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 295 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
296 for (int i = 0; i < 4; ++i) {
297 float wid = (i % 2 == 0) ? xd : zd;
298
299 beginShape();
300 vertex(0, 0);
301 vertex(wid, 0);
302 vertex(wid, yd);
303 vertex(wid - sw, yd);
304 vertex(wid - sw, sw);
305 vertex(0, sw);
306 endShape();
307 beginShape();
308 vertex(0, sw);
309 vertex(0, yd);
310 vertex(wid - sw, yd);
311 vertex(wid - sw, yd - sw);
312 vertex(sw, yd - sw);
313 vertex(sw, sw);
314 endShape();
315
316 translate(wid, 0, 0);
317 rotate(HALF_PI, 0, -1, 0);
318 }
319 popMatrix();
320}
321
49815cc0
MS
322void drawUI() {
323 if (uiOn) {
324 ui.draw();
325 } else {
326 ui.drawHelpTip();
327 }
328 ui.drawFPS();
329}
330
331boolean uiOn = true;
bf551144
MS
332int restoreToIndex = -1;
333
49815cc0 334void keyPressed() {
bf551144
MS
335 if (mappingMode) {
336 mappingTool.keyPressed();
337 }
3f8be614 338 switch (key) {
0e3c5542
MS
339 case '-':
340 case '_':
341 frameRate(--targetFramerate);
342 break;
343 case '=':
344 case '+':
345 frameRate(++targetFramerate);
346 break;
cc9fcf4b
MS
347 case 'd':
348 debugMode = !debugMode;
349 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 350 break;
bf551144
MS
351 case 'm':
352 mappingMode = !mappingMode;
353 if (mappingMode) {
354 LXPattern pattern = lx.getPattern();
355 for (int i = 0; i < patterns.length; ++i) {
356 if (pattern == patterns[i]) {
357 restoreToIndex = i;
358 break;
359 }
360 }
361 ui = mappingUI;
362 lx.setPatterns(new LXPattern[] { mappingTool });
363 } else {
364 ui = controlUI;
365 lx.setPatterns(patterns);
366 lx.goIndex(restoreToIndex);
367 }
368 break;
e73ef85d 369 case 'p':
79ae8245
MS
370 for (PandaDriver p : pandaBoards) {
371 p.toggle();
372 }
cc9fcf4b 373 break;
3f8be614
MS
374 case 'u':
375 uiOn = !uiOn;
376 break;
49815cc0
MS
377 }
378}
379
0a9f99cc 380int mx, my;
0a9f99cc 381void mousePressed() {
79ae8245
MS
382 ui.mousePressed();
383 if (mouseX < ui.leftPos) {
554e38ff
MS
384 if (debugMode) {
385 debugUI.mousePressed();
386 }
0a9f99cc
MS
387 mx = mouseX;
388 my = mouseY;
389 }
390}
391
392void mouseDragged() {
393 if (mouseX > ui.leftPos) {
394 ui.mouseDragged();
395 } else {
396 int dx = mouseX - mx;
397 int dy = mouseY - my;
398 mx = mouseX;
399 my = mouseY;
400 eyeA += dx*.003;
401 eyeX = midX + eyeR*sin(eyeA);
402 eyeZ = midZ + eyeR*cos(eyeA);
403 eyeY += dy;
404 }
405}
406
407void mouseReleased() {
79ae8245 408 ui.mouseReleased();
0a9f99cc 409}
bda5421d
MS
410
411void mouseWheel(int delta) {
0ba6ac44
MS
412 if (mouseX > ui.leftPos) {
413 ui.mouseWheel(delta);
414 } else {
415 eyeR = constrain(eyeR - delta, -500, -80);
416 eyeX = midX + eyeR*sin(eyeA);
417 eyeZ = midZ + eyeR*cos(eyeA);
418 }
bda5421d 419}
49815cc0 420