Rename CHANNEL_WIDTH to Cube. instead of Strip.
[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 int targetFramerate = 60;
38
39 int startMillis, lastMillis;
40 GLucose glucose;
41 HeronLX lx;
42 MappingTool mappingTool;
43 LXPattern[] patterns;
44 LXTransition[] transitions;
45 LXEffect[] effects;
46 OverlayUI ui;
47 ControlUI controlUI;
48 MappingUI mappingUI;
49 PandaDriver pandaFront;
50 PandaDriver pandaRear;
51 boolean mappingMode = false;
52
53 boolean pandaBoardsEnabled = false;
54
55 boolean debugMode = false;
56 DebugUI debugUI;
57
58 // Camera variables
59 float eyeR, eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
60
61 final float FEET = 12;
62
63 void setup() {
64 startMillis = lastMillis = millis();
65
66 // Initialize the Processing graphics environment
67 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
68 frameRate(targetFramerate);
69 noSmooth();
70 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
71 logTime("Created viewport");
72
73 // Create the GLucose engine to run the cubes
74 glucose = new GLucose(this, new SCMapping());
75 lx = glucose.lx;
76 lx.enableKeyboardTempo();
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");
84 glucose.setTransitions(transitions = transitions(glucose));
85 logTime("Built transitions");
86
87 // Build output driver
88 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
89 int[][] rearChannels = glucose.mapping.buildRearChannelList();
90 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
91 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
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");
95
96 // Build overlay UI
97 ui = controlUI = new ControlUI();
98 mappingUI = new MappingUI(mappingTool);
99 debugUI = new DebugUI(frontChannels, rearChannels);
100 logTime("Built overlay UI");
101
102 // MIDI devices
103 for (MidiInputDevice d : RWMidi.getInputDevices()) {
104 d.createInput(this);
105 }
106 SCMidiDevices.initializeStandardDevices(glucose);
107 logTime("Setup MIDI devices");
108
109 // Setup camera
110 midX = glucose.model.xMax/2 + 20;
111 midY = glucose.model.yMax/2;
112 midZ = glucose.model.zMax/2;
113 eyeR = -270;
114 eyeA = .15;
115 eyeY = midY + 20;
116 eyeX = midX + eyeR*sin(eyeA);
117 eyeZ = midZ + eyeR*cos(eyeA);
118 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
119 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
120 mouseWheel(mwe.getWheelRotation());
121 }});
122
123
124 println("Total setup: " + (millis() - startMillis) + "ms");
125 println("Hit the 'p' key to toggle Panda Board output");
126 }
127
128 void controllerChangeReceived(rwmidi.Controller cc) {
129 if (debugMode) {
130 println("CC: " + cc.toString());
131 }
132 }
133
134 void noteOnReceived(Note note) {
135 if (debugMode) {
136 println("Note On: " + note.toString());
137 }
138 }
139
140 void noteOffReceived(Note note) {
141 if (debugMode) {
142 println("Note Off: " + note.toString());
143 }
144 }
145
146 void logTime(String evt) {
147 int now = millis();
148 println(evt + ": " + (now - lastMillis) + "ms");
149 lastMillis = now;
150 }
151
152 void draw() {
153 // Draws the simulation and the 2D UI overlay
154 background(40);
155 color[] colors = glucose.getColors();
156 if (debugMode) {
157 debugUI.maskColors(colors);
158 }
159
160 camera(
161 eyeX, eyeY, eyeZ,
162 midX, midY, midZ,
163 0, -1, 0
164 );
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);
174 beginShape();
175 vertex(0, 0, 0);
176 vertex(trailerWidth, 0, 0);
177 vertex(trailerWidth, 0, trailerDepth);
178 vertex(0, 0, trailerDepth);
179 endShape();
180
181 noStroke();
182 fill(#292929);
183 for (Cube c : glucose.model.cubes) {
184 drawCube(c);
185 }
186
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);
193 // println(p.fx + ":" + p.fy + ":" + p.fz);
194 }
195 endShape();
196
197 // 2D Overlay
198 camera();
199 noLights();
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();
205
206 if (debugMode) {
207 debugUI.draw();
208 }
209
210 // TODO(mcslee): move into GLucose engine
211 if (pandaBoardsEnabled) {
212 pandaFront.send(colors);
213 pandaRear.send(colors);
214 }
215 }
216
217 void drawCube(Cube c) {
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);
219 }
220
221 void 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
253 void drawUI() {
254 if (uiOn) {
255 ui.draw();
256 } else {
257 ui.drawHelpTip();
258 }
259 ui.drawFPS();
260 }
261
262 boolean uiOn = true;
263 int restoreToIndex = -1;
264
265 void keyPressed() {
266 if (mappingMode) {
267 mappingTool.keyPressed();
268 }
269 switch (key) {
270 case '-':
271 case '_':
272 frameRate(--targetFramerate);
273 break;
274 case '=':
275 case '+':
276 frameRate(++targetFramerate);
277 break;
278 case 'd':
279 debugMode = !debugMode;
280 println("Debug output: " + (debugMode ? "ON" : "OFF"));
281 break;
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;
300 case 'p':
301 pandaBoardsEnabled = !pandaBoardsEnabled;
302 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
303 break;
304 case 'u':
305 uiOn = !uiOn;
306 break;
307 }
308 }
309
310 int mx, my;
311
312 void mousePressed() {
313 if (mouseX > ui.leftPos) {
314 ui.mousePressed();
315 } else {
316 if (debugMode) {
317 debugUI.mousePressed();
318 }
319 mx = mouseX;
320 my = mouseY;
321 }
322 }
323
324 void 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
339 void mouseReleased() {
340 if (mouseX > ui.leftPos) {
341 ui.mouseReleased();
342 }
343 }
344
345 void mouseWheel(int delta) {
346 eyeR = constrain(eyeR - delta, -500, -80);
347 eyeX = midX + eyeR*sin(eyeA);
348 eyeZ = midZ + eyeR*cos(eyeA);
349 }
350