Add bass cabinets!
[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 final float TRAILER_WIDTH = 240;
38 final float TRAILER_DEPTH = 97;
39 final float TRAILER_HEIGHT = 33;
40
41 final float BASS_WIDTH = 124;
42 final float BASS_HEIGHT = 31.5;
43 final float BASS_DEPTH = 66;
44 final float BASS_X = (TRAILER_WIDTH - BASS_WIDTH) / 2.;
45 final float BASS_Z = (TRAILER_DEPTH - BASS_DEPTH) / 2.;
46
47 int targetFramerate = 60;
48
49 int startMillis, lastMillis;
50 GLucose glucose;
51 HeronLX lx;
52 MappingTool mappingTool;
53 LXPattern[] patterns;
54 LXTransition[] transitions;
55 LXEffect[] effects;
56 OverlayUI ui;
57 ControlUI controlUI;
58 MappingUI mappingUI;
59 PandaDriver pandaFront;
60 PandaDriver pandaRear;
61 boolean mappingMode = false;
62
63 boolean pandaBoardsEnabled = false;
64
65 boolean debugMode = false;
66 DebugUI debugUI;
67
68 // Camera variables
69 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
70
71 final float FEET = 12;
72
73 void setup() {
74 startMillis = lastMillis = millis();
75
76 // Initialize the Processing graphics environment
77 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
78 frameRate(targetFramerate);
79 noSmooth();
80 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
81 logTime("Created viewport");
82
83 // Create the GLucose engine to run the cubes
84 glucose = new GLucose(this, new SCMapping());
85 lx = glucose.lx;
86 lx.enableKeyboardTempo();
87 logTime("Built GLucose engine");
88
89 // Set the patterns
90 glucose.lx.setPatterns(patterns = patterns(glucose));
91 logTime("Built patterns");
92 glucose.lx.addEffects(effects = effects(glucose));
93 logTime("Built effects");
94 glucose.setTransitions(transitions = transitions(glucose));
95 logTime("Built transitions");
96
97 // Build output driver
98 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
99 int[][] rearChannels = glucose.mapping.buildRearChannelList();
100 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
101 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
102 pandaFront = new PandaDriver(new NetAddress("192.168.1.28", 9001), glucose.model, frontChannels, flippedRGB);
103 pandaRear = new PandaDriver(new NetAddress("192.168.1.29", 9001), glucose.model, rearChannels, flippedRGB);
104 logTime("Build PandaDriver");
105
106 // Build overlay UI
107 ui = controlUI = new ControlUI();
108 mappingUI = new MappingUI(mappingTool);
109 debugUI = new DebugUI(frontChannels, rearChannels);
110 logTime("Built overlay UI");
111
112 // MIDI devices
113 for (MidiInputDevice d : RWMidi.getInputDevices()) {
114 d.createInput(this);
115 }
116 SCMidiDevices.initializeStandardDevices(glucose);
117 logTime("Setup MIDI devices");
118
119 // Setup camera
120 midX = glucose.model.xMax/2 + 20;
121 midY = glucose.model.yMax/2;
122 midZ = glucose.model.zMax/2;
123 eyeR = -270;
124 eyeA = .15;
125 eyeY = midY + 20;
126 eyeX = midX + eyeR*sin(eyeA);
127 eyeZ = midZ + eyeR*cos(eyeA);
128 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
129 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
130 mouseWheel(mwe.getWheelRotation());
131 }});
132
133
134 println("Total setup: " + (millis() - startMillis) + "ms");
135 println("Hit the 'p' key to toggle Panda Board output");
136 }
137
138 void controllerChangeReceived(rwmidi.Controller cc) {
139 if (debugMode) {
140 println("CC: " + cc.toString());
141 }
142 }
143
144 void noteOnReceived(Note note) {
145 if (debugMode) {
146 println("Note On: " + note.toString());
147 }
148 }
149
150 void noteOffReceived(Note note) {
151 if (debugMode) {
152 println("Note Off: " + note.toString());
153 }
154 }
155
156 void logTime(String evt) {
157 int now = millis();
158 println(evt + ": " + (now - lastMillis) + "ms");
159 lastMillis = now;
160 }
161
162 void draw() {
163 // Draws the simulation and the 2D UI overlay
164 background(40);
165 color[] colors = glucose.getColors();
166 if (debugMode) {
167 debugUI.maskColors(colors);
168 }
169
170 camera(
171 eyeX, eyeY, eyeZ,
172 midX, midY, midZ,
173 0, -1, 0
174 );
175
176 noStroke();
177 fill(#141414);
178 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
179 fill(#070707);
180 stroke(#222222);
181 beginShape();
182 vertex(0, 0, 0);
183 vertex(TRAILER_WIDTH, 0, 0);
184 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
185 vertex(0, 0, TRAILER_DEPTH);
186 endShape();
187
188 noStroke();
189 fill(#292929);
190 drawBox(BASS_X, 0, BASS_Z, 0, 0, 0, BASS_WIDTH, BASS_HEIGHT, BASS_DEPTH, Cube.CHANNEL_WIDTH);
191 for (Cube c : glucose.model.cubes) {
192 drawCube(c);
193 }
194
195 noFill();
196 strokeWeight(2);
197 beginShape(POINTS);
198 for (Point p : glucose.model.points) {
199 stroke(colors[p.index]);
200 vertex(p.fx, p.fy, p.fz);
201 // println(p.fx + ":" + p.fy + ":" + p.fz);
202 }
203 endShape();
204
205 // 2D Overlay
206 camera();
207 javax.media.opengl.GL gl= ((PGraphicsOpenGL)g).beginGL();
208 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
209 ((PGraphicsOpenGL)g).endGL();
210 strokeWeight(1);
211 drawUI();
212
213 if (debugMode) {
214 debugUI.draw();
215 }
216
217 // TODO(mcslee): move into GLucose engine
218 if (pandaBoardsEnabled) {
219 pandaFront.send(colors);
220 pandaRear.send(colors);
221 }
222 }
223
224 void drawCube(Cube c) {
225 drawBox(c.x, c.y, c.z, c.rx, c.ry, c.rz, Cube.EDGE_WIDTH, Cube.EDGE_HEIGHT, Cube.EDGE_WIDTH, Cube.CHANNEL_WIDTH);
226 }
227
228 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
229 pushMatrix();
230 translate(x, y, z);
231 rotate(rx, 1, 0, 0);
232 rotate(ry / 180. * PI, 0, -1, 0);
233 rotate(rz, 0, 0, 1);
234 for (int i = 0; i < 4; ++i) {
235 float wid = (i % 2 == 0) ? xd : zd;
236
237 beginShape();
238 vertex(0, 0);
239 vertex(wid, 0);
240 vertex(wid, yd);
241 vertex(wid - sw, yd);
242 vertex(wid - sw, sw);
243 vertex(0, sw);
244 endShape();
245 beginShape();
246 vertex(0, sw);
247 vertex(0, yd);
248 vertex(wid - sw, yd);
249 vertex(wid - sw, yd - sw);
250 vertex(sw, yd - sw);
251 vertex(sw, sw);
252 endShape();
253
254 translate(wid, 0, 0);
255 rotate(HALF_PI, 0, -1, 0);
256 }
257 popMatrix();
258 }
259
260 void drawUI() {
261 if (uiOn) {
262 ui.draw();
263 } else {
264 ui.drawHelpTip();
265 }
266 ui.drawFPS();
267 }
268
269 boolean uiOn = true;
270 int restoreToIndex = -1;
271
272 void keyPressed() {
273 if (mappingMode) {
274 mappingTool.keyPressed();
275 }
276 switch (key) {
277 case '-':
278 case '_':
279 frameRate(--targetFramerate);
280 break;
281 case '=':
282 case '+':
283 frameRate(++targetFramerate);
284 break;
285 case 'd':
286 debugMode = !debugMode;
287 println("Debug output: " + (debugMode ? "ON" : "OFF"));
288 break;
289 case 'm':
290 mappingMode = !mappingMode;
291 if (mappingMode) {
292 LXPattern pattern = lx.getPattern();
293 for (int i = 0; i < patterns.length; ++i) {
294 if (pattern == patterns[i]) {
295 restoreToIndex = i;
296 break;
297 }
298 }
299 ui = mappingUI;
300 lx.setPatterns(new LXPattern[] { mappingTool });
301 } else {
302 ui = controlUI;
303 lx.setPatterns(patterns);
304 lx.goIndex(restoreToIndex);
305 }
306 break;
307 case 'p':
308 pandaBoardsEnabled = !pandaBoardsEnabled;
309 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
310 break;
311 case 'u':
312 uiOn = !uiOn;
313 break;
314 }
315 }
316
317 int mx, my;
318
319 void mousePressed() {
320 if (mouseX > ui.leftPos) {
321 ui.mousePressed();
322 } else {
323 if (debugMode) {
324 debugUI.mousePressed();
325 }
326 mx = mouseX;
327 my = mouseY;
328 }
329 }
330
331 void mouseDragged() {
332 if (mouseX > ui.leftPos) {
333 ui.mouseDragged();
334 } else {
335 int dx = mouseX - mx;
336 int dy = mouseY - my;
337 mx = mouseX;
338 my = mouseY;
339 eyeA += dx*.003;
340 eyeX = midX + eyeR*sin(eyeA);
341 eyeZ = midZ + eyeR*cos(eyeA);
342 eyeY += dy;
343 }
344 }
345
346 void mouseReleased() {
347 if (mouseX > ui.leftPos) {
348 ui.mouseReleased();
349 }
350 }
351
352 void mouseWheel(int delta) {
353 eyeR = constrain(eyeR - delta, -500, -80);
354 eyeX = midX + eyeR*sin(eyeA);
355 eyeZ = midZ + eyeR*cos(eyeA);
356 }
357