Add a missing noStroke call
[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
e28f168c 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();
ab77005f
MS
179// drawBassBox(glucose.model.bassBox);
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);
922def98
MS
190 // TODO(mcslee): restore when bassBox/speakers are right again
191 // for (Point p : glucose.model.points) {
192 for (Cube cube : glucose.model.cubes) {
193 for (Point p : cube.points) {
194 stroke(colors[p.index]);
195 vertex(p.fx, p.fy, p.fz);
196 }
0a9f99cc
MS
197 }
198 endShape();
199
200 // 2D Overlay
201 camera();
e0cea600 202 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
0a9f99cc
MS
203 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
204 ((PGraphicsOpenGL)g).endGL();
205 strokeWeight(1);
206 drawUI();
e73ef85d 207
554e38ff
MS
208 if (debugMode) {
209 debugUI.draw();
210 }
a797d019 211
6702151a
MS
212 // Gamma correction here. Apply a cubic to the brightness
213 // for better representation of dynamic range
214 for (int i = 0; i < colors.length; ++i) {
215 float b = brightness(colors[i]) / 100.f;
216 colors[i] = color(
217 hue(colors[i]),
218 saturation(colors[i]),
219 (b*b*b) * 100.
220 );
221 }
e7f14d4d 222
e73ef85d 223 // TODO(mcslee): move into GLucose engine
79ae8245 224 for (PandaDriver p : pandaBoards) {
b58e5a1d 225 p.send(colors);
e73ef85d 226 }
49815cc0
MS
227}
228
92c06c97
MS
229void drawBassBox(BassBox b) {
230 float in = .15;
e76480d4
MS
231
232 noStroke();
233 fill(#191919);
234 pushMatrix();
235 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
236 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
237 popMatrix();
238
239 noStroke();
240 fill(#393939);
92c06c97
MS
241 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);
242
39011e7e
MS
243 pushMatrix();
244 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
245 float lastOffset = 0;
246 for (float offset : BoothFloor.STRIP_OFFSETS) {
247 translate(offset - lastOffset, 0, 0);
248 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
249 lastOffset = offset;
250 }
251 popMatrix();
252
92c06c97
MS
253 pushMatrix();
254 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
255 for (int j = 0; j < 2; ++j) {
256 pushMatrix();
257 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
258 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
259 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
260 }
261 popMatrix();
262 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
263 }
264 popMatrix();
265
266 pushMatrix();
267 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
268 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
269 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
270 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
ab77005f 271 popMatrix();
e76480d4 272
92c06c97
MS
273}
274
51d0d59a 275void drawCube(Cube c) {
254d34c0 276 float in = .15;
e76480d4
MS
277 noStroke();
278 fill(#393939);
254d34c0 279 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
280}
281
e76480d4
MS
282void drawSpeaker(Speaker s) {
283 float in = .15;
284
285 noStroke();
286 fill(#191919);
287 pushMatrix();
288 translate(s.x, s.y, s.z);
289 rotate(s.ry / 180. * PI, 0, -1, 0);
290 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
291 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
254fbb68
MS
292 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
293
294 fill(#222222);
295 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
e76480d4
MS
296 popMatrix();
297
e76480d4
MS
298 noStroke();
299 fill(#393939);
300 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);
ab77005f 301}
e76480d4 302
51d0d59a
MS
303void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
304 pushMatrix();
305 translate(x, y, z);
e0cea600 306 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 307 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 308 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
309 for (int i = 0; i < 4; ++i) {
310 float wid = (i % 2 == 0) ? xd : zd;
311
312 beginShape();
313 vertex(0, 0);
314 vertex(wid, 0);
315 vertex(wid, yd);
316 vertex(wid - sw, yd);
317 vertex(wid - sw, sw);
318 vertex(0, sw);
319 endShape();
320 beginShape();
321 vertex(0, sw);
322 vertex(0, yd);
323 vertex(wid - sw, yd);
324 vertex(wid - sw, yd - sw);
325 vertex(sw, yd - sw);
326 vertex(sw, sw);
327 endShape();
328
329 translate(wid, 0, 0);
330 rotate(HALF_PI, 0, -1, 0);
331 }
332 popMatrix();
333}
334
49815cc0
MS
335void drawUI() {
336 if (uiOn) {
337 ui.draw();
338 } else {
339 ui.drawHelpTip();
340 }
341 ui.drawFPS();
e28f168c 342 ui.drawDanText();
49815cc0
MS
343}
344
345boolean uiOn = true;
bf551144
MS
346int restoreToIndex = -1;
347
e28f168c 348boolean doDual = false;
ab77005f 349
49815cc0 350void keyPressed() {
bf551144
MS
351 if (mappingMode) {
352 mappingTool.keyPressed();
353 }
3f8be614 354 switch (key) {
e28f168c
AG
355 case 'w':
356 doDual = !doDual;
357 break;
0e3c5542
MS
358 case '-':
359 case '_':
360 frameRate(--targetFramerate);
361 break;
362 case '=':
363 case '+':
364 frameRate(++targetFramerate);
365 break;
cc9fcf4b
MS
366 case 'd':
367 debugMode = !debugMode;
368 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 369 break;
bf551144
MS
370 case 'm':
371 mappingMode = !mappingMode;
372 if (mappingMode) {
373 LXPattern pattern = lx.getPattern();
374 for (int i = 0; i < patterns.length; ++i) {
375 if (pattern == patterns[i]) {
376 restoreToIndex = i;
377 break;
378 }
379 }
380 ui = mappingUI;
381 lx.setPatterns(new LXPattern[] { mappingTool });
382 } else {
383 ui = controlUI;
384 lx.setPatterns(patterns);
385 lx.goIndex(restoreToIndex);
386 }
387 break;
e73ef85d 388 case 'p':
79ae8245
MS
389 for (PandaDriver p : pandaBoards) {
390 p.toggle();
391 }
cc9fcf4b 392 break;
3f8be614
MS
393 case 'u':
394 uiOn = !uiOn;
395 break;
49815cc0
MS
396 }
397}
398
0a9f99cc 399int mx, my;
0a9f99cc 400void mousePressed() {
79ae8245
MS
401 ui.mousePressed();
402 if (mouseX < ui.leftPos) {
554e38ff
MS
403 if (debugMode) {
404 debugUI.mousePressed();
405 }
0a9f99cc
MS
406 mx = mouseX;
407 my = mouseY;
408 }
409}
410
411void mouseDragged() {
412 if (mouseX > ui.leftPos) {
413 ui.mouseDragged();
414 } else {
415 int dx = mouseX - mx;
416 int dy = mouseY - my;
417 mx = mouseX;
418 my = mouseY;
419 eyeA += dx*.003;
420 eyeX = midX + eyeR*sin(eyeA);
421 eyeZ = midZ + eyeR*cos(eyeA);
422 eyeY += dy;
423 }
424}
425
426void mouseReleased() {
79ae8245 427 ui.mouseReleased();
0a9f99cc 428}
bda5421d
MS
429
430void mouseWheel(int delta) {
0ba6ac44
MS
431 if (mouseX > ui.leftPos) {
432 ui.mouseWheel(delta);
433 } else {
434 eyeR = constrain(eyeR - delta, -500, -80);
435 eyeX = midX + eyeR*sin(eyeA);
436 eyeZ = midZ + eyeR*cos(eyeA);
437 }
bda5421d 438}