Add a debug overlay mode that can set channels/cubes to black/white
[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 = 45;
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 void setup() {
62 startMillis = lastMillis = millis();
63
64 // Initialize the Processing graphics environment
65 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
66 frameRate(targetFramerate);
67 noSmooth();
68 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
69 logTime("Created viewport");
70
71 // Create the GLucose engine to run the cubes
72 glucose = new GLucose(this, new SCMapping());
73 lx = glucose.lx;
74 lx.enableKeyboardTempo();
75 logTime("Built GLucose engine");
76
77 // Set the patterns
78 glucose.lx.setPatterns(patterns = patterns(glucose));
79 logTime("Built patterns");
80 glucose.lx.addEffects(effects = effects(glucose));
81 logTime("Built effects");
82 glucose.setTransitions(transitions = transitions(glucose));
83 logTime("Built transitions");
84
85 // Build output driver
86 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
87 int[][] rearChannels = glucose.mapping.buildRearChannelList();
88 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
89 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
90 pandaFront = new PandaDriver(new NetAddress("192.168.1.28", 9001), glucose.model, frontChannels, flippedRGB);
91 pandaRear = new PandaDriver(new NetAddress("192.168.1.29", 9001), glucose.model, rearChannels, flippedRGB);
92 logTime("Build PandaDriver");
93
94 // Build overlay UI
95 ui = controlUI = new ControlUI();
96 mappingUI = new MappingUI(mappingTool);
97 debugUI = new DebugUI(frontChannels, rearChannels);
98 logTime("Built overlay UI");
99
100 // MIDI devices
101 for (MidiInputDevice d : RWMidi.getInputDevices()) {
102 d.createInput(this);
103 }
104 SCMidiDevices.initializeStandardDevices(glucose);
105 logTime("Setup MIDI devices");
106
107 // Setup camera
108 midX = glucose.model.xMax/2 + 20;
109 midY = glucose.model.yMax/2;
110 midZ = glucose.model.zMax/2;
111 eyeR = -270;
112 eyeA = .15;
113 eyeY = midY + 20;
114 eyeX = midX + eyeR*sin(eyeA);
115 eyeZ = midZ + eyeR*cos(eyeA);
116 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
117 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
118 mouseWheel(mwe.getWheelRotation());
119 }});
120
121
122 println("Total setup: " + (millis() - startMillis) + "ms");
123 println("Hit the 'p' key to toggle Panda Board output");
124 }
125
126 void controllerChangeReceived(rwmidi.Controller cc) {
127 if (debugMode) {
128 println("CC: " + cc.toString());
129 }
130 }
131
132 void noteOnReceived(Note note) {
133 if (debugMode) {
134 println("Note On: " + note.toString());
135 }
136 }
137
138 void noteOffReceived(Note note) {
139 if (debugMode) {
140 println("Note Off: " + note.toString());
141 }
142 }
143
144 void logTime(String evt) {
145 int now = millis();
146 println(evt + ": " + (now - lastMillis) + "ms");
147 lastMillis = now;
148 }
149
150 void draw() {
151 // Draws the simulation and the 2D UI overlay
152 background(40);
153 color[] colors = glucose.getColors();
154 if (debugMode) {
155 debugUI.maskColors(colors);
156 }
157
158 camera(
159 eyeX, eyeY, eyeZ,
160 midX, midY, midZ,
161 0, -1, 0
162 );
163 stroke(#333333);
164 fill(#292929);
165 float yFloor = -3;
166 beginShape();
167 vertex(0, yFloor, 0);
168 vertex(glucose.model.xMax, yFloor, 0);
169 vertex(glucose.model.xMax, yFloor, glucose.model.zMax);
170 vertex(0, yFloor, glucose.model.zMax);
171 endShape(CLOSE);
172
173 noFill();
174 strokeWeight(2);
175 beginShape(POINTS);
176 for (Point p : glucose.model.points) {
177 stroke(colors[p.index]);
178 vertex(p.fx, p.fy, p.fz);
179 }
180 endShape();
181
182 // 2D Overlay
183 camera();
184 javax.media.opengl.GL gl= ((PGraphicsOpenGL)g).beginGL();
185 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
186 ((PGraphicsOpenGL)g).endGL();
187 strokeWeight(1);
188 drawUI();
189
190 if (debugMode) {
191 debugUI.draw();
192 }
193
194 // TODO(mcslee): move into GLucose engine
195 if (pandaBoardsEnabled) {
196 pandaFront.send(colors);
197 pandaRear.send(colors);
198 }
199 }
200
201 void drawUI() {
202 if (uiOn) {
203 ui.draw();
204 } else {
205 ui.drawHelpTip();
206 }
207 ui.drawFPS();
208 }
209
210 boolean uiOn = true;
211 int restoreToIndex = -1;
212
213 void keyPressed() {
214 if (mappingMode) {
215 mappingTool.keyPressed();
216 }
217 switch (key) {
218 case '-':
219 case '_':
220 frameRate(--targetFramerate);
221 break;
222 case '=':
223 case '+':
224 frameRate(++targetFramerate);
225 break;
226 case 'd':
227 debugMode = !debugMode;
228 println("Debug output: " + (debugMode ? "ON" : "OFF"));
229 break;
230 case 'm':
231 mappingMode = !mappingMode;
232 if (mappingMode) {
233 LXPattern pattern = lx.getPattern();
234 for (int i = 0; i < patterns.length; ++i) {
235 if (pattern == patterns[i]) {
236 restoreToIndex = i;
237 break;
238 }
239 }
240 ui = mappingUI;
241 lx.setPatterns(new LXPattern[] { mappingTool });
242 } else {
243 ui = controlUI;
244 lx.setPatterns(patterns);
245 lx.goIndex(restoreToIndex);
246 }
247 break;
248 case 'p':
249 pandaBoardsEnabled = !pandaBoardsEnabled;
250 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
251 break;
252 case 'u':
253 uiOn = !uiOn;
254 break;
255 }
256 }
257
258 int mx, my;
259
260 void mousePressed() {
261 if (mouseX > ui.leftPos) {
262 ui.mousePressed();
263 } else {
264 if (debugMode) {
265 debugUI.mousePressed();
266 }
267 mx = mouseX;
268 my = mouseY;
269 }
270 }
271
272 void mouseDragged() {
273 if (mouseX > ui.leftPos) {
274 ui.mouseDragged();
275 } else {
276 int dx = mouseX - mx;
277 int dy = mouseY - my;
278 mx = mouseX;
279 my = mouseY;
280 eyeA += dx*.003;
281 eyeX = midX + eyeR*sin(eyeA);
282 eyeZ = midZ + eyeR*cos(eyeA);
283 eyeY += dy;
284 }
285 }
286
287 void mouseReleased() {
288 if (mouseX > ui.leftPos) {
289 ui.mouseReleased();
290 }
291 }
292
293 void mouseWheel(int delta) {
294 eyeR = constrain(eyeR - delta, -500, -80);
295 eyeX = midX + eyeR*sin(eyeA);
296 eyeZ = midZ + eyeR*cos(eyeA);
297 }
298