Overhaul GL rendering to use Processing primitives, much faster
[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
MS
55boolean debugMode = false;
56
0a9f99cc
MS
57// Camera variables
58final float eyeR = -270;
59float eyeA, eyeX, eyeY, eyeZ, midX, midY, midZ;
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);
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
119void controllerChangeReceived(rwmidi.Controller cc) {
120 if (debugMode) {
121 println("CC: " + cc.toString());
122 }
123}
124
125void noteOnReceived(Note note) {
126 if (debugMode) {
127 println("Note On: " + note.toString());
128 }
129}
130
131void noteOffReceived(Note note) {
132 if (debugMode) {
133 println("Note Off: " + note.toString());
134 }
135}
136
49815cc0
MS
137void logTime(String evt) {
138 int now = millis();
139 println(evt + ": " + (now - lastMillis) + "ms");
140 lastMillis = now;
141}
142
143void 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
186void drawUI() {
187 if (uiOn) {
188 ui.draw();
189 } else {
190 ui.drawHelpTip();
191 }
192 ui.drawFPS();
193}
194
195boolean uiOn = true;
bf551144
MS
196int restoreToIndex = -1;
197
49815cc0 198void 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
242int mx, my;
243
244void mousePressed() {
245 if (mouseX > ui.leftPos) {
246 ui.mousePressed();
247 } else {
248 mx = mouseX;
249 my = mouseY;
250 }
251}
252
253void 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
268void mouseReleased() {
269 if (mouseX > ui.leftPos) {
270 ui.mouseReleased();
271 }
272}
49815cc0 273