New strips on the DJ booth floor
[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
44 int startMillis, lastMillis;
45 GLucose glucose;
46 HeronLX lx;
47 MappingTool mappingTool;
48 LXPattern[] patterns;
49 LXTransition[] transitions;
50 LXEffect[] effects;
51 OverlayUI ui;
52 ControlUI controlUI;
53 MappingUI mappingUI;
54 PandaDriver[] pandaBoards;
55 boolean mappingMode = false;
56 boolean debugMode = false;
57 DebugUI debugUI;
58
59 // Camera variables
60 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
61
62 void setup() {
63 startMillis = lastMillis = millis();
64
65 // Initialize the Processing graphics environment
66 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
67 frameRate(targetFramerate);
68 noSmooth();
69 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
70 logTime("Created viewport");
71
72 // Create the GLucose engine to run the cubes
73 glucose = new GLucose(this, buildModel());
74 lx = glucose.lx;
75 lx.enableKeyboardTempo();
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");
83 glucose.setTransitions(transitions = transitions(glucose));
84 logTime("Built transitions");
85
86 // Build output driver
87 PandaMapping[] pandaMappings = buildPandaList();
88 pandaBoards = new PandaDriver[pandaMappings.length];
89 int pbi = 0;
90 for (PandaMapping pm : pandaMappings) {
91 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
92 }
93 mappingTool = new MappingTool(glucose, pandaMappings);
94 logTime("Built PandaDriver");
95
96 // Build overlay UI
97 ui = controlUI = new ControlUI();
98 mappingUI = new MappingUI(mappingTool);
99 debugUI = new DebugUI(pandaMappings);
100 logTime("Built overlay UI");
101
102 // MIDI devices
103 for (MidiInputDevice d : RWMidi.getInputDevices()) {
104 d.createInput(this);
105 }
106 SCMidiDevices.initializeStandardDevices(glucose);
107 logTime("Setup MIDI devices");
108
109 // Setup camera
110 midX = TRAILER_WIDTH/2. + 20;
111 midY = glucose.model.yMax/2;
112 midZ = TRAILER_DEPTH/2.;
113 eyeR = -290;
114 eyeA = .15;
115 eyeY = midY + 20;
116 eyeX = midX + eyeR*sin(eyeA);
117 eyeZ = midZ + eyeR*cos(eyeA);
118 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
119 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
120 mouseWheel(mwe.getWheelRotation());
121 }});
122
123 println("Total setup: " + (millis() - startMillis) + "ms");
124 println("Hit the 'p' key to toggle Panda Board output");
125
126 println(glucose.model.bassBox.points.size());
127 }
128
129 void controllerChangeReceived(rwmidi.Controller cc) {
130 if (debugMode) {
131 println("CC: " + cc.toString());
132 }
133 }
134
135 void noteOnReceived(Note note) {
136 if (debugMode) {
137 println("Note On: " + note.toString());
138 }
139 }
140
141 void noteOffReceived(Note note) {
142 if (debugMode) {
143 println("Note Off: " + note.toString());
144 }
145 }
146
147 void logTime(String evt) {
148 int now = millis();
149 println(evt + ": " + (now - lastMillis) + "ms");
150 lastMillis = now;
151 }
152
153 void draw() {
154 // Draws the simulation and the 2D UI overlay
155 background(40);
156 color[] colors = glucose.getColors();
157 if (debugMode) {
158 debugUI.maskColors(colors);
159 }
160
161 camera(
162 eyeX, eyeY, eyeZ,
163 midX, midY, midZ,
164 0, -1, 0
165 );
166
167 noStroke();
168 fill(#141414);
169 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
170 fill(#070707);
171 stroke(#222222);
172 beginShape();
173 vertex(0, 0, 0);
174 vertex(TRAILER_WIDTH, 0, 0);
175 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
176 vertex(0, 0, TRAILER_DEPTH);
177 endShape();
178
179 noStroke();
180 drawBassBox(glucose.model.bassBox);
181 for (Speaker s : glucose.model.speakers) {
182 drawSpeaker(s);
183 }
184 for (Cube c : glucose.model.cubes) {
185 drawCube(c);
186 }
187
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();
199 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
200 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
201 ((PGraphicsOpenGL)g).endGL();
202 strokeWeight(1);
203 drawUI();
204
205 if (debugMode) {
206 debugUI.draw();
207 }
208
209 // TODO(mcslee): move into GLucose engine
210 for (PandaDriver p : pandaBoards) {
211 p.send(colors);
212 }
213 }
214
215 void drawBassBox(BassBox b) {
216 float in = .15;
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);
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
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
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();
258
259 }
260
261 void drawCube(Cube c) {
262 float in = .15;
263 noStroke();
264 fill(#393939);
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);
266 }
267
268 void 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);
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);
282 popMatrix();
283
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
290 void 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);
293 rotate(rx / 180. * PI, -1, 0, 0);
294 rotate(ry / 180. * PI, 0, -1, 0);
295 rotate(rz / 180. * PI, 0, 0, -1);
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
322 void drawUI() {
323 if (uiOn) {
324 ui.draw();
325 } else {
326 ui.drawHelpTip();
327 }
328 ui.drawFPS();
329 }
330
331 boolean uiOn = true;
332 int restoreToIndex = -1;
333
334 void keyPressed() {
335 if (mappingMode) {
336 mappingTool.keyPressed();
337 }
338 switch (key) {
339 case '-':
340 case '_':
341 frameRate(--targetFramerate);
342 break;
343 case '=':
344 case '+':
345 frameRate(++targetFramerate);
346 break;
347 case 'd':
348 debugMode = !debugMode;
349 println("Debug output: " + (debugMode ? "ON" : "OFF"));
350 break;
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;
369 case 'p':
370 for (PandaDriver p : pandaBoards) {
371 p.toggle();
372 }
373 break;
374 case 'u':
375 uiOn = !uiOn;
376 break;
377 }
378 }
379
380 int mx, my;
381 void mousePressed() {
382 ui.mousePressed();
383 if (mouseX < ui.leftPos) {
384 if (debugMode) {
385 debugUI.mousePressed();
386 }
387 mx = mouseX;
388 my = mouseY;
389 }
390 }
391
392 void 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
407 void mouseReleased() {
408 ui.mouseReleased();
409 }
410
411 void mouseWheel(int delta) {
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 }
419 }
420