fabric
[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
0a9f99cc 37int targetFramerate = 45;
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
49815cc0
MS
61void setup() {
62 startMillis = lastMillis = millis();
63
64 // Initialize the Processing graphics environment
65 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 66 frameRate(targetFramerate);
3f8be614 67 noSmooth();
49815cc0
MS
68 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
69 logTime("Created viewport");
70
71 // Create the GLucose engine to run the cubes
1ecdb44a 72 glucose = new GLucose(this, new SCMapping());
49815cc0 73 lx = glucose.lx;
cc9fcf4b 74 lx.enableKeyboardTempo();
49815cc0
MS
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");
cc9fcf4b 82 glucose.setTransitions(transitions = transitions(glucose));
49815cc0 83 logTime("Built transitions");
e73ef85d
MS
84
85 // Build output driver
86 int[][] frontChannels = glucose.mapping.buildFrontChannelList();
87 int[][] rearChannels = glucose.mapping.buildRearChannelList();
88 int[][] flippedRGB = glucose.mapping.buildFlippedRGBList();
2bae07c9 89 mappingTool = new MappingTool(glucose, frontChannels, rearChannels);
e73ef85d
MS
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");
49815cc0
MS
93
94 // Build overlay UI
bf551144
MS
95 ui = controlUI = new ControlUI();
96 mappingUI = new MappingUI(mappingTool);
554e38ff 97 debugUI = new DebugUI(frontChannels, rearChannels);
49815cc0 98 logTime("Built overlay UI");
1ecdb44a 99
49815cc0 100 // MIDI devices
cc9fcf4b
MS
101 for (MidiInputDevice d : RWMidi.getInputDevices()) {
102 d.createInput(this);
103 }
104 SCMidiDevices.initializeStandardDevices(glucose);
3f8be614 105 logTime("Setup MIDI devices");
554e38ff 106
0a9f99cc
MS
107 // Setup camera
108 midX = glucose.model.xMax/2 + 20;
109 midY = glucose.model.yMax/2;
110 midZ = glucose.model.zMax/2;
bda5421d 111 eyeR = -270;
0a9f99cc
MS
112 eyeA = .15;
113 eyeY = midY + 20;
114 eyeX = midX + eyeR*sin(eyeA);
115 eyeZ = midZ + eyeR*cos(eyeA);
bda5421d
MS
116 addMouseWheelListener(new java.awt.event.MouseWheelListener() {
117 public void mouseWheelMoved(java.awt.event.MouseWheelEvent mwe) {
118 mouseWheel(mwe.getWheelRotation());
119 }});
120
0a9f99cc 121
49815cc0 122 println("Total setup: " + (millis() - startMillis) + "ms");
e73ef85d 123 println("Hit the 'p' key to toggle Panda Board output");
49815cc0
MS
124}
125
cc9fcf4b
MS
126void controllerChangeReceived(rwmidi.Controller cc) {
127 if (debugMode) {
128 println("CC: " + cc.toString());
129 }
130}
131
132void noteOnReceived(Note note) {
133 if (debugMode) {
134 println("Note On: " + note.toString());
135 }
136}
137
138void noteOffReceived(Note note) {
139 if (debugMode) {
140 println("Note Off: " + note.toString());
141 }
142}
143
49815cc0
MS
144void logTime(String evt) {
145 int now = millis();
146 println(evt + ": " + (now - lastMillis) + "ms");
147 lastMillis = now;
148}
149
150void draw() {
0a9f99cc
MS
151 // Draws the simulation and the 2D UI overlay
152 background(40);
153 color[] colors = glucose.getColors();
554e38ff
MS
154 if (debugMode) {
155 debugUI.maskColors(colors);
156 }
157
0a9f99cc
MS
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();
e73ef85d 189
554e38ff
MS
190 if (debugMode) {
191 debugUI.draw();
192 }
193
e73ef85d
MS
194 // TODO(mcslee): move into GLucose engine
195 if (pandaBoardsEnabled) {
e73ef85d
MS
196 pandaFront.send(colors);
197 pandaRear.send(colors);
198 }
49815cc0
MS
199}
200
201void drawUI() {
202 if (uiOn) {
203 ui.draw();
204 } else {
205 ui.drawHelpTip();
206 }
207 ui.drawFPS();
208}
209
210boolean uiOn = true;
bf551144
MS
211int restoreToIndex = -1;
212
49815cc0 213void keyPressed() {
bf551144
MS
214 if (mappingMode) {
215 mappingTool.keyPressed();
216 }
3f8be614 217 switch (key) {
0e3c5542
MS
218 case '-':
219 case '_':
220 frameRate(--targetFramerate);
221 break;
222 case '=':
223 case '+':
224 frameRate(++targetFramerate);
225 break;
cc9fcf4b
MS
226 case 'd':
227 debugMode = !debugMode;
228 println("Debug output: " + (debugMode ? "ON" : "OFF"));
554e38ff 229 break;
bf551144
MS
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;
e73ef85d
MS
248 case 'p':
249 pandaBoardsEnabled = !pandaBoardsEnabled;
250 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
cc9fcf4b 251 break;
3f8be614
MS
252 case 'u':
253 uiOn = !uiOn;
254 break;
49815cc0
MS
255 }
256}
257
0a9f99cc
MS
258int mx, my;
259
260void mousePressed() {
261 if (mouseX > ui.leftPos) {
262 ui.mousePressed();
263 } else {
554e38ff
MS
264 if (debugMode) {
265 debugUI.mousePressed();
266 }
0a9f99cc
MS
267 mx = mouseX;
268 my = mouseY;
269 }
270}
271
272void 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
287void mouseReleased() {
288 if (mouseX > ui.leftPos) {
289 ui.mouseReleased();
290 }
291}
bda5421d
MS
292
293void mouseWheel(int delta) {
294 eyeR = constrain(eyeR - delta, -500, -80);
295 eyeX = midX + eyeR*sin(eyeA);
296 eyeZ = midZ + eyeR*cos(eyeA);
297}
49815cc0 298