Merge branch 'master' of github.com:sugarcubes/SugarCubes
[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
92c06c97 37// The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
87998ff3
MS
38final float TRAILER_WIDTH = 240;
39final float TRAILER_DEPTH = 97;
40final float TRAILER_HEIGHT = 33;
41
51d0d59a 42int targetFramerate = 60;
49815cc0
MS
43
44int startMillis, lastMillis;
45GLucose glucose;
46HeronLX lx;
bf551144 47MappingTool mappingTool;
49815cc0
MS
48LXPattern[] patterns;
49LXTransition[] transitions;
50LXEffect[] effects;
51OverlayUI ui;
bf551144
MS
52ControlUI controlUI;
53MappingUI mappingUI;
79ae8245 54PandaDriver[] pandaBoards;
bf551144 55boolean mappingMode = false;
cc9fcf4b 56boolean debugMode = false;
554e38ff 57DebugUI debugUI;
cc9fcf4b 58
0a9f99cc 59// Camera variables
bda5421d 60float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 61
49815cc0
MS
62void setup() {
63 startMillis = lastMillis = millis();
64
65 // Initialize the Processing graphics environment
66 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 67 frameRate(targetFramerate);
3f8be614 68 noSmooth();
49815cc0
MS
69 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
70 logTime("Created viewport");
71
72 // Create the GLucose engine to run the cubes
186bc4d3 73 glucose = new GLucose(this, buildModel());
49815cc0 74 lx = glucose.lx;
cc9fcf4b 75 lx.enableKeyboardTempo();
49815cc0
MS
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");
cc9fcf4b 83 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 84 logTime("Built transitions");
e73ef85d
MS
85
86 // Build output driver
186bc4d3 87 PandaMapping[] pandaMappings = buildPandaList();
45f43cc2
MS
88 pandaBoards = new PandaDriver[pandaMappings.length];
89 int pbi = 0;
90 for (PandaMapping pm : pandaMappings) {
44b8de9c 91 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
45f43cc2
MS
92 }
93 mappingTool = new MappingTool(glucose, pandaMappings);
94 logTime("Built PandaDriver");
49815cc0
MS
95
96 // Build overlay UI
bf551144
MS
97 ui = controlUI = new ControlUI();
98 mappingUI = new MappingUI(mappingTool);
45f43cc2 99 debugUI = new DebugUI(pandaMappings);
49815cc0 100 logTime("Built overlay UI");
1ecdb44a 101
49815cc0 102 // MIDI devices
cc9fcf4b
MS
103 for (MidiInputDevice d : RWMidi.getInputDevices()) {
104 d.createInput(this);
105 }
106 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 107 logTime("Setup MIDI devices");
554e38ff 108
0a9f99cc 109 // Setup camera
e0cea600 110 midX = TRAILER_WIDTH/2. + 20;
0a9f99cc 111 midY = glucose.model.yMax/2;
e0cea600
MS
112 midZ = TRAILER_DEPTH/2.;
113 eyeR = -290;
0a9f99cc
MS
114 eyeA = .15;
115 eyeY = midY + 20;
116 eyeX = midX + eyeR*sin(eyeA);
117 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
118 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
119 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
120 mouseWheel(mwe.getWheelRotation());
121 }});
122
0a9f99cc 123
49815cc0 124 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 125 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
126}
127
cc9fcf4b
MS
128void controllerChangeReceived(rwmidi.Controller cc) {
129 if (debugMode) {
130 println("CC: " + cc.toString());
131 }
132}
133
134void noteOnReceived(Note note) {
135 if (debugMode) {
136 println("Note On: " + note.toString());
137 }
138}
139
140void noteOffReceived(Note note) {
141 if (debugMode) {
142 println("Note Off: " + note.toString());
143 }
144}
145
49815cc0
MS
146void logTime(String evt) {
147 int now = millis();
148 println(evt + ": " + (now - lastMillis) + "ms");
149 lastMillis = now;
150}
151
152void draw() {
0a9f99cc
MS
153 // Draws the simulation and the 2D UI overlay
154 background(40);
155 color[] colors = glucose.getColors();
554e38ff
MS
156 if (debugMode) {
157 debugUI.maskColors(colors);
158 }
51d0d59a 159
0a9f99cc
MS
160 camera(
161 eyeX, eyeY, eyeZ,
162 midX, midY, midZ,
163 0, -1, 0
164 );
51d0d59a 165
51d0d59a
MS
166 noStroke();
167 fill(#141414);
87998ff3 168 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
169 fill(#070707);
170 stroke(#222222);
0a9f99cc 171 beginShape();
51d0d59a 172 vertex(0, 0, 0);
87998ff3
MS
173 vertex(TRAILER_WIDTH, 0, 0);
174 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
175 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 176 endShape();
0a9f99cc 177
51d0d59a 178 noStroke();
76471486 179 fill(#393939);
92c06c97 180 drawBassBox(glucose.model.bassBox);
51d0d59a
MS
181 for (Cube c : glucose.model.cubes) {
182 drawCube(c);
183 }
184
0a9f99cc
MS
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();
e0cea600 196 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
0a9f99cc
MS
197 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
198 ((PGraphicsOpenGL)g).endGL();
199 strokeWeight(1);
200 drawUI();
e73ef85d 201
554e38ff
MS
202 if (debugMode) {
203 debugUI.draw();
204 }
a797d019 205
e73ef85d 206 // TODO(mcslee): move into GLucose engine
79ae8245
MS
207 for (PandaDriver p : pandaBoards) {
208 p.send(colors);
e73ef85d 209 }
49815cc0
MS
210}
211
92c06c97
MS
212void 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
51d0d59a 237void drawCube(Cube c) {
254d34c0
MS
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);
51d0d59a
MS
240}
241
242void 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);
e0cea600 245 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 246 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 247 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
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
49815cc0
MS
274void drawUI() {
275 if (uiOn) {
276 ui.draw();
277 } else {
278 ui.drawHelpTip();
279 }
280 ui.drawFPS();
281}
282
283boolean uiOn = true;
bf551144
MS
284int restoreToIndex = -1;
285
49815cc0 286void keyPressed() {
bf551144
MS
287 if (mappingMode) {
288 mappingTool.keyPressed();
289 }
3f8be614 290 switch (key) {
0e3c5542
MS
291 case '-':
292 case '_':
293 frameRate(--targetFramerate);
294 break;
295 case '=':
296 case '+':
297 frameRate(++targetFramerate);
298 break;
cc9fcf4b
MS
299 case 'd':
300 debugMode = !debugMode;
301 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 302 break;
bf551144
MS
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;
e73ef85d 321 case 'p':
79ae8245
MS
322 for (PandaDriver p : pandaBoards) {
323 p.toggle();
324 }
cc9fcf4b 325 break;
3f8be614
MS
326 case 'u':
327 uiOn = !uiOn;
328 break;
49815cc0
MS
329 }
330}
331
0a9f99cc 332int mx, my;
0a9f99cc 333void mousePressed() {
79ae8245
MS
334 ui.mousePressed();
335 if (mouseX < ui.leftPos) {
554e38ff
MS
336 if (debugMode) {
337 debugUI.mousePressed();
338 }
0a9f99cc
MS
339 mx = mouseX;
340 my = mouseY;
341 }
342}
343
344void 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
359void mouseReleased() {
79ae8245 360 ui.mouseReleased();
0a9f99cc 361}
bda5421d
MS
362
363void mouseWheel(int delta) {
0ba6ac44
MS
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 }
bda5421d 371}
49815cc0 372