Strips are now like cube strips on the bass bins
[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
0a9f99cc 123
49815cc0 124 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 125 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
126}
127
cc9fcf4b
MS
128void controllerChangeReceived(rwmidi.Controller cc) {
129 if (debugMode) {
130 println("CC: " + cc.toString());
131 }
132}
133
134void noteOnReceived(Note note) {
135 if (debugMode) {
136 println("Note On: " + note.toString());
137 }
138}
139
140void noteOffReceived(Note note) {
141 if (debugMode) {
142 println("Note Off: " + note.toString());
143 }
144}
145
49815cc0
MS
146void logTime(String evt) {
147 int now = millis();
148 println(evt + ": " + (now - lastMillis) + "ms");
149 lastMillis = now;
150}
151
152void draw() {
0a9f99cc
MS
153 // Draws the simulation and the 2D UI overlay
154 background(40);
155 color[] colors = glucose.getColors();
554e38ff
MS
156 if (debugMode) {
157 debugUI.maskColors(colors);
158 }
51d0d59a 159
0a9f99cc
MS
160 camera(
161 eyeX, eyeY, eyeZ,
162 midX, midY, midZ,
163 0, -1, 0
164 );
51d0d59a 165
51d0d59a
MS
166 noStroke();
167 fill(#141414);
87998ff3 168 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
169 fill(#070707);
170 stroke(#222222);
0a9f99cc 171 beginShape();
51d0d59a 172 vertex(0, 0, 0);
87998ff3
MS
173 vertex(TRAILER_WIDTH, 0, 0);
174 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
175 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 176 endShape();
0a9f99cc 177
51d0d59a 178 noStroke();
92c06c97 179 drawBassBox(glucose.model.bassBox);
e76480d4
MS
180 for (Speaker s : glucose.model.speakers) {
181 drawSpeaker(s);
182 }
51d0d59a
MS
183 for (Cube c : glucose.model.cubes) {
184 drawCube(c);
185 }
186
0a9f99cc
MS
187 noFill();
188 strokeWeight(2);
189 beginShape(POINTS);
190 for (Point p : glucose.model.points) {
191 stroke(colors[p.index]);
192 vertex(p.fx, p.fy, p.fz);
193 }
194 endShape();
195
196 // 2D Overlay
197 camera();
e0cea600 198 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
0a9f99cc
MS
199 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
200 ((PGraphicsOpenGL)g).endGL();
201 strokeWeight(1);
202 drawUI();
e73ef85d 203
554e38ff
MS
204 if (debugMode) {
205 debugUI.draw();
206 }
a797d019 207
e73ef85d 208 // TODO(mcslee): move into GLucose engine
79ae8245
MS
209 for (PandaDriver p : pandaBoards) {
210 p.send(colors);
e73ef85d 211 }
49815cc0
MS
212}
213
92c06c97
MS
214void drawBassBox(BassBox b) {
215 float in = .15;
e76480d4
MS
216
217 noStroke();
218 fill(#191919);
219 pushMatrix();
220 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
221 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
222 popMatrix();
223
224 noStroke();
225 fill(#393939);
92c06c97
MS
226 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);
227
228 pushMatrix();
229 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
230 for (int j = 0; j < 2; ++j) {
231 pushMatrix();
232 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
233 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
234 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
235 }
236 popMatrix();
237 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
238 }
239 popMatrix();
240
241 pushMatrix();
242 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
243 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
244 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
245 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
246 popMatrix();
e76480d4 247
92c06c97
MS
248}
249
51d0d59a 250void drawCube(Cube c) {
254d34c0 251 float in = .15;
e76480d4
MS
252 noStroke();
253 fill(#393939);
254d34c0 254 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
255}
256
e76480d4
MS
257void drawSpeaker(Speaker s) {
258 float in = .15;
259
260 noStroke();
261 fill(#191919);
262 pushMatrix();
263 translate(s.x, s.y, s.z);
264 rotate(s.ry / 180. * PI, 0, -1, 0);
265 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
266 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
267 popMatrix();
268
269
270 noStroke();
271 fill(#393939);
272 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);
273}
274
275
51d0d59a
MS
276void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
277 pushMatrix();
278 translate(x, y, z);
e0cea600 279 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 280 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 281 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
282 for (int i = 0; i < 4; ++i) {
283 float wid = (i % 2 == 0) ? xd : zd;
284
285 beginShape();
286 vertex(0, 0);
287 vertex(wid, 0);
288 vertex(wid, yd);
289 vertex(wid - sw, yd);
290 vertex(wid - sw, sw);
291 vertex(0, sw);
292 endShape();
293 beginShape();
294 vertex(0, sw);
295 vertex(0, yd);
296 vertex(wid - sw, yd);
297 vertex(wid - sw, yd - sw);
298 vertex(sw, yd - sw);
299 vertex(sw, sw);
300 endShape();
301
302 translate(wid, 0, 0);
303 rotate(HALF_PI, 0, -1, 0);
304 }
305 popMatrix();
306}
307
49815cc0
MS
308void drawUI() {
309 if (uiOn) {
310 ui.draw();
311 } else {
312 ui.drawHelpTip();
313 }
314 ui.drawFPS();
315}
316
317boolean uiOn = true;
bf551144
MS
318int restoreToIndex = -1;
319
49815cc0 320void keyPressed() {
bf551144
MS
321 if (mappingMode) {
322 mappingTool.keyPressed();
323 }
3f8be614 324 switch (key) {
0e3c5542
MS
325 case '-':
326 case '_':
327 frameRate(--targetFramerate);
328 break;
329 case '=':
330 case '+':
331 frameRate(++targetFramerate);
332 break;
cc9fcf4b
MS
333 case 'd':
334 debugMode = !debugMode;
335 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 336 break;
bf551144
MS
337 case 'm':
338 mappingMode = !mappingMode;
339 if (mappingMode) {
340 LXPattern pattern = lx.getPattern();
341 for (int i = 0; i < patterns.length; ++i) {
342 if (pattern == patterns[i]) {
343 restoreToIndex = i;
344 break;
345 }
346 }
347 ui = mappingUI;
348 lx.setPatterns(new LXPattern[] { mappingTool });
349 } else {
350 ui = controlUI;
351 lx.setPatterns(patterns);
352 lx.goIndex(restoreToIndex);
353 }
354 break;
e73ef85d 355 case 'p':
79ae8245
MS
356 for (PandaDriver p : pandaBoards) {
357 p.toggle();
358 }
cc9fcf4b 359 break;
3f8be614
MS
360 case 'u':
361 uiOn = !uiOn;
362 break;
49815cc0
MS
363 }
364}
365
0a9f99cc 366int mx, my;
0a9f99cc 367void mousePressed() {
79ae8245
MS
368 ui.mousePressed();
369 if (mouseX < ui.leftPos) {
554e38ff
MS
370 if (debugMode) {
371 debugUI.mousePressed();
372 }
0a9f99cc
MS
373 mx = mouseX;
374 my = mouseY;
375 }
376}
377
378void mouseDragged() {
379 if (mouseX > ui.leftPos) {
380 ui.mouseDragged();
381 } else {
382 int dx = mouseX - mx;
383 int dy = mouseY - my;
384 mx = mouseX;
385 my = mouseY;
386 eyeA += dx*.003;
387 eyeX = midX + eyeR*sin(eyeA);
388 eyeZ = midZ + eyeR*cos(eyeA);
389 eyeY += dy;
390 }
391}
392
393void mouseReleased() {
79ae8245 394 ui.mouseReleased();
0a9f99cc 395}
bda5421d
MS
396
397void mouseWheel(int delta) {
0ba6ac44
MS
398 if (mouseX > ui.leftPos) {
399 ui.mouseWheel(delta);
400 } else {
401 eyeR = constrain(eyeR - delta, -500, -80);
402 eyeX = midX + eyeR*sin(eyeA);
403 eyeZ = midZ + eyeR*cos(eyeA);
404 }
bda5421d 405}
49815cc0 406