Use the new layout code
[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) {
4972d7c4 223 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
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