Clean up some stuff now that we know each pandaboard has fixed # of channels
[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
87998ff3
MS
37final float TRAILER_WIDTH = 240;
38final float TRAILER_DEPTH = 97;
39final float TRAILER_HEIGHT = 33;
40
41final float BASS_WIDTH = 124;
42final float BASS_HEIGHT = 31.5;
43final float BASS_DEPTH = 66;
44final float BASS_X = (TRAILER_WIDTH - BASS_WIDTH) / 2.;
45final float BASS_Z = (TRAILER_DEPTH - BASS_DEPTH) / 2.;
46
51d0d59a 47int targetFramerate = 60;
49815cc0
MS
48
49int startMillis, lastMillis;
45f43cc2 50SCMapping mapping;
49815cc0
MS
51GLucose glucose;
52HeronLX lx;
bf551144 53MappingTool mappingTool;
49815cc0
MS
54LXPattern[] patterns;
55LXTransition[] transitions;
56LXEffect[] effects;
57OverlayUI ui;
bf551144
MS
58ControlUI controlUI;
59MappingUI mappingUI;
79ae8245 60PandaDriver[] pandaBoards;
bf551144 61boolean mappingMode = false;
cc9fcf4b 62boolean debugMode = false;
554e38ff 63DebugUI debugUI;
cc9fcf4b 64
0a9f99cc 65// Camera variables
bda5421d 66float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 67
49815cc0
MS
68void setup() {
69 startMillis = lastMillis = millis();
70
71 // Initialize the Processing graphics environment
72 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 73 frameRate(targetFramerate);
3f8be614 74 noSmooth();
49815cc0
MS
75 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
76 logTime("Created viewport");
77
78 // Create the GLucose engine to run the cubes
45f43cc2
MS
79 mapping = new SCMapping();
80 glucose = new GLucose(this, mapping);
49815cc0 81 lx = glucose.lx;
cc9fcf4b 82 lx.enableKeyboardTempo();
49815cc0
MS
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");
cc9fcf4b 90 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 91 logTime("Built transitions");
e73ef85d
MS
92
93 // Build output driver
45f43cc2
MS
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.channelList);
99 }
100 mappingTool = new MappingTool(glucose, pandaMappings);
101 logTime("Built PandaDriver");
49815cc0
MS
102
103 // Build overlay UI
bf551144
MS
104 ui = controlUI = new ControlUI();
105 mappingUI = new MappingUI(mappingTool);
45f43cc2 106 debugUI = new DebugUI(pandaMappings);
49815cc0 107 logTime("Built overlay UI");
1ecdb44a 108
49815cc0 109 // MIDI devices
cc9fcf4b
MS
110 for (MidiInputDevice d : RWMidi.getInputDevices()) {
111 d.createInput(this);
112 }
113 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 114 logTime("Setup MIDI devices");
554e38ff 115
0a9f99cc 116 // Setup camera
e0cea600 117 midX = TRAILER_WIDTH/2. + 20;
0a9f99cc 118 midY = glucose.model.yMax/2;
e0cea600
MS
119 midZ = TRAILER_DEPTH/2.;
120 eyeR = -290;
0a9f99cc
MS
121 eyeA = .15;
122 eyeY = midY + 20;
123 eyeX = midX + eyeR*sin(eyeA);
124 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
125 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
126 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
127 mouseWheel(mwe.getWheelRotation());
128 }});
129
0a9f99cc 130
49815cc0 131 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 132 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
133}
134
cc9fcf4b
MS
135void controllerChangeReceived(rwmidi.Controller cc) {
136 if (debugMode) {
137 println("CC: " + cc.toString());
138 }
139}
140
141void noteOnReceived(Note note) {
142 if (debugMode) {
143 println("Note On: " + note.toString());
144 }
145}
146
147void noteOffReceived(Note note) {
148 if (debugMode) {
149 println("Note Off: " + note.toString());
150 }
151}
152
49815cc0
MS
153void logTime(String evt) {
154 int now = millis();
155 println(evt + ": " + (now - lastMillis) + "ms");
156 lastMillis = now;
157}
158
159void draw() {
0a9f99cc
MS
160 // Draws the simulation and the 2D UI overlay
161 background(40);
162 color[] colors = glucose.getColors();
554e38ff
MS
163 if (debugMode) {
164 debugUI.maskColors(colors);
165 }
51d0d59a 166
0a9f99cc
MS
167 camera(
168 eyeX, eyeY, eyeZ,
169 midX, midY, midZ,
170 0, -1, 0
171 );
51d0d59a 172
51d0d59a
MS
173 noStroke();
174 fill(#141414);
87998ff3 175 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
176 fill(#070707);
177 stroke(#222222);
0a9f99cc 178 beginShape();
51d0d59a 179 vertex(0, 0, 0);
87998ff3
MS
180 vertex(TRAILER_WIDTH, 0, 0);
181 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
182 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 183 endShape();
0a9f99cc 184
51d0d59a
MS
185 noStroke();
186 fill(#292929);
87998ff3 187 drawBox(BASS_X, 0, BASS_Z, 0, 0, 0, BASS_WIDTH, BASS_HEIGHT, BASS_DEPTH, Cube.CHANNEL_WIDTH);
51d0d59a
MS
188 for (Cube c : glucose.model.cubes) {
189 drawCube(c);
190 }
191
0a9f99cc
MS
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);
a797d019 198 // println(p.fx + ":" + p.fy + ":" + p.fz);
0a9f99cc
MS
199 }
200 endShape();
201
202 // 2D Overlay
203 camera();
e0cea600 204 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
0a9f99cc
MS
205 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
206 ((PGraphicsOpenGL)g).endGL();
207 strokeWeight(1);
208 drawUI();
e73ef85d 209
554e38ff
MS
210 if (debugMode) {
211 debugUI.draw();
212 }
a797d019 213
e73ef85d 214 // TODO(mcslee): move into GLucose engine
79ae8245
MS
215 for (PandaDriver p : pandaBoards) {
216 p.send(colors);
e73ef85d 217 }
49815cc0
MS
218}
219
51d0d59a 220void drawCube(Cube c) {
254d34c0
MS
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);
51d0d59a
MS
223}
224
225void 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);
e0cea600 228 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 229 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 230 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
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
49815cc0
MS
257void drawUI() {
258 if (uiOn) {
259 ui.draw();
260 } else {
261 ui.drawHelpTip();
262 }
263 ui.drawFPS();
264}
265
266boolean uiOn = true;
bf551144
MS
267int restoreToIndex = -1;
268
49815cc0 269void keyPressed() {
bf551144
MS
270 if (mappingMode) {
271 mappingTool.keyPressed();
272 }
3f8be614 273 switch (key) {
0e3c5542
MS
274 case '-':
275 case '_':
276 frameRate(--targetFramerate);
277 break;
278 case '=':
279 case '+':
280 frameRate(++targetFramerate);
281 break;
cc9fcf4b
MS
282 case 'd':
283 debugMode = !debugMode;
284 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 285 break;
bf551144
MS
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;
e73ef85d 304 case 'p':
79ae8245
MS
305 for (PandaDriver p : pandaBoards) {
306 p.toggle();
307 }
cc9fcf4b 308 break;
3f8be614
MS
309 case 'u':
310 uiOn = !uiOn;
311 break;
49815cc0
MS
312 }
313}
314
0a9f99cc 315int mx, my;
0a9f99cc 316void mousePressed() {
79ae8245
MS
317 ui.mousePressed();
318 if (mouseX < ui.leftPos) {
554e38ff
MS
319 if (debugMode) {
320 debugUI.mousePressed();
321 }
0a9f99cc
MS
322 mx = mouseX;
323 my = mouseY;
324 }
325}
326
327void 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
342void mouseReleased() {
79ae8245 343 ui.mouseReleased();
0a9f99cc 344}
bda5421d
MS
345
346void mouseWheel(int delta) {
0ba6ac44
MS
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 }
bda5421d 354}
49815cc0 355