Merge branch 'master' of github.com:sugarcubes/SugarCubes
[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 fill(#393939);
180 drawBassBox(glucose.model.bassBox);
181 for (Cube c : glucose.model.cubes) {
182 drawCube(c);
183 }
184
185 noFill();
186 strokeWeight(2);
187 beginShape(POINTS);
188 for (Point p : glucose.model.points) {
189 stroke(colors[p.index]);
190 vertex(p.fx, p.fy, p.fz);
191 }
192 endShape();
193
194 // 2D Overlay
195 camera();
196 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
197 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
198 ((PGraphicsOpenGL)g).endGL();
199 strokeWeight(1);
200 drawUI();
201
202 if (debugMode) {
203 debugUI.draw();
204 }
205
206 // TODO(mcslee): move into GLucose engine
207 for (PandaDriver p : pandaBoards) {
208 p.send(colors);
209 }
210 }
211
212 void drawBassBox(BassBox b) {
213 float in = .15;
214 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);
215
216 pushMatrix();
217 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
218 for (int j = 0; j < 2; ++j) {
219 pushMatrix();
220 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
221 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
222 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
223 }
224 popMatrix();
225 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
226 }
227 popMatrix();
228
229 pushMatrix();
230 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
231 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
232 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
233 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
234 popMatrix();
235 }
236
237 void drawCube(Cube c) {
238 float in = .15;
239 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);
240 }
241
242 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
243 pushMatrix();
244 translate(x, y, z);
245 rotate(rx / 180. * PI, -1, 0, 0);
246 rotate(ry / 180. * PI, 0, -1, 0);
247 rotate(rz / 180. * PI, 0, 0, -1);
248 for (int i = 0; i < 4; ++i) {
249 float wid = (i % 2 == 0) ? xd : zd;
250
251 beginShape();
252 vertex(0, 0);
253 vertex(wid, 0);
254 vertex(wid, yd);
255 vertex(wid - sw, yd);
256 vertex(wid - sw, sw);
257 vertex(0, sw);
258 endShape();
259 beginShape();
260 vertex(0, sw);
261 vertex(0, yd);
262 vertex(wid - sw, yd);
263 vertex(wid - sw, yd - sw);
264 vertex(sw, yd - sw);
265 vertex(sw, sw);
266 endShape();
267
268 translate(wid, 0, 0);
269 rotate(HALF_PI, 0, -1, 0);
270 }
271 popMatrix();
272 }
273
274 void drawUI() {
275 if (uiOn) {
276 ui.draw();
277 } else {
278 ui.drawHelpTip();
279 }
280 ui.drawFPS();
281 }
282
283 boolean uiOn = true;
284 int restoreToIndex = -1;
285
286 void keyPressed() {
287 if (mappingMode) {
288 mappingTool.keyPressed();
289 }
290 switch (key) {
291 case '-':
292 case '_':
293 frameRate(--targetFramerate);
294 break;
295 case '=':
296 case '+':
297 frameRate(++targetFramerate);
298 break;
299 case 'd':
300 debugMode = !debugMode;
301 println("Debug output: " + (debugMode ? "ON" : "OFF"));
302 break;
303 case 'm':
304 mappingMode = !mappingMode;
305 if (mappingMode) {
306 LXPattern pattern = lx.getPattern();
307 for (int i = 0; i < patterns.length; ++i) {
308 if (pattern == patterns[i]) {
309 restoreToIndex = i;
310 break;
311 }
312 }
313 ui = mappingUI;
314 lx.setPatterns(new LXPattern[] { mappingTool });
315 } else {
316 ui = controlUI;
317 lx.setPatterns(patterns);
318 lx.goIndex(restoreToIndex);
319 }
320 break;
321 case 'p':
322 for (PandaDriver p : pandaBoards) {
323 p.toggle();
324 }
325 break;
326 case 'u':
327 uiOn = !uiOn;
328 break;
329 }
330 }
331
332 int mx, my;
333 void mousePressed() {
334 ui.mousePressed();
335 if (mouseX < ui.leftPos) {
336 if (debugMode) {
337 debugUI.mousePressed();
338 }
339 mx = mouseX;
340 my = mouseY;
341 }
342 }
343
344 void mouseDragged() {
345 if (mouseX > ui.leftPos) {
346 ui.mouseDragged();
347 } else {
348 int dx = mouseX - mx;
349 int dy = mouseY - my;
350 mx = mouseX;
351 my = mouseY;
352 eyeA += dx*.003;
353 eyeX = midX + eyeR*sin(eyeA);
354 eyeZ = midZ + eyeR*cos(eyeA);
355 eyeY += dy;
356 }
357 }
358
359 void mouseReleased() {
360 ui.mouseReleased();
361 }
362
363 void mouseWheel(int delta) {
364 if (mouseX > ui.leftPos) {
365 ui.mouseWheel(delta);
366 } else {
367 eyeR = constrain(eyeR - delta, -500, -80);
368 eyeX = midX + eyeR*sin(eyeA);
369 eyeZ = midZ + eyeR*cos(eyeA);
370 }
371 }
372