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