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