-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path3d.py
More file actions
377 lines (356 loc) · 9.67 KB
/
3d.py
File metadata and controls
377 lines (356 loc) · 9.67 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# Homework for UB DMS 423 - Fall 14
# by Xiaoyu Tai
#
# Real-time Satellite Visualization
# Input Data type: TLS(Two-line element set)
# Can be found at http://www.celestrak.com/NORAD/elements/
#
# Tweak from global.py from Dr. Dave Pape
# https://gist.github.com/davepape/7324958
#
# Hi-Res Map is from NASA:
# http://earthobservatory.nasa.gov/blogs/elegantfigures/files/2011/10/land_shallow_topo_2011_8192.jpg
#
# How to control:
# Press UP/DOWN/LEFT/RIGHT to rotate the globe
# Drag the mouse to feel the
#
import ephem, datetime, urllib.request, urllib.error, urllib.parse, json, math
from pyglet.gl import *
from math import *
window = pyglet.window.Window(700,700)
keys = pyglet.window.key.KeyStateHandler()
window.push_handlers(keys)
resource = [["Space Stations", "stations"],
["NOAA Weather Satellites", "noaa"],
["GPS Operational", "gps-ops"],
["Intelsat Satellites", "intelsat"],
["Science Satellites", "science"],
["Miscellaneous Military", "military"],
["Last 30 Days' Launches", "tle-new"]]
def open_new_file(num):
global source, lines, sats, show_all_line
source = open("data/"+resource[num][1] + ".txt")
print((resource[num][0]))
# Uncomment following lines for online access
name = resource[num][1]
url = "http://www.celestrak.com/NORAD/elements/" + name +".txt"
source = urllib.request.urlopen(url).readlines()
lines = [line.decode("utf-8").replace("\r\n", "") for line in source]
sats = []
show_all_line = 0
for x in range(int(len(lines) / 3)):
e = Satellite(lines[x * 3], lines[x * 3 + 1], lines[x * 3 + 2])
e.compute()
sats.append(e)
class Satellite:
def __init__(self, name, l1, l2):
self.e = ephem.readtle(name, l1, l2)
def compute(self):
self.e.compute(datetime.datetime.utcnow())
self.long = math.degrees(float(self.e.sublong))
self.lat = math.degrees(float(self.e.sublat))
self.height = abs(int(self.e.elevation))
r = 6378150 + self.height
self.r = r / 1000000.0
self.x = -cos(radians(self.lat)) * cos(radians(self.long)) * self.r
self.y = sin(radians(self.lat)) * self.r
self.z = cos(radians(self.lat)) * sin(radians(self.long)) * self.r
self.vlists = sat_shape
self.label = pyglet.text.Label(self.e.name, y=15, anchor_x="center", color=(255,255,255,200))
def draw(self):
glLoadIdentity()
glTranslatef(0,0,-zoom+6.37815)
glRotatef(ro, 1, 0, 0)
glRotatef(rocc, 0, 0, 1)
glTranslatef(0,0,-6.37815)
glRotatef(-angle_y, 1, 0, 0)
glRotatef(angle_x, 0, 1, 0)
glTranslatef(self.x,self.y,self.z)
glColor3f(1,0,0)
glScalef(zoom/100.0, zoom/100.0, zoom/100.0)
for v in self.vlists:
v.draw(GL_TRIANGLE_STRIP)
glScalef(0.02, 0.02, 0.02)
glRotatef(-angle_x, 0, 1, 0)
glRotatef(angle_y, 1, 0, 0)
glRotatef(-rocc, 0, 0, 1)
glRotatef(-ro, 1, 0, 0)
self.label.draw()
self.draw_line()
def draw_line(self):
self.init_line()
glLoadIdentity()
# glEnable(GL_BLEND)
glTranslatef(0,0,-zoom+6.37815)
glRotatef(ro, 1, 0, 0)
glRotatef(rocc, 0, 0, 1)
glTranslatef(0,0,-6.37815)
glRotatef(-angle_y, 1, 0, 0)
glRotatef(angle_x, 0, 1, 0)
# glColor3f(1,0,0)
glColor4f(1,0,0,0)
for x in self.vline_list:
x.draw(GL_LINE_STRIP)
# glDisable(GL_BLEND)
def init_line(self):
self.lines, self.vline_list, current_line = [], [], []
for x in range(-20,20):
temp = datetime.datetime.utcnow() + datetime.timedelta(seconds=80*x)
self.e.compute(temp)
lon = math.degrees(float(self.e.sublong))
lat = math.degrees(float(self.e.sublat))
r = 6378150 + abs(int(self.e.elevation))
r = float(r)/1000000
x = -cos(radians(lat)) * cos(radians(lon)) * r
y = sin(radians(lat)) * r
z = cos(radians(lat)) * sin(radians(lon)) * r
current_line.extend((x,y,z))
self.lines.append(current_line)
for x in self.lines:
self.vline_list.append(pyglet.graphics.vertex_list(int(len(x)/3), ("v3f", x)))
def init():
global resource, tex, tex2, step, vlists
global angle_x, angle_y, v_x, v_y, v_dx, v_dy, zoom
global geo, sat_shape, ro, rocc
ro,rocc = 0,0
sat_shape = satellite_shape()
open_new_file(1)
geo = 1
tex = pyglet.image.load('assets/map_4096.jpg').get_texture()
tex2 = pyglet.image.load('assets/s.png').get_texture()
step = 9
vlists = []
r = 6.37815
for lat in range(-90,90,step):
verts = []
texc = []
noramls = []
for lon in range(-180,181,step):
x = -cos(radians(lat)) * cos(radians(lon)) * r
y = sin(radians(lat)) * r
z = cos(radians(lat)) * sin(radians(lon)) * r
s = (lon+180) / 360.0
t = (lat+90) / 180.0
verts += [x,y,z]
texc += [s,t]
noramls += [x,y,z]
x = -cos(radians((lat+step))) * cos(radians(lon)) * r
y = sin(radians((lat+step))) * r
z = cos(radians((lat+step))) * sin(radians(lon)) * r
s = (lon+180) / 360.0
t = ((lat+step)+90) / 180.0
verts += [x,y,z]
texc += [s,t]
noramls += [x,y,z]
vlist = pyglet.graphics.vertex_list(int(len(verts)/3), ('v3f', verts), ('t2f', texc), ('n3f', noramls))
vlists.append(vlist)
#42.8864468, -78.78897
angle_x = 168.78897
angle_y = -43.000809
v_x = 0
v_y = 0
v_dx = 0
v_dy = 0
zoom = 100
glEnable(GL_DEPTH_TEST)
# glEnable(GL_LIGHTING)
# glEnable(GL_LIGHT0)
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
# glOrtho(-1,1,-1,1,-1,1)
gluPerspective(10, 1, 0.1, 10000.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslatef(0,0,-zoom+6.37815)
glRotatef(ro, 1, 0, 0)
glRotatef(rocc, 0, 0, 1)
glTranslatef(0,0,-6.37815)
glRotatef(-angle_y, 1, 0, 0)
glRotatef(angle_x, 0, 1, 0)
# glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat*4)(*[0, -0.4, -1, 0]))
# glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (GLfloat*4)(*[1, 1, 1, 1]))
# glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat*4)(*[1.0, 1.0, 1.0, 0]))
glColor3f(1,1,1)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, tex.id)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
for v in vlists:
v.draw(GL_TRIANGLE_STRIP)
glDisable(GL_TEXTURE_2D)
# glBindTexture(GL_TEXTURE_2D, tex2.id)
for s in sats:
s.draw()
def update(dt):
global angle_x, angle_y, zoom, ro, rocc
# angle_x += 0.5
for x in sats:
x.compute()
keys_control()
velocity_control()
if (-angle_x+90) > 180:
angle_x = 180+90
if (-angle_x+90) < -180:
angle_x = -180+90
if zoom < 8:
zoom = 8
elif zoom > 1000:
zoom = 1000
if ro < -90:
ro = -90
elif ro > 90:
ro = 90
if rocc < -180:
rocc = 180
elif rocc > 180:
rocc = -180
if not geo:
print(("lat: "+str(-angle_y)+", long: "+str(-angle_x+90)))
print((formatted_address(geocoding(-angle_y,-angle_x+90))))
def keys_control():
global angle_x, angle_y, keys, zoom, geo, ro, rocc
if keys[pyglet.window.key.LEFT]:
angle_x += 0.5
elif keys[pyglet.window.key.RIGHT]:
angle_x -= 0.5
if keys[pyglet.window.key.DOWN]:
if angle_y + 0.5 > 88:
angle_y = 88
else:
angle_y += 0.5
elif keys[pyglet.window.key.UP]:
if angle_y - 0.5 < -88:
angle_y = -88
else:
angle_y -= 0.5
if keys[pyglet.window.key.Z]:
zoom *= 1.03
elif keys[pyglet.window.key.X]:
zoom /= 1.03
if keys[pyglet.window.key.A]:
ro += 1
elif keys[pyglet.window.key.S]:
ro -= 1
elif keys[pyglet.window.key.D]:
ro = 0
if keys[pyglet.window.key.Q]:
rocc += 1
elif keys[pyglet.window.key.W]:
rocc -= 1
elif keys[pyglet.window.key.E]:
rocc = 0
if keys[pyglet.window.key.N]:
angle_x,angle_y,zoom = -170,-30,80
if keys[pyglet.window.key.G]:
geo = 0
def velocity_control():
global angle_x, angle_y, v_x, v_y, v_dx, v_dy
global x_plus, y_plus, zoom
temp = 0.012 * zoom
angle_x += v_x
angle_y += v_y
if angle_y - 0.5 < -88:
angle_y = -88
v_dy,v_y = 0,0
elif angle_y + 0.5 > 88:
angle_y = 88
v_dy,v_y = 0,0
v_x += v_dx
v_y += v_dy
if abs(v_x) < temp:
v_dx,v_x = 0,0
else:
v_dx = x_plus
if abs(v_y) < temp:
v_dy,v_y = 0,0
else:
v_dy = y_plus
def geocoding(lat, lng):
global geo
data = json.dumps([])
APIkey = ""
url = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+str(lat)+","+str(lng)+"&key="+APIkey
req = urllib.request.Request(url, data, {'Content-Type': 'application/json'})
f = urllib.request.urlopen(req)
response = f.read()
d = json.loads(response)
f.close()
geo = 1
return d
def formatted_address(d):
if len(d['results']):
return d['results'][0]['formatted_address'].encode('utf-8')
else:
return "nowhere"
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
global angle_x, angle_y, zoom, v_x, v_y, x_plus, y_plus
temp = 0.0025 * zoom
angle_x += temp*dx
if angle_y + temp*dy > 88:
angle_y = 88
elif angle_y + temp*dy < -88:
angle_y = -88
else:
angle_y += temp*dy
if dx > 0:
x_plus = temp*-2
else:
x_plus = temp*2
if dy > 0:
y_plus = temp*-2
else:
y_plus = temp*2
v_x,v_y = temp*dx, temp*dy
@window.event
def on_mouse_scroll(x, y, scroll_x, scroll_y):
global angle_x, angle_y, zoom, v_x, v_y, x_plus, y_plus
temp = 0.005 * zoom
angle_x += temp*scroll_x*5
if angle_y + temp*-scroll_y > 88:
angle_y = 88
elif angle_y + temp*-scroll_y < -88:
angle_y = -88
else:
angle_y += temp*-scroll_y*5
if scroll_x > 0:
x_plus = temp*-2
else:
x_plus = temp*2
if -scroll_y > 0:
y_plus = temp*-2
else:
y_plus = temp*2
v_x,v_y = temp*scroll_x*5, temp*-scroll_y*5
def satellite_shape():
r = 0.08
vlists = []
verts = []
texc = []
noramls = []
verts += [-r,r,r]
verts += [-r,-r,r]
verts += [r,r,r]
verts += [r,-r,r]
verts += [r,-r,-r]
verts += [r,r,r]
verts += [r,r,-r]
verts += [-r,r,r]
verts += [-r,r,-r]
verts += [-r,-r,r]
verts += [-r,-r,-r]
verts += [r,-r,r]
verts += [r,-r,-r]
verts += [r,r,-r]
verts += [-r,-r,-r]
verts += [-r,r,-r]
noramls = verts
vlist = pyglet.graphics.vertex_list(int(len(verts)/3), ('v3f', verts), ('n3f', noramls))
vlists.append(vlist)
return vlists
init()
pyglet.clock.schedule_interval(update,1/30.0)
pyglet.app.run()