forked from arda-guler/miniLanding3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.py
403 lines (313 loc) · 15.1 KB
/
graphics.py
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy
import random
from math_utils import *
from ui_text import *
def drawOrigin():
glBegin(GL_LINES)
glColor(1,0,0)
glVertex3f(0,0,0)
glVertex3f(0,1000,0)
glColor(0,1,0)
glVertex3f(0,0,0)
glVertex3f(1000,0,0)
glColor(0,0,1)
glVertex3f(0,0,0)
glVertex3f(0,0,1000)
glEnd()
def spherical2cartesian(rho, theta, phi):
x = math.cos(theta) * math.sin(phi) * rho
y = math.sin(theta) * math.sin(phi) * rho
z = math.cos(phi) * rho
return [x, y, z]
def initBackground(star_num = 100):
stars = []
for i in range(star_num):
rho = 200000
theta = random.uniform(0, 3.14)
phi = random.uniform(0, 3.14)
rand_pos = spherical2cartesian(rho, theta, phi)
stars.append([rand_pos[0], rand_pos[1], rand_pos[2]])
return stars
def drawBackground(stars):
glPushMatrix()
glColor(1, 1, 1)
glBegin(GL_POINTS)
for star in stars:
glVertex3f(star[0],star[1],star[2])
glEnd()
glPopMatrix()
def drawVessel(v):
# here we go
glPushMatrix()
# change color we render with
glColor(0.3, 0.3, 0.35)
# put us in correct position
glTranslatef(v.get_pos()[0], v.get_pos()[1], v.get_pos()[2])
# actually render model now, with triangles
for mesh in v.model.mesh_list:
glBegin(GL_POLYGON)
for face in mesh.faces:
for vertex_i in face:
vertex_i = v.model.vertices[vertex_i]
vertex_i = numpy.matmul(numpy.array(vertex_i), v.get_orient())
vertex_i = [vertex_i[0], vertex_i[1], vertex_i[2]]
glVertex3f(vertex_i[0], vertex_i[1], vertex_i[2])
glEnd()
# engine plume cone
if v.get_main_engine():
glPushMatrix()
glColor(0.9, 0.7, 0)
glBegin(GL_LINES)
plume_start = [0,-5,0]
plume_start = numpy.matmul(numpy.array(plume_start), v.get_orient())
plume_end_1 = [0.05*v.get_percent_thrust(), -0.15*v.get_percent_thrust(), 0]
plume_end_1 = numpy.matmul(numpy.array(plume_end_1), v.get_orient())
glVertex3f(plume_start[0], plume_start[1], plume_start[2])
glVertex3f(plume_end_1[0], plume_end_1[1], plume_end_1[2])
plume_end_2 = [-0.05*v.get_percent_thrust(), -0.15*v.get_percent_thrust(), 0]
plume_end_2 = numpy.matmul(numpy.array(plume_end_2), v.get_orient())
glVertex3f(plume_start[0], plume_start[1], plume_start[2])
glVertex3f(plume_end_2[0], plume_end_2[1], plume_end_2[2])
plume_end_3 = [0, -0.15*v.get_percent_thrust(), 0.05*v.get_percent_thrust()]
plume_end_3 = numpy.matmul(numpy.array(plume_end_3), v.get_orient())
glVertex3f(plume_start[0], plume_start[1], plume_start[2])
glVertex3f(plume_end_3[0], plume_end_3[1], plume_end_3[2])
plume_end_4 = [0, -0.15*v.get_percent_thrust(), -0.05*v.get_percent_thrust()]
plume_end_4 = numpy.matmul(numpy.array(plume_end_4), v.get_orient())
glVertex3f(plume_start[0], plume_start[1], plume_start[2])
glVertex3f(plume_end_4[0], plume_end_4[1], plume_end_4[2])
glVertex3f(plume_end_1[0], plume_end_1[1], plume_end_1[2])
glVertex3f(plume_end_3[0], plume_end_3[1], plume_end_3[2])
glVertex3f(plume_end_3[0], plume_end_3[1], plume_end_3[2])
glVertex3f(plume_end_2[0], plume_end_2[1], plume_end_2[2])
glVertex3f(plume_end_2[0], plume_end_2[1], plume_end_2[2])
glVertex3f(plume_end_4[0], plume_end_4[1], plume_end_4[2])
glVertex3f(plume_end_4[0], plume_end_4[1], plume_end_4[2])
glVertex3f(plume_end_1[0], plume_end_1[1], plume_end_1[2])
glEnd()
glPopMatrix()
# now get out
glPopMatrix()
def drawTerrain(t, current_ship, render_dist):
glPushMatrix()
glTranslatef(t.get_center()[0], t.get_center()[1], t.get_center()[2])
glColor(0.8, 0.8, 0.8)
glBegin(GL_POLYGON)
x_lines_num = t.x_lines_num
z_lines_num = t.z_lines_num
x_spacing = t.x_spacing
z_spacing = t.z_spacing
rect_render_dist = 250*render_dist + current_ship.get_pos()[1] * render_dist
rel_x = current_ship.get_pos()[0] - t.center[0]
rel_z = current_ship.get_pos()[2] - t.center[2]
render_min_x = rel_x - rect_render_dist
render_max_x = rel_x + rect_render_dist
x_min_index = max(int(x_lines_num/2 + render_min_x/x_spacing), 0)
x_max_index = max(int(x_lines_num/2 + render_max_x/x_spacing), x_lines_num-1)
render_min_z = rel_z - rect_render_dist
render_max_z = rel_z + rect_render_dist
z_min_index = max(int(z_lines_num/2 + render_min_z/z_spacing), 0)
z_max_index = max(int(z_lines_num/2 + render_max_z/z_spacing), z_lines_num-1)
#print(x_min_index, x_max_index)
#print(z_min_index, z_max_index)
indices = []
ai = 0
for a in range(z_min_index, z_max_index):
indices.append([])
for b in range(x_min_index, x_max_index):
indices[ai].append(a * x_lines_num + b)
ai += 1
for z in indices:
for x in z:
#print(x)
try:
glVertex3f(t.vertices[x][0], t.vertices[x][1], t.vertices[x][2])
glVertex3f(t.vertices[x+x_lines_num][0], t.vertices[x+x_lines_num][1], t.vertices[x+x_lines_num][2])
except:
pass
## for a in range(t.z_lines_num):
## for b in range(t.x_lines_num):
## # why the hell is drawing lines so bloody expensive??
## # anyway, don't draw those that are too far away
## if (abs(current_ship.get_pos()[0] - t.vertices[a*t.x_lines_num+b][0]) < 250*render_dist + current_ship.get_pos()[1] * render_dist and
## abs(current_ship.get_pos()[2] - t.vertices[a*t.x_lines_num+b][2]) < 250*render_dist + current_ship.get_pos()[1] * render_dist):
## if not b+1 == t.x_lines_num:
## glVertex3f(t.vertices[a*t.x_lines_num+b][0], t.vertices[a*t.x_lines_num+b][1], t.vertices[a*t.x_lines_num+b][2])
## #glVertex3f(t.vertices[a*t.x_lines_num+b+1][0], t.vertices[a*t.x_lines_num+b+1][1], t.vertices[a*t.x_lines_num+b+1][2])
##
## if not a-1 < 0:
## glVertex3f(t.vertices[(a-1)*t.x_lines_num+b][0], t.vertices[(a-1)*t.x_lines_num+b][1], t.vertices[(a-1)*t.x_lines_num+b][2])
## glVertex3f(t.vertices[a*t.x_lines_num+b][0], t.vertices[a*t.x_lines_num+b][1], t.vertices[a*t.x_lines_num+b][2])
glEnd()
glColor(0.0, 0.5, 0.0)
for z in indices:
glBegin(GL_LINE_STRIP)
for x in z:
#print(x)
try:
glVertex3f(t.vertices[x][0], t.vertices[x][1], t.vertices[x][2])
glVertex3f(t.vertices[x+x_lines_num+1][0], t.vertices[x+x_lines_num+1][1], t.vertices[x+x_lines_num+1][2])
except:
pass
## for a in range(t.z_lines_num):
## glBegin(GL_LINE_STRIP)
## for b in range(t.x_lines_num):
## # why the hell is drawing lines so bloody expensive??
## # anyway, don't draw those that are too far away
## if (abs(current_ship.get_pos()[0] - t.vertices[a*t.x_lines_num+b][0]) < 100 * render_dist + current_ship.get_pos()[1] * 0.625 * render_dist and
## abs(current_ship.get_pos()[2] - t.vertices[a*t.x_lines_num+b][2]) < 100 * render_dist + current_ship.get_pos()[1] * 0.625 * render_dist):
## if not b+1 == t.x_lines_num:
## glVertex3f(t.vertices[a*t.x_lines_num+b][0], t.vertices[a*t.x_lines_num+b][1], t.vertices[a*t.x_lines_num+b][2])
## #glVertex3f(t.vertices[a*t.x_lines_num+b+1][0], t.vertices[a*t.x_lines_num+b+1][1], t.vertices[a*t.x_lines_num+b+1][2])
##
## if not a-1 < 0:
## glVertex3f(t.vertices[(a-1)*t.x_lines_num+b][0], t.vertices[(a-1)*t.x_lines_num+b][1], t.vertices[(a-1)*t.x_lines_num+b][2])
## glVertex3f(t.vertices[a*t.x_lines_num+b][0], t.vertices[a*t.x_lines_num+b][1], t.vertices[a*t.x_lines_num+b][2])
glEnd()
glPopMatrix()
def drawPoint2D(x, y, color, camera):
glPushMatrix()
glTranslate(-camera.get_pos()[0],
-camera.get_pos()[1],
-camera.get_pos()[2])
glColor(color[0], color[1], color[2])
glBegin(GL_POINTS)
x1 = x * 100
y1 = y * 100
glVertex3f((x1) * camera.get_orient()[0][0] + (y1) * camera.get_orient()[1][0] + (-1000) * camera.get_orient()[2][0],
(x1) * camera.get_orient()[0][1] + (y1) * camera.get_orient()[1][1] + (-1000) * camera.get_orient()[2][1],
(x1) * camera.get_orient()[0][2] + (y1) * camera.get_orient()[1][2] + (-1000) * camera.get_orient()[2][2])
glEnd()
glPopMatrix()
def drawLine2D(x1, y1, x2, y2, color, camera):
glPushMatrix()
glTranslate(-camera.get_pos()[0],
-camera.get_pos()[1],
-camera.get_pos()[2])
glColor(color[0], color[1], color[2])
glBegin(GL_LINES)
x1 = x1 * 100
y1 = y1 * 100
x2 = x2 * 100
y2 = y2 * 100
glVertex3f((x1) * camera.get_orient()[0][0] + (y1) * camera.get_orient()[1][0] + (-1000) * camera.get_orient()[2][0],
(x1) * camera.get_orient()[0][1] + (y1) * camera.get_orient()[1][1] + (-1000) * camera.get_orient()[2][1],
(x1) * camera.get_orient()[0][2] + (y1) * camera.get_orient()[1][2] + (-1000) * camera.get_orient()[2][2])
glVertex3f((x2) * camera.get_orient()[0][0] + (y2) * camera.get_orient()[1][0] + (-1000) * camera.get_orient()[2][0],
(x2) * camera.get_orient()[0][1] + (y2) * camera.get_orient()[1][1] + (-1000) * camera.get_orient()[2][1],
(x2) * camera.get_orient()[0][2] + (y2) * camera.get_orient()[1][2] + (-1000) * camera.get_orient()[2][2])
glEnd()
glPopMatrix()
def drawRectangle2D(x1, y1, x2, y2, color, camera):
drawLine2D(x1, y1, x2, y1, color, camera)
drawLine2D(x1, y1, x1, y2, color, camera)
drawLine2D(x2, y1, x2, y2, color, camera)
drawLine2D(x1, y2, x2, y2, color, camera)
def drawNumbers(camera, ship, autopilot, terrain, at_descent_rate):
# Velocity
vel_x = "VX " + str(round(ship.get_vel()[0], 3))
vel_y = "VY " + str(round(ship.get_vel()[1], 3))
vel_z = "VZ " + str(round(ship.get_vel()[2], 3))
render_AN(vel_x, (0,1,1), (4.45, 3.2), camera, font_size=0.1)
render_AN(vel_y, (0,1,1), (4.45, 2.7), camera, font_size=0.1)
render_AN(vel_z, (0,1,1), (4.45, 2.2), camera, font_size=0.1)
# Altitude
if ship.get_alt_quick(terrain) > 50:
alt = "ALT " + str(round(ship.get_alt_quick(terrain), 3))
else:
alt = "ALT " + str(round(ship.get_alt(terrain), 3))
render_AN(alt, (0,1,0), (4.45, 0), camera, font_size=0.1)
# AP descent rate cmd
adr = "APDR " + str(round(at_descent_rate, 1))
render_AN(adr, (1,0,1), (8, 2.7), camera, font_size=0.1)
# Propellant
prop = "PROP " + str(round(ship.get_prop_mass(), 1))
render_AN(prop, (0.75,0,1), (4.45, -5.3), camera, font_size=0.1)
def drawInterface(camera, ship, autopilot, terrain, at_descent_rate, thrust_update_cmd, rot_damp):
drawNumbers(camera, ship, autopilot, terrain, at_descent_rate)
## # artificial horizon
## glPushMatrix()
## glTranslate(-camera.get_pos()[0],
## -camera.get_pos()[1],
## -camera.get_pos()[2])
##
## glColor(0.9, 0.9, 0.9)
##
## glBegin(GL_LINES)
##
## glVertex3f(5 * camera.get_orient()[0][0] + 0 * camera.get_orient()[1][0] + (-10) * camera.get_orient()[2][0],
## 0,
## 5 * camera.get_orient()[0][2] + 0 * camera.get_orient()[1][2] + (-10) * camera.get_orient()[2][2])
##
## glVertex3f(3 * camera.get_orient()[0][0] + 0 * camera.get_orient()[1][0] + (-10) * camera.get_orient()[2][0],
## 0,
## 3 * camera.get_orient()[0][2] + 0 * camera.get_orient()[1][2] + (-10) * camera.get_orient()[2][2])
## glEnd()
## glPopMatrix()
# thrust setting
percent_thrust = ship.get_percent_thrust()
thrust_line_y = percent_thrust/100 * 3 -5
if thrust_update_cmd:
thrust_ap_percent = max(min(ship.get_percent_thrust() + (thrust_update_cmd / ship.get_max_thrust()) * 100, 100), 0)
thrust_ap_line_y = thrust_ap_percent/100 * 3 -5
# left chevron (>)
drawLine2D(6.8, thrust_ap_line_y + 0.2, 7, thrust_ap_line_y, [0.85, 0, 0.85], camera)
drawLine2D(6.8, thrust_ap_line_y - 0.2, 7, thrust_ap_line_y, [0.85, 0, 0.85], camera)
# right chevron (<)
drawLine2D(8.2, thrust_ap_line_y + 0.2, 8, thrust_ap_line_y, [0.85, 0, 0.85], camera)
drawLine2D(8.2, thrust_ap_line_y - 0.2, 8, thrust_ap_line_y, [0.85, 0, 0.85], camera)
#drawLine2D(4.9, thrust_ap_line_y, 6.1, thrust_ap_line_y, [0.75, 0, 0.75], camera)
drawLine2D(7,thrust_line_y,8,thrust_line_y,[1,0,1], camera)
drawRectangle2D(7,-2,8,-5,[1,0,1], camera)
# angular velocity display
drawRectangle2D(8, 4, 10, 6, [1,0,0], camera)
if rot_damp:
drawLine2D(8, 4, 10, 6, [1,0,0], camera)
drawLine2D(8, 6, 10, 4, [1,0,0], camera)
# centerlines
drawLine2D(8, 5, 10, 5, [0.5,0,0], camera)
drawLine2D(9, 4, 9, 6, [0.5,0,0], camera)
drawLine2D(9, 6, 9, 6.5, [0.5,0,0], camera)
# horiz line (moves in y direction)
drawLine2D(8, max(min(ship.get_ang_vel()[0]/8 + 5, 5.9), 4.1),
10, max(min(ship.get_ang_vel()[0]/8 + 5, 5.9), 4.1),
[1,0,0], camera)
# vert line (moves in x direction)
drawLine2D(max(min(-ship.get_ang_vel()[1]/8 + 9, 9.9), 8.1), 4,
max(min(-ship.get_ang_vel()[1]/8 + 9, 9.9), 8.1), 6,
[1,0,0], camera)
# roll
drawRectangle2D(8, 6, 10, 6.5, [1,0,0], camera)
drawLine2D(max(min(-ship.get_ang_vel()[2]/8 + 9,9.9),8.1), 6,
max(min(-ship.get_ang_vel()[2]/8 + 9,9.9),8.1), 6.5, [1,0,0], camera)
# linear velocity display
drawRectangle2D(5, 4, 7, 6, [0,1,1], camera)
# centerlines
drawLine2D(5, 5, 7, 5, [0,0.5,0.5], camera)
drawLine2D(5, 5, 7, 5, [0,0.5,0.5], camera)
horizontal_vel_x = ship.get_vel()[0] * ship.get_orient()[0][0] + ship.get_vel()[2] * ship.get_orient()[0][2]
horizontal_vel_z = ship.get_vel()[2] * ship.get_orient()[2][2] + ship.get_vel()[0] * ship.get_orient()[2][0]
# local horizontal z
drawLine2D(5,max(min(-horizontal_vel_z/25 + 5, 5.9), 4.1),
7,max(min(-horizontal_vel_z/25 + 5, 5.9), 4.1),
[0,1,1], camera)
# local horizontal x
drawLine2D(max(min(horizontal_vel_x/25 + 6, 6.9), 5.1), 4,
max(min(horizontal_vel_x/25 + 6, 6.9), 5.1), 6,
[0,1,1], camera)
# descent speed
drawRectangle2D(4.5, 4, 5, 6, [0,1,1], camera)
# centerline
drawLine2D(4.5, 5.5, 5, 5.5, [0,0.5,0.5], camera)
# descent rate
drawLine2D(4.5, max(min(ship.get_vel()[1]/10 + 5.5, 5.9), 4.1),
5, max(min(ship.get_vel()[1]/10 + 5.5, 5.9), 4.1),
[0,1,1], camera)
# AP light
if autopilot:
drawRectangle2D(7, -1.9, 7.4, -1.5, [0,0.8,0], camera)
if ship.get_prop_mass() < 500:
drawRectangle2D(7.6, -1.9, 8, -1.5, [1,0,0], camera)