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