-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomeStar2Simulator.pde
176 lines (138 loc) · 3.95 KB
/
DomeStar2Simulator.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import processing.opengl.*;
import hypermedia.net.*;
import java.util.concurrent.*;
int DOME_RADIUS = 12;
int strips = 40; // Number of strips around the circumference of the sphere
int lights_per_strip = 32*5; // Number of lights along the strip
int packet_length = strips*lights_per_strip*3 + 1;
Boolean demoMode = true;
BlockingQueue newImageQueue;
color[][] frameBuffer;
DemoTransmitter demoTransmitter;
UDP udp;
PeasyCam pCamera;
BlinkeyLights blinkeyLights;
Dome dome;
Hud hud;
ImageHud imageHud;
PFont font;
PImage groundTexture;
void setup() {
size(512, 425, P3D);
colorMode(RGB,255);
frameRate(60);
pCamera = new PeasyCam(this, 0, -50, 0, 100);
pCamera.setMinimumDistance(.2);
pCamera.setMaximumDistance(150*10);
pCamera.setSuppressRollRotationMode();
pCamera.rotateX(-.4);
pCamera.setWheelScale(0.05);
newImageQueue = new ArrayBlockingQueue(2);
udp = new UDP( this, 58082 );
udp.listen( true );
font = loadFont("Serif-24.vlw");
hud = new Hud();
dome = new Dome(DOME_RADIUS);
blinkeyLights = new BlinkeyLights(DOME_RADIUS, strips, lights_per_strip);
imageHud = new ImageHud(20, height-160-20, strips, lights_per_strip);
groundTexture = loadImage("Lost Lake.jpg");
demoTransmitter = new DemoTransmitter();
demoTransmitter.start();
}
int animationStep = 0;
int maxConvertedByte = 0;
int convertByte(byte b) {
int c = (b<0) ? 256+b : b;
if (c > maxConvertedByte) {
maxConvertedByte = c;
println("Max Converted Byte is now " + c);
}
return c;
}
void receive(byte[] data, String ip, int port) {
if (demoMode) {
println("Started receiving data from " + ip + ". Demo mode disabled.");
demoMode = false;
}
if (data[0] == 2) {
// We got a new mode, so copy it out
String modeName = new String(data);
hud.setHudText(modeName);
return;
}
if (data[0] != 1) {
println("Packet header mismatch. Expected 1, got " + data[0]);
return;
}
if (data.length != packet_length) {
println("Packet size mismatch. Expected "+packet_length+", got " + data.length);
return;
}
if (newImageQueue.size() > 0) {
//println("Buffer full, dropping frame!");
return;
}
color[] newImage = new color[strips*lights_per_strip];
for (int i=0; i< strips*lights_per_strip; i++) {
// Processing doesn't like it when you call the color function while in an event
// go figure
newImage[i] = (int)(0xff<<24 | convertByte(data[i*3 + 1])<<16) |
(convertByte(data[i*3 + 2])<<8) |
(convertByte(data[i*3 + 3]));
}
try {
newImageQueue.put(newImage);
}
catch( InterruptedException e ) {
println("Interrupted Exception caught");
}
}
void drawGround() {
stroke(92, 51);
fill(92, 51);
int tilefactor = 10;
float bound = DOME_RADIUS*10*4;
for (int x = 0; x < tilefactor; x++) {
for (int y = 0; y < tilefactor; y++) {
pushMatrix();
translate(0, 0.5, 0);
translate(bound/tilefactor*x-bound/2, 0, bound/tilefactor*y-bound/2);
beginShape();
texture(groundTexture);
textureMode(NORMAL);
vertex(0, .5, 0, 0, 0);
vertex(bound/tilefactor, .5, 0, 1, 0);
vertex(bound/tilefactor, .5, bound/tilefactor, 1, 1);
vertex(0, .5, bound/tilefactor, 0, 1);
endShape();
popMatrix();
}
}
}
void drawFPS() {
pCamera.beginHUD();
noLights();
fill(255, 255, 255);
textFont(font);
textAlign(CENTER);
text(int(frameRate), width - 50, height - 50);
pCamera.endHUD();
}
void draw() {
background(0);
lights();
drawGround();
dome.draw();
blinkeyLights.draw();
hud.draw();
imageHud.draw();
drawFPS();
if (newImageQueue.size() > 0) {
color[] newImage = (color[])newImageQueue.remove();
blinkeyLights.update(newImage);
imageHud.update(newImage);
}
}