Update panda output code to be a touch more sane
[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 final float TRAILER_WIDTH = 240;
38 final float TRAILER_DEPTH = 97;
39 final float TRAILER_HEIGHT = 33;
40
41 final float BASS_WIDTH = 124;
42 final float BASS_HEIGHT = 31.5;
43 final float BASS_DEPTH = 66;
44 final float BASS_X = (TRAILER_WIDTH - BASS_WIDTH) / 2.;
45 final float BASS_Z = (TRAILER_DEPTH - BASS_DEPTH) / 2.;
46
47 int targetFramerate = 60;
48
49 int startMillis, lastMillis;
50 SCMapping mapping;
51 GLucose glucose;
52 HeronLX lx;
53 MappingTool mappingTool;
54 LXPattern[] patterns;
55 LXTransition[] transitions;
56 LXEffect[] effects;
57 OverlayUI ui;
58 ControlUI controlUI;
59 MappingUI mappingUI;
60 PandaDriver[] pandaBoards;
61 boolean mappingMode = false;
62 boolean debugMode = false;
63 DebugUI debugUI;
64
65 // Camera variables
66 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
67
68 void setup() {
69 startMillis = lastMillis = millis();
70
71 // Initialize the Processing graphics environment
72 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
73 frameRate(targetFramerate);
74 noSmooth();
75 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
76 logTime("Created viewport");
77
78 // Create the GLucose engine to run the cubes
79 mapping = new SCMapping();
80 glucose = new GLucose(this, mapping);
81 lx = glucose.lx;
82 lx.enableKeyboardTempo();
83 logTime("Built GLucose engine");
84
85 // Set the patterns
86 glucose.lx.setPatterns(patterns = patterns(glucose));
87 logTime("Built patterns");
88 glucose.lx.addEffects(effects = effects(glucose));
89 logTime("Built effects");
90 glucose.setTransitions(transitions = transitions(glucose));
91 logTime("Built transitions");
92
93 // Build output driver
94 PandaMapping[] pandaMappings = mapping.buildPandaList();
95 pandaBoards = new PandaDriver[pandaMappings.length];
96 int pbi = 0;
97 for (PandaMapping pm : pandaMappings) {
98 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
99 }
100 mappingTool = new MappingTool(glucose, pandaMappings);
101 logTime("Built PandaDriver");
102
103 // Build overlay UI
104 ui = controlUI = new ControlUI();
105 mappingUI = new MappingUI(mappingTool);
106 debugUI = new DebugUI(pandaMappings);
107 logTime("Built overlay UI");
108
109 // MIDI devices
110 for (MidiInputDevice d : RWMidi.getInputDevices()) {
111 d.createInput(this);
112 }
113 SCMidiDevices.initializeStandardDevices(glucose);
114 logTime("Setup MIDI devices");
115
116 // Setup camera
117 midX = TRAILER_WIDTH/2. + 20;
118 midY = glucose.model.yMax/2;
119 midZ = TRAILER_DEPTH/2.;
120 eyeR = -290;
121 eyeA = .15;
122 eyeY = midY + 20;
123 eyeX = midX + eyeR*sin(eyeA);
124 eyeZ = midZ + eyeR*cos(eyeA);
125 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
126 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
127 mouseWheel(mwe.getWheelRotation());
128 }});
129
130
131 println("Total setup: " + (millis() - startMillis) + "ms");
132 println("Hit the 'p' key to toggle Panda Board output");
133 }
134
135 void controllerChangeReceived(rwmidi.Controller cc) {
136 if (debugMode) {
137 println("CC: " + cc.toString());
138 }
139 }
140
141 void noteOnReceived(Note note) {
142 if (debugMode) {
143 println("Note On: " + note.toString());
144 }
145 }
146
147 void noteOffReceived(Note note) {
148 if (debugMode) {
149 println("Note Off: " + note.toString());
150 }
151 }
152
153 void logTime(String evt) {
154 int now = millis();
155 println(evt + ": " + (now - lastMillis) + "ms");
156 lastMillis = now;
157 }
158
159 void draw() {
160 // Draws the simulation and the 2D UI overlay
161 background(40);
162 color[] colors = glucose.getColors();
163 if (debugMode) {
164 debugUI.maskColors(colors);
165 }
166
167 camera(
168 eyeX, eyeY, eyeZ,
169 midX, midY, midZ,
170 0, -1, 0
171 );
172
173 noStroke();
174 fill(#141414);
175 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
176 fill(#070707);
177 stroke(#222222);
178 beginShape();
179 vertex(0, 0, 0);
180 vertex(TRAILER_WIDTH, 0, 0);
181 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
182 vertex(0, 0, TRAILER_DEPTH);
183 endShape();
184
185 noStroke();
186 fill(#292929);
187 drawBox(BASS_X, 0, BASS_Z, 0, 0, 0, BASS_WIDTH, BASS_HEIGHT, BASS_DEPTH, Cube.CHANNEL_WIDTH);
188 for (Cube c : glucose.model.cubes) {
189 drawCube(c);
190 }
191
192 noFill();
193 strokeWeight(2);
194 beginShape(POINTS);
195 for (Point p : glucose.model.points) {
196 stroke(colors[p.index]);
197 vertex(p.fx, p.fy, p.fz);
198 // println(p.fx + ":" + p.fy + ":" + p.fz);
199 }
200 endShape();
201
202 // 2D Overlay
203 camera();
204 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
205 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
206 ((PGraphicsOpenGL)g).endGL();
207 strokeWeight(1);
208 drawUI();
209
210 if (debugMode) {
211 debugUI.draw();
212 }
213
214 // TODO(mcslee): move into GLucose engine
215 for (PandaDriver p : pandaBoards) {
216 p.send(colors);
217 }
218 }
219
220 void drawCube(Cube c) {
221 float in = .15;
222 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);
223 }
224
225 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
226 pushMatrix();
227 translate(x, y, z);
228 rotate(rx / 180. * PI, -1, 0, 0);
229 rotate(ry / 180. * PI, 0, -1, 0);
230 rotate(rz / 180. * PI, 0, 0, -1);
231 for (int i = 0; i < 4; ++i) {
232 float wid = (i % 2 == 0) ? xd : zd;
233
234 beginShape();
235 vertex(0, 0);
236 vertex(wid, 0);
237 vertex(wid, yd);
238 vertex(wid - sw, yd);
239 vertex(wid - sw, sw);
240 vertex(0, sw);
241 endShape();
242 beginShape();
243 vertex(0, sw);
244 vertex(0, yd);
245 vertex(wid - sw, yd);
246 vertex(wid - sw, yd - sw);
247 vertex(sw, yd - sw);
248 vertex(sw, sw);
249 endShape();
250
251 translate(wid, 0, 0);
252 rotate(HALF_PI, 0, -1, 0);
253 }
254 popMatrix();
255 }
256
257 void drawUI() {
258 if (uiOn) {
259 ui.draw();
260 } else {
261 ui.drawHelpTip();
262 }
263 ui.drawFPS();
264 }
265
266 boolean uiOn = true;
267 int restoreToIndex = -1;
268
269 void keyPressed() {
270 if (mappingMode) {
271 mappingTool.keyPressed();
272 }
273 switch (key) {
274 case '-':
275 case '_':
276 frameRate(--targetFramerate);
277 break;
278 case '=':
279 case '+':
280 frameRate(++targetFramerate);
281 break;
282 case 'd':
283 debugMode = !debugMode;
284 println("Debug output: " + (debugMode ? "ON" : "OFF"));
285 break;
286 case 'm':
287 mappingMode = !mappingMode;
288 if (mappingMode) {
289 LXPattern pattern = lx.getPattern();
290 for (int i = 0; i < patterns.length; ++i) {
291 if (pattern == patterns[i]) {
292 restoreToIndex = i;
293 break;
294 }
295 }
296 ui = mappingUI;
297 lx.setPatterns(new LXPattern[] { mappingTool });
298 } else {
299 ui = controlUI;
300 lx.setPatterns(patterns);
301 lx.goIndex(restoreToIndex);
302 }
303 break;
304 case 'p':
305 for (PandaDriver p : pandaBoards) {
306 p.toggle();
307 }
308 break;
309 case 'u':
310 uiOn = !uiOn;
311 break;
312 }
313 }
314
315 int mx, my;
316 void mousePressed() {
317 ui.mousePressed();
318 if (mouseX < ui.leftPos) {
319 if (debugMode) {
320 debugUI.mousePressed();
321 }
322 mx = mouseX;
323 my = mouseY;
324 }
325 }
326
327 void mouseDragged() {
328 if (mouseX > ui.leftPos) {
329 ui.mouseDragged();
330 } else {
331 int dx = mouseX - mx;
332 int dy = mouseY - my;
333 mx = mouseX;
334 my = mouseY;
335 eyeA += dx*.003;
336 eyeX = midX + eyeR*sin(eyeA);
337 eyeZ = midZ + eyeR*cos(eyeA);
338 eyeY += dy;
339 }
340 }
341
342 void mouseReleased() {
343 ui.mouseReleased();
344 }
345
346 void mouseWheel(int delta) {
347 if (mouseX > ui.leftPos) {
348 ui.mouseWheel(delta);
349 } else {
350 eyeR = constrain(eyeR - delta, -500, -80);
351 eyeX = midX + eyeR*sin(eyeA);
352 eyeZ = midZ + eyeR*cos(eyeA);
353 }
354 }
355