-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendering.py
214 lines (191 loc) · 5.81 KB
/
rendering.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
import numpy as np
from structures import *
from vtk import *
def normalise(vector):
return vector / np.linalg.norm(vector)
def polygon_order(points, center, normal):
if len(points) == 0:
return np.array([])
x = normalise(points[0] - center)
z = normalise(normal)
y = np.cross(x,z)
px = np.dot(points, x)
py = np.dot(points, y)
return ConvexHull(np.array([px, py]).T).vertices
def make_lines(points, lines):
vpoints = vtkPoints()
vpoints.SetNumberOfPoints(len(points))
for i, point in enumerate(points):
vpoints.SetPoint(i, point)
vlines = vtkCellArray()
for start, end in lines:
vline = vtkLine()
vline.GetPointIds().SetId(0, start)
vline.GetPointIds().SetId(1, end)
vlines.InsertNextCell(vline)
poly = vtkPolyData()
poly.SetPoints(vpoints)
poly.SetLines(vlines)
return poly
def ship_model(ship):
model = make_lines(ship.vertices, ship.edges)
polygons = vtkCellArray()
for vertices, center, normal in zip(
ship.face_vertices,
ship.face_centers,
ship.face_normals):
order = polygon_order(ship.vertices[vertices], center, normal)
if len(order) == 0:
continue
polygon = vtkPolygon()
ids = polygon.GetPointIds()
ids.SetNumberOfIds(len(order))
for i, point in enumerate(vertices[order]):
ids.SetId(i, point)
polygons.InsertNextCell(polygon)
model.SetPolys(polygons)
return model
def ship_normals(ship):
points = vtkPoints()
points.SetNumberOfPoints(ship.num_faces * 2)
lines = vtkCellArray()
for vertices, center, normal in ship_faces(ship):
points.SetPoint(2*i, center)
points.SetPoint(2*i + 1, center + normal)
line = vtkLine()
line.GetPointIds().SetId(0, 2*i)
line.GetPointIds().SetId(1, 2*i + 1)
lines.InsertNextCell(line)
model = vtkPolyData()
model.SetLines(lines)
return model
class ShipInstance(object):
dummy = vtkPolyData()
dummy.SetPoints(vtkPoints())
sphere = vtkSphereSource()
sphere.SetRadius(20000)
sphere.SetPhiResolution(18)
sphere.SetThetaResolution(36)
def __init__(self, slot):
self.slot = slot
self.transform = vtkTransform()
self.transform.PostMultiply()
self.filter = vtkTransformPolyDataFilter()
self.filter.SetTransform(self.transform)
self.filter.SetInputData(self.dummy)
self.mapper = vtkPolyDataMapper()
self.mapper.SetInputConnection(self.filter.GetOutputPort())
self.actor = vtkActor()
self.prop = self.actor.GetProperty()
self.prop.SetColor(0.4,0.4,0.4)
self.prop.EdgeVisibilityOn()
self.prop.SetEdgeColor(1,1,1)
self.prop.SetLineWidth(3)
self.actor.SetMapper(self.mapper)
self.actor.VisibilityOff()
self.ship_type = 0
self.laser_poly = make_lines([[0, 0, 0], [50000, 0, 0]], [[0, 1]])
self.laser_filter = vtkTransformPolyDataFilter()
self.laser_filter.SetTransform(self.transform)
self.laser_filter.SetInputData(self.laser_poly)
self.laser_mapper = vtkPolyDataMapper()
self.laser_mapper.SetInputConnection(self.laser_filter.GetOutputPort())
self.laser_actor = vtkActor()
self.laser_actor.SetMapper(self.laser_mapper)
self.laser_state = False
def update(self, game):
# Get ship type and state for this slot from game state.
ship_type = game.ship_types[self.slot]
state = game.ship_states[self.slot]
if ship_type != self.ship_type:
# Ship type has changed.
if ship_type == 0:
# Slot not in use.
self.filter.SetInputData(self.dummy)
self.actor.VisibilityOff()
elif ship_type & 0x80:
# Planet, render as a sphere.
self.filter.SetInputConnection(self.sphere.GetOutputPort())
self.prop.EdgeVisibilityOff()
self.actor.VisibilityOn()
else:
# Set correct model and make visible.
self.ship = game.ship_data[ship_type - 1]
self.filter.SetInputData(ship_model(self.ship))
self.prop.EdgeVisibilityOn()
self.ship_type = ship_type
# Set transform from ship state.
matrix = np.empty((4,4))
matrix[0:3,0:3] = state.rot
matrix[0:3,3] = state.pos
matrix[3,0:3] = 0
matrix[3,3] = 1
self.transform.SetMatrix(matrix.reshape(16) / 50.0)
self.filter.Update()
self.laser_filter.Update()
# Set visibility according to whether ship is alive.
self.actor.SetVisibility(state.is_alive())
# Set laser visibility according to whether ship is firing.
# Make lasers flicker by alternating frames.
self.laser_actor.SetVisibility(state.is_firing() and self.laser_state)
self.laser_state = not self.laser_state
def lines_2d(points, lines):
points_3d = np.empty((len(points), 3))
points_3d[:,0:2] = points
points_3d[:,2] = 0
poly = make_lines(points_3d, lines)
coord = vtkCoordinate()
coord.SetCoordinateSystemToNormalizedViewport()
mapper = vtkPolyDataMapper2D()
mapper.SetTransformCoordinate(coord)
mapper.SetInputData(poly)
actor = vtkActor2D()
actor.SetMapper(mapper)
return actor
def lines_3d(points_3d, lines):
poly = make_lines(points_3d, lines)
mapper = vtkPolyDataMapper()
mapper.SetInputData(poly)
actor = vtkActor()
actor.SetMapper(mapper)
return actor
def make_dust(game):
points = vtkPoints()
points.SetNumberOfPoints(game.num_dust * 2)
lines = vtkCellArray()
positions = (game.dust_positions) / 50.0
speed = [0, 0, -game.speed / 200.0]
for i, speck in enumerate(positions):
points.SetPoint(2*i, speck)
points.SetPoint(2*i+1, speck - speed)
line = vtkLine()
line.GetPointIds().SetId(0, 2*i)
line.GetPointIds().SetId(1, 2*i+1)
lines.InsertNextCell(line)
poly = vtkPolyData()
poly.SetPoints(points)
poly.SetLines(lines)
return poly
crosshair_points = np.array([
[0.5, 0.55], [0.5, 0.6],
[0.5, 0.45], [0.5, 0.4],
[0.55, 0.5], [0.6, 0.5],
[0.45, 0.5], [0.4, 0.5]])
crosshair_lines = np.array([
[0, 1], [2, 3],
[4, 5], [6, 7]])
border_points = np.array([
[0.025, 0.025], [0.025, 0.975],
[0.975, 0.975], [0.975, 0.025]])
border_lines = np.array([
[0, 1], [1, 2],
[2, 3], [3, 0]])
laser_points = np.array([
[0.15, 0.00], [0.20, 0.00],
[0.80, 0.00], [0.85, 0.00],
[0.50, 0.50]
])
laser_lines = np.array([
[0, 4], [1, 4],
[2, 4], [3, 4]
])