Update IP addresses
[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 GLucose glucose;
51 HeronLX lx;
52 MappingTool mappingTool;
53 LXPattern[] patterns;
54 LXTransition[] transitions;
55 LXEffect[] effects;
56 OverlayUI ui;
57 ControlUI controlUI;
58 MappingUI mappingUI;
59 PandaDriver pandaFront;
60 PandaDriver pandaRear;
61 boolean mappingMode = false;
62
63 boolean pandaBoardsEnabled = false;
64
65 boolean debugMode = false;
66 DebugUI debugUI;
67
68 // Camera variables
69 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
70
71 void setup() {
72 startMillis = lastMillis = millis();
73
74 // Initialize the Processing graphics environment
75 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
76 frameRate(targetFramerate);
77 noSmooth();
78 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
79 logTime("Created viewport");
80
81 // Create the GLucose engine to run the cubes
82 glucose = new GLucose(this, new SCMapping());
83 lx = glucose.lx;
84 lx.enableKeyboardTempo();
85 logTime("Built GLucose engine");
86
87 // Set the patterns
88 glucose.lx.setPatterns(patterns = patterns(glucose));
89 logTime("Built patterns");
90 glucose.lx.addEffects(effects = effects(glucose));
91 logTime("Built effects");
92 glucose.setTransitions(transitions = transitions(glucose));
93 logTime("Built transitions");
94
95 // Build output driver
96 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
97 int[][] rearChannels = glucose.mapping.buildRearChannelList();
98 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
99 pandaFront = new PandaDriver(new NetAddress("10.200.1.28", 9001), glucose.model, frontChannels);
100 pandaRear = new PandaDriver(new NetAddress("10.200.1.29", 9001), glucose.model, rearChannels);
101 logTime("Build PandaDriver");
102
103 // Build overlay UI
104 ui = controlUI = new ControlUI();
105 mappingUI = new MappingUI(mappingTool);
106 debugUI = new DebugUI(frontChannels, rearChannels);
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 if (pandaBoardsEnabled) {
216 pandaFront.send(colors);
217 // pandaRear.send(colors);
218 }
219 }
220
221 void drawCube(Cube c) {
222 float in = .15;
223 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);
224 }
225
226 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
227 pushMatrix();
228 translate(x, y, z);
229 rotate(rx / 180. * PI, -1, 0, 0);
230 rotate(ry / 180. * PI, 0, -1, 0);
231 rotate(rz / 180. * PI, 0, 0, -1);
232 for (int i = 0; i < 4; ++i) {
233 float wid = (i % 2 == 0) ? xd : zd;
234
235 beginShape();
236 vertex(0, 0);
237 vertex(wid, 0);
238 vertex(wid, yd);
239 vertex(wid - sw, yd);
240 vertex(wid - sw, sw);
241 vertex(0, sw);
242 endShape();
243 beginShape();
244 vertex(0, sw);
245 vertex(0, yd);
246 vertex(wid - sw, yd);
247 vertex(wid - sw, yd - sw);
248 vertex(sw, yd - sw);
249 vertex(sw, sw);
250 endShape();
251
252 translate(wid, 0, 0);
253 rotate(HALF_PI, 0, -1, 0);
254 }
255 popMatrix();
256 }
257
258 void drawUI() {
259 if (uiOn) {
260 ui.draw();
261 } else {
262 ui.drawHelpTip();
263 }
264 ui.drawFPS();
265 }
266
267 boolean uiOn = true;
268 int restoreToIndex = -1;
269
270 void keyPressed() {
271 if (mappingMode) {
272 mappingTool.keyPressed();
273 }
274 switch (key) {
275 case '-':
276 case '_':
277 frameRate(--targetFramerate);
278 break;
279 case '=':
280 case '+':
281 frameRate(++targetFramerate);
282 break;
283 case 'd':
284 debugMode = !debugMode;
285 println("Debug output: " + (debugMode ? "ON" : "OFF"));
286 break;
287 case 'm':
288 mappingMode = !mappingMode;
289 if (mappingMode) {
290 LXPattern pattern = lx.getPattern();
291 for (int i = 0; i < patterns.length; ++i) {
292 if (pattern == patterns[i]) {
293 restoreToIndex = i;
294 break;
295 }
296 }
297 ui = mappingUI;
298 lx.setPatterns(new LXPattern[] { mappingTool });
299 } else {
300 ui = controlUI;
301 lx.setPatterns(patterns);
302 lx.goIndex(restoreToIndex);
303 }
304 break;
305 case 'p':
306 pandaBoardsEnabled = !pandaBoardsEnabled;
307 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
308 break;
309 case 'u':
310 uiOn = !uiOn;
311 break;
312 }
313 }
314
315 int mx, my;
316
317 void mousePressed() {
318 if (mouseX > ui.leftPos) {
319 ui.mousePressed();
320 } else {
321 if (debugMode) {
322 debugUI.mousePressed();
323 }
324 mx = mouseX;
325 my = mouseY;
326 }
327 }
328
329 void mouseDragged() {
330 if (mouseX > ui.leftPos) {
331 ui.mouseDragged();
332 } else {
333 int dx = mouseX - mx;
334 int dy = mouseY - my;
335 mx = mouseX;
336 my = mouseY;
337 eyeA += dx*.003;
338 eyeX = midX + eyeR*sin(eyeA);
339 eyeZ = midZ + eyeR*cos(eyeA);
340 eyeY += dy;
341 }
342 }
343
344 void mouseReleased() {
345 if (mouseX > ui.leftPos) {
346 ui.mouseReleased();
347 }
348 }
349
350 void mouseWheel(int delta) {
351 if (mouseX > ui.leftPos) {
352 ui.mouseWheel(delta);
353 } else {
354 eyeR = constrain(eyeR - delta, -500, -80);
355 eyeX = midX + eyeR*sin(eyeA);
356 eyeZ = midZ + eyeR*cos(eyeA);
357 }
358 }
359