Overhaul GL rendering to use Processing primitives, much faster
[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
57 // Camera variables
58 final float eyeR = -270;
59 float 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 logTime("Built overlay UI");
98
99 // MIDI devices
100 for (MidiInputDevice d : RWMidi.getInputDevices()) {
101 d.createInput(this);
102 }
103 SCMidiDevices.initializeStandardDevices(glucose);
104 logTime("Setup MIDI devices");
105
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
115 println("Total setup: " + (millis() - startMillis) + "ms");
116 println("Hit the 'p' key to toggle Panda Board output");
117 }
118
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
137 void logTime(String evt) {
138 int now = millis();
139 println(evt + ": " + (now - lastMillis) + "ms");
140 lastMillis = now;
141 }
142
143 void draw() {
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();
178
179 // TODO(mcslee): move into GLucose engine
180 if (pandaBoardsEnabled) {
181 pandaFront.send(colors);
182 pandaRear.send(colors);
183 }
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;
196 int restoreToIndex = -1;
197
198 void keyPressed() {
199 if (mappingMode) {
200 mappingTool.keyPressed();
201 }
202 switch (key) {
203 case '-':
204 case '_':
205 frameRate(--targetFramerate);
206 break;
207 case '=':
208 case '+':
209 frameRate(++targetFramerate);
210 break;
211 case 'd':
212 debugMode = !debugMode;
213 println("Debug output: " + (debugMode ? "ON" : "OFF"));
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;
232 case 'p':
233 pandaBoardsEnabled = !pandaBoardsEnabled;
234 println("PandaBoard Output: " + (pandaBoardsEnabled ? "ON" : "OFF"));
235 break;
236 case 'u':
237 uiOn = !uiOn;
238 break;
239 }
240 }
241
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 }
273