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