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