Tweaking strip stuff and some anims
[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
124 println("Total setup: " + (millis() - startMillis) + "ms");
125 println("Hit the 'p' key to toggle Panda Board output");
126 }
127
128 void controllerChangeReceived(rwmidi.Controller cc) {
129 if (debugMode) {
130 println("CC: " + cc.toString());
131 }
132 }
133
134 void noteOnReceived(Note note) {
135 if (debugMode) {
136 println("Note On: " + note.toString());
137 }
138 }
139
140 void noteOffReceived(Note note) {
141 if (debugMode) {
142 println("Note Off: " + note.toString());
143 }
144 }
145
146 void logTime(String evt) {
147 int now = millis();
148 println(evt + ": " + (now - lastMillis) + "ms");
149 lastMillis = now;
150 }
151
152 void draw() {
153 // Draws the simulation and the 2D UI overlay
154 background(40);
155 color[] colors = glucose.getColors();
156 if (debugMode) {
157 debugUI.maskColors(colors);
158 }
159
160 camera(
161 eyeX, eyeY, eyeZ,
162 midX, midY, midZ,
163 0, -1, 0
164 );
165
166 noStroke();
167 fill(#141414);
168 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
169 fill(#070707);
170 stroke(#222222);
171 beginShape();
172 vertex(0, 0, 0);
173 vertex(TRAILER_WIDTH, 0, 0);
174 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
175 vertex(0, 0, TRAILER_DEPTH);
176 endShape();
177
178 noStroke();
179 drawBassBox(glucose.model.bassBox);
180 for (Speaker s : glucose.model.speakers) {
181 drawSpeaker(s);
182 }
183 for (Cube c : glucose.model.cubes) {
184 drawCube(c);
185 }
186
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();
198 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
199 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
200 ((PGraphicsOpenGL)g).endGL();
201 strokeWeight(1);
202 drawUI();
203
204 if (debugMode) {
205 debugUI.draw();
206 }
207
208 // TODO(mcslee): move into GLucose engine
209 for (PandaDriver p : pandaBoards) {
210 p.send(colors);
211 }
212 }
213
214 void drawBassBox(BassBox b) {
215 float in = .15;
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);
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();
247
248 }
249
250 void drawCube(Cube c) {
251 float in = .15;
252 noStroke();
253 fill(#393939);
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);
255 }
256
257 void 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 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
268
269 fill(#222222);
270 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
271 popMatrix();
272
273 noStroke();
274 fill(#393939);
275 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);
276 }
277
278
279 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
280 pushMatrix();
281 translate(x, y, z);
282 rotate(rx / 180. * PI, -1, 0, 0);
283 rotate(ry / 180. * PI, 0, -1, 0);
284 rotate(rz / 180. * PI, 0, 0, -1);
285 for (int i = 0; i < 4; ++i) {
286 float wid = (i % 2 == 0) ? xd : zd;
287
288 beginShape();
289 vertex(0, 0);
290 vertex(wid, 0);
291 vertex(wid, yd);
292 vertex(wid - sw, yd);
293 vertex(wid - sw, sw);
294 vertex(0, sw);
295 endShape();
296 beginShape();
297 vertex(0, sw);
298 vertex(0, yd);
299 vertex(wid - sw, yd);
300 vertex(wid - sw, yd - sw);
301 vertex(sw, yd - sw);
302 vertex(sw, sw);
303 endShape();
304
305 translate(wid, 0, 0);
306 rotate(HALF_PI, 0, -1, 0);
307 }
308 popMatrix();
309 }
310
311 void drawUI() {
312 if (uiOn) {
313 ui.draw();
314 } else {
315 ui.drawHelpTip();
316 }
317 ui.drawFPS();
318 }
319
320 boolean uiOn = true;
321 int restoreToIndex = -1;
322
323 void keyPressed() {
324 if (mappingMode) {
325 mappingTool.keyPressed();
326 }
327 switch (key) {
328 case '-':
329 case '_':
330 frameRate(--targetFramerate);
331 break;
332 case '=':
333 case '+':
334 frameRate(++targetFramerate);
335 break;
336 case 'd':
337 debugMode = !debugMode;
338 println("Debug output: " + (debugMode ? "ON" : "OFF"));
339 break;
340 case 'm':
341 mappingMode = !mappingMode;
342 if (mappingMode) {
343 LXPattern pattern = lx.getPattern();
344 for (int i = 0; i < patterns.length; ++i) {
345 if (pattern == patterns[i]) {
346 restoreToIndex = i;
347 break;
348 }
349 }
350 ui = mappingUI;
351 lx.setPatterns(new LXPattern[] { mappingTool });
352 } else {
353 ui = controlUI;
354 lx.setPatterns(patterns);
355 lx.goIndex(restoreToIndex);
356 }
357 break;
358 case 'p':
359 for (PandaDriver p : pandaBoards) {
360 p.toggle();
361 }
362 break;
363 case 'u':
364 uiOn = !uiOn;
365 break;
366 }
367 }
368
369 int mx, my;
370 void mousePressed() {
371 ui.mousePressed();
372 if (mouseX < ui.leftPos) {
373 if (debugMode) {
374 debugUI.mousePressed();
375 }
376 mx = mouseX;
377 my = mouseY;
378 }
379 }
380
381 void mouseDragged() {
382 if (mouseX > ui.leftPos) {
383 ui.mouseDragged();
384 } else {
385 int dx = mouseX - mx;
386 int dy = mouseY - my;
387 mx = mouseX;
388 my = mouseY;
389 eyeA += dx*.003;
390 eyeX = midX + eyeR*sin(eyeA);
391 eyeZ = midZ + eyeR*cos(eyeA);
392 eyeY += dy;
393 }
394 }
395
396 void mouseReleased() {
397 ui.mouseReleased();
398 }
399
400 void mouseWheel(int delta) {
401 if (mouseX > ui.leftPos) {
402 ui.mouseWheel(delta);
403 } else {
404 eyeR = constrain(eyeR - delta, -500, -80);
405 eyeX = midX + eyeR*sin(eyeA);
406 eyeZ = midZ + eyeR*cos(eyeA);
407 }
408 }
409