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