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