Fix bug in how sending data to pandaboards
[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;
e73ef85d
MS
59PandaDriver pandaFront;
60PandaDriver pandaRear;
bf551144 61boolean mappingMode = false;
e73ef85d
MS
62
63boolean pandaBoardsEnabled = false;
49815cc0 64
cc9fcf4b 65boolean debugMode = false;
554e38ff 66DebugUI debugUI;
cc9fcf4b 67
0a9f99cc 68// Camera variables
bda5421d 69float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
0a9f99cc 70
49815cc0
MS
71void setup() {
72 startMillis = lastMillis = millis();
73
74 // Initialize the Processing graphics environment
75 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 76 frameRate(targetFramerate);
3f8be614 77 noSmooth();
49815cc0
MS
78 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
79 logTime("Created viewport");
80
81 // Create the GLucose engine to run the cubes
1ecdb44a 82 glucose = new GLucose(this, new SCMapping());
49815cc0 83 lx = glucose.lx;
cc9fcf4b 84 lx.enableKeyboardTempo();
49815cc0
MS
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");
cc9fcf4b 92 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 93 logTime("Built transitions");
e73ef85d
MS
94
95 // Build output driver
96 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
97 int[][] rearChannels = glucose.mapping.buildRearChannelList();
2bae07c9 98 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
bfff6bc2
MS
99 pandaFront = new PandaDriver(new NetAddress("192.168.1.28", 9001), glucose.model, frontChannels);
100 pandaRear = new PandaDriver(new NetAddress("192.168.1.29", 9001), glucose.model, rearChannels);
e73ef85d 101 logTime("Build PandaDriver");
49815cc0
MS
102
103 // Build overlay UI
bf551144
MS
104 ui = controlUI = new ControlUI();
105 mappingUI = new MappingUI(mappingTool);
554e38ff 106 debugUI = new DebugUI(frontChannels, rearChannels);
49815cc0 107 logTime("Built overlay UI");
1ecdb44a 108
49815cc0 109 // MIDI devices
cc9fcf4b
MS
110 for (MidiInputDevice d : RWMidi.getInputDevices()) {
111 d.createInput(this);
112 }
113 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 114 logTime("Setup MIDI devices");
554e38ff 115
0a9f99cc 116 // Setup camera
e0cea600 117 midX = TRAILER_WIDTH/2. + 20;
0a9f99cc 118 midY = glucose.model.yMax/2;
e0cea600
MS
119 midZ = TRAILER_DEPTH/2.;
120 eyeR = -290;
0a9f99cc
MS
121 eyeA = .15;
122 eyeY = midY + 20;
123 eyeX = midX + eyeR*sin(eyeA);
124 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
125 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
126 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
127 mouseWheel(mwe.getWheelRotation());
128 }});
129
0a9f99cc 130
49815cc0 131 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 132 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
133}
134
cc9fcf4b
MS
135void controllerChangeReceived(rwmidi.Controller cc) {
136 if (debugMode) {
137 println("CC: " + cc.toString());
138 }
139}
140
141void noteOnReceived(Note note) {
142 if (debugMode) {
143 println("Note On: " + note.toString());
144 }
145}
146
147void noteOffReceived(Note note) {
148 if (debugMode) {
149 println("Note Off: " + note.toString());
150 }
151}
152
49815cc0
MS
153void logTime(String evt) {
154 int now = millis();
155 println(evt + ": " + (now - lastMillis) + "ms");
156 lastMillis = now;
157}
158
159void draw() {
0a9f99cc
MS
160 // Draws the simulation and the 2D UI overlay
161 background(40);
162 color[] colors = glucose.getColors();
554e38ff
MS
163 if (debugMode) {
164 debugUI.maskColors(colors);
165 }
51d0d59a 166
0a9f99cc
MS
167 camera(
168 eyeX, eyeY, eyeZ,
169 midX, midY, midZ,
170 0, -1, 0
171 );
51d0d59a 172
51d0d59a
MS
173 noStroke();
174 fill(#141414);
87998ff3 175 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
51d0d59a
MS
176 fill(#070707);
177 stroke(#222222);
0a9f99cc 178 beginShape();
51d0d59a 179 vertex(0, 0, 0);
87998ff3
MS
180 vertex(TRAILER_WIDTH, 0, 0);
181 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
182 vertex(0, 0, TRAILER_DEPTH);
51d0d59a 183 endShape();
0a9f99cc 184
51d0d59a
MS
185 noStroke();
186 fill(#292929);
87998ff3 187 drawBox(BASS_X, 0, BASS_Z, 0, 0, 0, BASS_WIDTH, BASS_HEIGHT, BASS_DEPTH, Cube.CHANNEL_WIDTH);
51d0d59a
MS
188 for (Cube c : glucose.model.cubes) {
189 drawCube(c);
190 }
191
0a9f99cc
MS
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);
a797d019 198 // println(p.fx + ":" + p.fy + ":" + p.fz);
0a9f99cc
MS
199 }
200 endShape();
201
202 // 2D Overlay
203 camera();
e0cea600 204 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
0a9f99cc
MS
205 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
206 ((PGraphicsOpenGL)g).endGL();
207 strokeWeight(1);
208 drawUI();
e73ef85d 209
554e38ff
MS
210 if (debugMode) {
211 debugUI.draw();
212 }
a797d019 213
e73ef85d
MS
214 // TODO(mcslee): move into GLucose engine
215 if (pandaBoardsEnabled) {
e73ef85d 216 pandaFront.send(colors);
bfff6bc2 217 // pandaRear.send(colors);
e73ef85d 218 }
49815cc0
MS
219}
220
51d0d59a 221void drawCube(Cube c) {
254d34c0
MS
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);
51d0d59a
MS
224}
225
226void 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);
e0cea600 229 rotate(rx / 180. * PI, -1, 0, 0);
51d0d59a 230 rotate(ry / 180. * PI, 0, -1, 0);
e0cea600 231 rotate(rz / 180. * PI, 0, 0, -1);
51d0d59a
MS
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
49815cc0
MS
258void drawUI() {
259 if (uiOn) {
260 ui.draw();
261 } else {
262 ui.drawHelpTip();
263 }
264 ui.drawFPS();
265}
266
267boolean uiOn = true;
bf551144
MS
268int restoreToIndex = -1;
269
49815cc0 270void keyPressed() {
bf551144
MS
271 if (mappingMode) {
272 mappingTool.keyPressed();
273 }
3f8be614 274 switch (key) {
0e3c5542
MS
275 case '-':
276 case '_':
277 frameRate(--targetFramerate);
278 break;
279 case '=':
280 case '+':
281 frameRate(++targetFramerate);
282 break;
cc9fcf4b
MS
283 case 'd':
284 debugMode = !debugMode;
285 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 286 break;
bf551144
MS
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;
e73ef85d
MS
305 case 'p':
306 pandaBoardsEnabled = !pandaBoardsEnabled;
307 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
cc9fcf4b 308 break;
3f8be614
MS
309 case 'u':
310 uiOn = !uiOn;
311 break;
49815cc0
MS
312 }
313}
314
0a9f99cc
MS
315int mx, my;
316
317void mousePressed() {
318 if (mouseX > ui.leftPos) {
319 ui.mousePressed();
320 } else {
554e38ff
MS
321 if (debugMode) {
322 debugUI.mousePressed();
323 }
0a9f99cc
MS
324 mx = mouseX;
325 my = mouseY;
326 }
327}
328
329void 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
344void mouseReleased() {
345 if (mouseX > ui.leftPos) {
346 ui.mouseReleased();
347 }
348}
bda5421d
MS
349
350void mouseWheel(int delta) {
351 eyeR = constrain(eyeR - delta, -500, -80);
352 eyeX = midX + eyeR*sin(eyeA);
353 eyeZ = midZ + eyeR*cos(eyeA);
354}
49815cc0 355