-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.py
242 lines (185 loc) · 7.67 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
import OpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
import math
import numpy as np
from rigidbody import *
from ui import *
class Color:
def __init__(self, r, g, b):
self.r = r
self.g = g
self.b = b
def drawOrigin():
glBegin(GL_LINES)
glColor(1,0,0)
glVertex3f(0,0,0)
glVertex3f(100,0,0)
glColor(0,1,0)
glVertex3f(0,0,0)
glVertex3f(0,100,0)
glColor(0,0,1)
glVertex3f(0,0,0)
glVertex3f(0,0,100)
glEnd()
def drawControls(cam, ctrl_state, first_person_ui):
glPointSize(3)
# aileron, elevator
drawRectangle2D(7, -3, 10, -6, Color(1, 0, 1), cam, first_person_ui)
drawPoint2D(8.5 - ctrl_state[0], -4.5 - ctrl_state[1], Color(1, 0, 1), cam, first_person_ui)
# rudder
drawRectangle2D(7, -2, 10, -3, Color(1, 0, 1), cam, first_person_ui)
drawPoint2D(8.5 + ctrl_state[2], -2.5, Color(1, 0, 1), cam, first_person_ui)
glPointSize(1)
def drawPoint(p, color):
glColor(color.r, color.g, color.b)
glPushMatrix()
glTranslatef(p.pos[0], p.pos[1], p.pos[2])
glBegin(GL_POINTS)
glVertex3f(0, 0, 0)
glEnd()
glPopMatrix()
def drawFlatland(cam, flatland, size=2000, divisions=50):
spacing = 10**(int(math.log(abs(cam.pos[1] - flatland.height) + 2, 10)) + 1)
# size = abs(cam.pos.y) * 10
# subdivisions
scene_spacing = spacing / 10
N = divisions * 10
corner_x = (-cam.pos[0] - N * 0.5 * scene_spacing) + cam.pos[0] % (scene_spacing)
corner_z = (-cam.pos[2] - N * 0.5 * scene_spacing) + cam.pos[2] % (scene_spacing)
glColor(flatland.color.r/2, flatland.color.g/2, flatland.color.b/2)
glBegin(GL_LINES)
for i in range(N + 1):
cx = corner_x + i * scene_spacing
z0 = corner_z
z1 = corner_z + N * scene_spacing
glVertex3f(cx, flatland.height, z0)
glVertex3f(cx, flatland.height, z1)
for i in range(N + 1):
x0 = corner_x
x1 = corner_x + N * scene_spacing
cz = corner_z + i * scene_spacing
glVertex3f(x0, flatland.height, cz)
glVertex3f(x1, flatland.height, cz)
glEnd()
# superdivisions
scene_spacing = spacing
N = divisions
corner_x = (-cam.pos[0] - N * 0.5 * scene_spacing) + cam.pos[0] % (scene_spacing)
corner_z = (-cam.pos[2] - N * 0.5 * scene_spacing) + cam.pos[2] % (scene_spacing)
glColor(flatland.color.r, flatland.color.g, flatland.color.b)
glBegin(GL_LINES)
for i in range(N + 1):
cx = corner_x + i * scene_spacing
z0 = corner_z
z1 = corner_z + N * scene_spacing
glVertex3f(cx, flatland.height, z0)
glVertex3f(cx, flatland.height, z1)
for i in range(N + 1):
x0 = corner_x
x1 = corner_x + N * scene_spacing
cz = corner_z + i * scene_spacing
glVertex3f(x0, flatland.height, cz)
glVertex3f(x1, flatland.height, cz)
glEnd()
def drawForces(forces):
for f in forces:
glPushMatrix()
scaler = 0.2
start_position = f.point.pos
end_position = f.point.pos + f.force
f_vector = f.force * scaler
f_dir = f_vector.normalized()
arrowhead_start = f.force * scaler * 0.8
if not f_dir.cross(vec3(1,0,0)) == vec3(0,0,0):
arrowhead_vector1 = f_dir.cross(vec3(1,0,0))
else:
arrowhead_vector1 = f_dir.cross(vec3(0,1,0))
arrowhead_vector2 = arrowhead_vector1.cross(f_dir)
arrowhead_vector1 = arrowhead_vector1 * f.force.mag() * scaler * 0.1
arrowhead_vector2 = arrowhead_vector2 * f.force.mag() * scaler * 0.1
arrowhead_pt1 = arrowhead_start + arrowhead_vector1
arrowhead_pt2 = arrowhead_start - arrowhead_vector1
arrowhead_pt3 = arrowhead_start + arrowhead_vector2
arrowhead_pt4 = arrowhead_start - arrowhead_vector2
glTranslate(start_position.x, start_position.y, start_position.z)
glColor(1,0,1)
glBegin(GL_LINES)
glVertex3f(0,0,0)
glVertex3f(f_vector.x, f_vector.y, f_vector.z)
glVertex3f(arrowhead_pt1.x, arrowhead_pt1.y, arrowhead_pt1.z)
glVertex3f(arrowhead_pt3.x, arrowhead_pt3.y, arrowhead_pt3.z)
glVertex3f(arrowhead_pt2.x, arrowhead_pt2.y, arrowhead_pt2.z)
glVertex3f(arrowhead_pt4.x, arrowhead_pt4.y, arrowhead_pt4.z)
glVertex3f(arrowhead_pt2.x, arrowhead_pt2.y, arrowhead_pt2.z)
glVertex3f(arrowhead_pt3.x, arrowhead_pt3.y, arrowhead_pt3.z)
glVertex3f(arrowhead_pt1.x, arrowhead_pt1.y, arrowhead_pt1.z)
glVertex3f(arrowhead_pt4.x, arrowhead_pt4.y, arrowhead_pt4.z)
glVertex3f(arrowhead_pt1.x, arrowhead_pt1.y, arrowhead_pt1.z)
glVertex3f(f_vector.x, f_vector.y, f_vector.z)
glVertex3f(arrowhead_pt2.x, arrowhead_pt2.y, arrowhead_pt2.z)
glVertex3f(f_vector.x, f_vector.y, f_vector.z)
glVertex3f(arrowhead_pt3.x, arrowhead_pt3.y, arrowhead_pt3.z)
glVertex3f(f_vector.x, f_vector.y, f_vector.z)
glVertex3f(arrowhead_pt4.x, arrowhead_pt4.y, arrowhead_pt4.z)
glVertex3f(f_vector.x, f_vector.y, f_vector.z)
glEnd()
glPopMatrix()
def drawModel(model, pos, orient, scale, color=Color(1, 1, 1)):
glPushMatrix()
glTranslatef(pos[0], pos[1], pos[2])
glColor(color.r, color.g, color.b)
glBegin(GL_LINES)
for lines in model.lines:
v1 = model.vertices[lines[0]]
v2 = model.vertices[lines[1]]
v1_rot = np.array([[v1[0] * orient[0][0] + v1[1] * orient[1][0] + v1[2] * orient[2][0]],
[v1[0] * orient[0][1] + v1[1] * orient[1][1] + v1[2] * orient[2][1]],
[v1[0] * orient[0][2] + v1[1] * orient[1][2] + v1[2] * orient[2][2]]])
v2_rot = np.array([[v2[0] * orient[0][0] + v2[1] * orient[1][0] + v2[2] * orient[2][0]],
[v2[0] * orient[0][1] + v2[1] * orient[1][1] + v2[2] * orient[2][1]],
[v2[0] * orient[0][2] + v2[1] * orient[1][2] + v2[2] * orient[2][2]]])
glVertex3f(v1_rot[0], v1_rot[1], v1_rot[2])
glVertex3f(v2_rot[0], v2_rot[1], v2_rot[2])
glEnd()
for faces in model.faces:
glBegin(GL_POLYGON)
vnum = len(faces)
for i in range(vnum):
v = model.vertices[faces[i]]
v_rot = np.array([[v[0] * orient[0][0] + v[1] * orient[1][0] + v[2] * orient[2][0]],
[v[0] * orient[0][1] + v[1] * orient[1][1] + v[2] * orient[2][1]],
[v[0] * orient[0][2] + v[1] * orient[1][2] + v[2] * orient[2][2]]])
glVertex3f(v_rot[0], v_rot[1], v_rot[2])
glEnd()
glPopMatrix()
def drawMissileTrails(m):
if len(m.trail) < 2:
return
glPushMatrix()
glColor(1, 1, 1)
glBegin(GL_LINES)
for p in m.trail:
glVertex3f(p[0], p[1], p[2])
glEnd()
glPopMatrix()
def drawScene(cam, flatland, bodies, cities, scenery_objects, ctrl_state, first_person_ui=False):
drawFlatland(cam, flatland)
for c in cities:
city_dist = np.linalg.norm(c.pos - cam.pos)
if city_dist < c.size * 1e4:
for b in c.buildings:
drawModel(b.model, b.pos, np.eye(3), 1, b.color)
elif city_dist < c.size * 1e6:
for b in c.buildings:
drawPoint(b, b.color)
for o in scenery_objects:
drawModel(o.model, o.pos, np.eye(3), 1, o.color)
for b in bodies:
if np.linalg.norm(b.pos - cam.pos) < 1e5:
drawModel(b.model, b.pos, b.orient, 1, Color(1, 0, 0))
else:
drawPoint(o, Color(1, 0, 0))
if isinstance(b, Rocket):
drawMissileTrails(b)
drawControls(cam, ctrl_state, first_person_ui)