Commit | Line | Data |
---|---|---|
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 | ||
16 | import glucose.*; | |
17 | import glucose.control.*; | |
3f8be614 | 18 | import glucose.effect.*; |
f3f5a876 | 19 | import glucose.model.*; |
49815cc0 | 20 | import glucose.pattern.*; |
f3f5a876 | 21 | import glucose.transform.*; |
49815cc0 | 22 | import glucose.transition.*; |
49815cc0 | 23 | import heronarts.lx.*; |
3f8be614 | 24 | import heronarts.lx.control.*; |
49815cc0 | 25 | import heronarts.lx.effect.*; |
49815cc0 | 26 | import heronarts.lx.modulator.*; |
f3f5a876 | 27 | import heronarts.lx.pattern.*; |
49815cc0 MS |
28 | import heronarts.lx.transition.*; |
29 | import ddf.minim.*; | |
30 | import ddf.minim.analysis.*; | |
31 | import processing.opengl.*; | |
5d70e4d7 | 32 | import rwmidi.*; |
49815cc0 MS |
33 | |
34 | final int VIEWPORT_WIDTH = 900; | |
35 | final int VIEWPORT_HEIGHT = 700; | |
0e3c5542 | 36 | |
0a9f99cc | 37 | int targetFramerate = 45; |
49815cc0 MS |
38 | |
39 | int startMillis, lastMillis; | |
40 | GLucose glucose; | |
41 | HeronLX lx; | |
bf551144 | 42 | MappingTool mappingTool; |
49815cc0 MS |
43 | LXPattern[] patterns; |
44 | LXTransition[] transitions; | |
45 | LXEffect[] effects; | |
46 | OverlayUI ui; | |
bf551144 MS |
47 | ControlUI controlUI; |
48 | MappingUI mappingUI; | |
e73ef85d MS |
49 | PandaDriver pandaFront; |
50 | PandaDriver pandaRear; | |
bf551144 | 51 | boolean mappingMode = false; |
e73ef85d MS |
52 | |
53 | boolean pandaBoardsEnabled = false; | |
49815cc0 | 54 | |
cc9fcf4b MS |
55 | boolean debugMode = false; |
56 | ||
0a9f99cc MS |
57 | // Camera variables |
58 | final float eyeR = -270; | |
59 | float eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ; | |
60 | ||
49815cc0 MS |
61 | void 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); | |
49815cc0 | 97 | logTime("Built overlay UI"); |
1ecdb44a | 98 | |
49815cc0 | 99 | // MIDI devices |
cc9fcf4b MS |
100 | for (MidiInputDevice d : RWMidi.getInputDevices()) { |
101 | d.createInput(this); | |
102 | } | |
103 | SCMidiDevices.initializeStandardDevices(glucose); | |
3f8be614 | 104 | logTime("Setup MIDI devices"); |
49815cc0 | 105 | |
0a9f99cc MS |
106 | // Setup camera |
107 | midX = glucose.model.xMax/2 + 20; | |
108 | midY = glucose.model.yMax/2; | |
109 | midZ = glucose.model.zMax/2; | |
110 | eyeA = .15; | |
111 | eyeY = midY + 20; | |
112 | eyeX = midX + eyeR*sin(eyeA); | |
113 | eyeZ = midZ + eyeR*cos(eyeA); | |
114 | ||
49815cc0 | 115 | println("Total setup: " + (millis() - startMillis) + "ms"); |
e73ef85d | 116 | println("Hit the 'p' key to toggle Panda Board output"); |
49815cc0 MS |
117 | } |
118 | ||
cc9fcf4b MS |
119 | void controllerChangeReceived(rwmidi.Controller cc) { |
120 | if (debugMode) { | |
121 | println("CC: " + cc.toString()); | |
122 | } | |
123 | } | |
124 | ||
125 | void noteOnReceived(Note note) { | |
126 | if (debugMode) { | |
127 | println("Note On: " + note.toString()); | |
128 | } | |
129 | } | |
130 | ||
131 | void noteOffReceived(Note note) { | |
132 | if (debugMode) { | |
133 | println("Note Off: " + note.toString()); | |
134 | } | |
135 | } | |
136 | ||
49815cc0 MS |
137 | void logTime(String evt) { |
138 | int now = millis(); | |
139 | println(evt + ": " + (now - lastMillis) + "ms"); | |
140 | lastMillis = now; | |
141 | } | |
142 | ||
143 | void draw() { | |
0a9f99cc MS |
144 | // Draws the simulation and the 2D UI overlay |
145 | background(40); | |
146 | color[] colors = glucose.getColors(); | |
147 | camera( | |
148 | eyeX, eyeY, eyeZ, | |
149 | midX, midY, midZ, | |
150 | 0, -1, 0 | |
151 | ); | |
152 | stroke(#333333); | |
153 | fill(#292929); | |
154 | float yFloor = -3; | |
155 | beginShape(); | |
156 | vertex(0, yFloor, 0); | |
157 | vertex(glucose.model.xMax, yFloor, 0); | |
158 | vertex(glucose.model.xMax, yFloor, glucose.model.zMax); | |
159 | vertex(0, yFloor, glucose.model.zMax); | |
160 | endShape(CLOSE); | |
161 | ||
162 | noFill(); | |
163 | strokeWeight(2); | |
164 | beginShape(POINTS); | |
165 | for (Point p : glucose.model.points) { | |
166 | stroke(colors[p.index]); | |
167 | vertex(p.fx, p.fy, p.fz); | |
168 | } | |
169 | endShape(); | |
170 | ||
171 | // 2D Overlay | |
172 | camera(); | |
173 | javax.media.opengl.GL gl= ((PGraphicsOpenGL)g).beginGL(); | |
174 | gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT); | |
175 | ((PGraphicsOpenGL)g).endGL(); | |
176 | strokeWeight(1); | |
177 | drawUI(); | |
e73ef85d MS |
178 | |
179 | // TODO(mcslee): move into GLucose engine | |
180 | if (pandaBoardsEnabled) { | |
e73ef85d MS |
181 | pandaFront.send(colors); |
182 | pandaRear.send(colors); | |
183 | } | |
49815cc0 MS |
184 | } |
185 | ||
186 | void drawUI() { | |
187 | if (uiOn) { | |
188 | ui.draw(); | |
189 | } else { | |
190 | ui.drawHelpTip(); | |
191 | } | |
192 | ui.drawFPS(); | |
193 | } | |
194 | ||
195 | boolean uiOn = true; | |
bf551144 MS |
196 | int restoreToIndex = -1; |
197 | ||
49815cc0 | 198 | void keyPressed() { |
bf551144 MS |
199 | if (mappingMode) { |
200 | mappingTool.keyPressed(); | |
201 | } | |
3f8be614 | 202 | switch (key) { |
0e3c5542 MS |
203 | case '-': |
204 | case '_': | |
205 | frameRate(--targetFramerate); | |
206 | break; | |
207 | case '=': | |
208 | case '+': | |
209 | frameRate(++targetFramerate); | |
210 | break; | |
cc9fcf4b MS |
211 | case 'd': |
212 | debugMode = !debugMode; | |
213 | println("Debug output: " + (debugMode ? "ON" : "OFF")); | |
bf551144 MS |
214 | case 'm': |
215 | mappingMode = !mappingMode; | |
216 | if (mappingMode) { | |
217 | LXPattern pattern = lx.getPattern(); | |
218 | for (int i = 0; i < patterns.length; ++i) { | |
219 | if (pattern == patterns[i]) { | |
220 | restoreToIndex = i; | |
221 | break; | |
222 | } | |
223 | } | |
224 | ui = mappingUI; | |
225 | lx.setPatterns(new LXPattern[] { mappingTool }); | |
226 | } else { | |
227 | ui = controlUI; | |
228 | lx.setPatterns(patterns); | |
229 | lx.goIndex(restoreToIndex); | |
230 | } | |
231 | break; | |
e73ef85d MS |
232 | case 'p': |
233 | pandaBoardsEnabled = !pandaBoardsEnabled; | |
234 | println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF")); | |
cc9fcf4b | 235 | break; |
3f8be614 MS |
236 | case 'u': |
237 | uiOn = !uiOn; | |
238 | break; | |
49815cc0 MS |
239 | } |
240 | } | |
241 | ||
0a9f99cc MS |
242 | int mx, my; |
243 | ||
244 | void mousePressed() { | |
245 | if (mouseX > ui.leftPos) { | |
246 | ui.mousePressed(); | |
247 | } else { | |
248 | mx = mouseX; | |
249 | my = mouseY; | |
250 | } | |
251 | } | |
252 | ||
253 | void mouseDragged() { | |
254 | if (mouseX > ui.leftPos) { | |
255 | ui.mouseDragged(); | |
256 | } else { | |
257 | int dx = mouseX - mx; | |
258 | int dy = mouseY - my; | |
259 | mx = mouseX; | |
260 | my = mouseY; | |
261 | eyeA += dx*.003; | |
262 | eyeX = midX + eyeR*sin(eyeA); | |
263 | eyeZ = midZ + eyeR*cos(eyeA); | |
264 | eyeY += dy; | |
265 | } | |
266 | } | |
267 | ||
268 | void mouseReleased() { | |
269 | if (mouseX > ui.leftPos) { | |
270 | ui.mouseReleased(); | |
271 | } | |
272 | } | |
49815cc0 | 273 |