Basic changes to support new layout
[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 = -2;
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 // println(p.fx + ":" + p.fy + ":" + p.fz);
180 }
181 endShape();
182
183 // 2D Overlay
184 camera();
185 javax.media.opengl.GL gl= ((PGraphicsOpenGL)g).beginGL();
186 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
187 ((PGraphicsOpenGL)g).endGL();
188 strokeWeight(1);
189 drawUI();
190
191 if (debugMode) {
192 debugUI.draw();
193 }
194
195 // TODO(mcslee): move into GLucose engine
196 if (pandaBoardsEnabled) {
197 pandaFront.send(colors);
198 pandaRear.send(colors);
199 }
200 }
201
202 void drawUI() {
203 if (uiOn) {
204 ui.draw();
205 } else {
206 ui.drawHelpTip();
207 }
208 ui.drawFPS();
209 }
210
211 boolean uiOn = true;
212 int restoreToIndex = -1;
213
214 void keyPressed() {
215 if (mappingMode) {
216 mappingTool.keyPressed();
217 }
218 switch (key) {
219 case '-':
220 case '_':
221 frameRate(--targetFramerate);
222 break;
223 case '=':
224 case '+':
225 frameRate(++targetFramerate);
226 break;
227 case 'd':
228 debugMode = !debugMode;
229 println("Debug output: " + (debugMode ? "ON" : "OFF"));
230 break;
231 case 'm':
232 mappingMode = !mappingMode;
233 if (mappingMode) {
234 LXPattern pattern = lx.getPattern();
235 for (int i = 0; i < patterns.length; ++i) {
236 if (pattern == patterns[i]) {
237 restoreToIndex = i;
238 break;
239 }
240 }
241 ui = mappingUI;
242 lx.setPatterns(new LXPattern[] { mappingTool });
243 } else {
244 ui = controlUI;
245 lx.setPatterns(patterns);
246 lx.goIndex(restoreToIndex);
247 }
248 break;
249 case 'p':
250 pandaBoardsEnabled = !pandaBoardsEnabled;
251 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
252 break;
253 case 'u':
254 uiOn = !uiOn;
255 break;
256 }
257 }
258
259 int mx, my;
260
261 void mousePressed() {
262 if (mouseX > ui.leftPos) {
263 ui.mousePressed();
264 } else {
265 if (debugMode) {
266 debugUI.mousePressed();
267 }
268 mx = mouseX;
269 my = mouseY;
270 }
271 }
272
273 void mouseDragged() {
274 if (mouseX > ui.leftPos) {
275 ui.mouseDragged();
276 } else {
277 int dx = mouseX - mx;
278 int dy = mouseY - my;
279 mx = mouseX;
280 my = mouseY;
281 eyeA += dx*.003;
282 eyeX = midX + eyeR*sin(eyeA);
283 eyeZ = midZ + eyeR*cos(eyeA);
284 eyeY += dy;
285 }
286 }
287
288 void mouseReleased() {
289 if (mouseX > ui.leftPos) {
290 ui.mouseReleased();
291 }
292 }
293
294 void mouseWheel(int delta) {
295 eyeR = constrain(eyeR - delta, -500, -80);
296 eyeX = midX + eyeR*sin(eyeA);
297 eyeZ = midZ + eyeR*cos(eyeA);
298 }
299