-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
421 lines (349 loc) · 12.7 KB
/
draw.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
from display import *
from matrix import *
from gmath import *
def draw_scanline(x0, z0, x1, z1, y, screen, zbuffer, color):
if x0 > x1:
tx = x0
tz = z0
x0 = x1
z0 = z1
x1 = tx
z1 = tz
x = x0
z = z0
delta_z = (z1 - z0) / (x1 - x0 + 1) if (x1 - x0 + 1) != 0 else 0
while x <= x1:
plot(screen, zbuffer, color, x, y, z)
x+= 1
z+= delta_z
def scanline_convert(polygons, i, screen, zbuffer, color):
flip = False
BOT = 0
TOP = 2
MID = 1
points = [ (polygons[i][0], polygons[i][1], polygons[i][2]),
(polygons[i+1][0], polygons[i+1][1], polygons[i+1][2]),
(polygons[i+2][0], polygons[i+2][1], polygons[i+2][2]) ]
# alas random color, we hardly knew ye
#color = [0,0,0]
#color[RED] = (23*(i/3)) %256
#color[GREEN] = (109*(i/3)) %256
#color[BLUE] = (227*(i/3)) %256
points.sort(key = lambda x: x[1])
x0 = points[BOT][0]
z0 = points[BOT][2]
x1 = points[BOT][0]
z1 = points[BOT][2]
y = int(points[BOT][1])
distance0 = int(points[TOP][1]) - y * 1.0 + 1
distance1 = int(points[MID][1]) - y * 1.0 + 1
distance2 = int(points[TOP][1]) - int(points[MID][1]) * 1.0 + 1
dx0 = (points[TOP][0] - points[BOT][0]) / distance0 if distance0 != 0 else 0
dz0 = (points[TOP][2] - points[BOT][2]) / distance0 if distance0 != 0 else 0
dx1 = (points[MID][0] - points[BOT][0]) / distance1 if distance1 != 0 else 0
dz1 = (points[MID][2] - points[BOT][2]) / distance1 if distance1 != 0 else 0
while y <= int(points[TOP][1]):
if ( not flip and y >= int(points[MID][1])):
flip = True
dx1 = (points[TOP][0] - points[MID][0]) / distance2 if distance2 != 0 else 0
dz1 = (points[TOP][2] - points[MID][2]) / distance2 if distance2 != 0 else 0
x1 = points[MID][0]
z1 = points[MID][2]
#draw_line(int(x0), y, z0, int(x1), y, z1, screen, zbuffer, color)
draw_scanline(int(x0), z0, int(x1), z1, y, screen, zbuffer, color)
x0+= dx0
z0+= dz0
x1+= dx1
z1+= dz1
y+= 1
def add_polygon( polygons, x0, y0, z0, x1, y1, z1, x2, y2, z2 ):
add_point(polygons, x0, y0, z0)
add_point(polygons, x1, y1, z1)
add_point(polygons, x2, y2, z2)
def add_mesh (polygons, parsed_file, group):
v = parsed_file['vertices']
for vertices in parsed_file['faces'][group]:
for i in range (len (vertices)):
if i == len (vertices) - 1:
p1 = vertices[i]
p2 = vertices[0]
p3 = vertices[1]
elif i == len (vertices) - 2:
p1 = vertices[i]
p2 = vertices[i + 1]
p3 = vertices[0]
else:
p1 = vertices[i]
p2 = vertices[i + 1]
p3 = vertices[i + 2]
add_polygon (polygons, v[p1][0], v[p1][1], v[p1][2],
v[p2][0], v[p2][1], v[p2][2],
v[p3][0], v[p3][1], v[p3][2])
def draw_polygons( polygons, screen, zbuffer, view, ambient, light, symbols, reflect):
if len(polygons) < 2:
print('Need at least 3 points to draw')
return
point = 0
while point < len(polygons) - 2:
normal = calculate_normal(polygons, point)[:]
#print normal
if normal[2] > 0:
color = get_lighting(normal, view, ambient, light, symbols, reflect )
scanline_convert(polygons, point, screen, zbuffer, color)
# draw_line( int(polygons[point][0]),
# int(polygons[point][1]),
# polygons[point][2],
# int(polygons[point+1][0]),
# int(polygons[point+1][1]),
# polygons[point+1][2],
# screen, zbuffer, color)
# draw_line( int(polygons[point+2][0]),
# int(polygons[point+2][1]),
# polygons[point+2][2],
# int(polygons[point+1][0]),
# int(polygons[point+1][1]),
# polygons[point+1][2],
# screen, zbuffer, color)
# draw_line( int(polygons[point][0]),
# int(polygons[point][1]),
# polygons[point][2],
# int(polygons[point+2][0]),
# int(polygons[point+2][1]),
# polygons[point+2][2],
# screen, zbuffer, color)
point+= 3
def add_box( polygons, x, y, z, width, height, depth ):
x1 = x + width
y1 = y - height
z1 = z - depth
#front
add_polygon(polygons, x, y, z, x1, y1, z, x1, y, z)
add_polygon(polygons, x, y, z, x, y1, z, x1, y1, z)
#back
add_polygon(polygons, x1, y, z1, x, y1, z1, x, y, z1)
add_polygon(polygons, x1, y, z1, x1, y1, z1, x, y1, z1)
#right side
add_polygon(polygons, x1, y, z, x1, y1, z1, x1, y, z1)
add_polygon(polygons, x1, y, z, x1, y1, z, x1, y1, z1)
#left side
add_polygon(polygons, x, y, z1, x, y1, z, x, y, z)
add_polygon(polygons, x, y, z1, x, y1, z1, x, y1, z)
#top
add_polygon(polygons, x, y, z1, x1, y, z, x1, y, z1)
add_polygon(polygons, x, y, z1, x, y, z, x1, y, z)
#bottom
add_polygon(polygons, x, y1, z, x1, y1, z1, x1, y1, z)
add_polygon(polygons, x, y1, z, x, y1, z1, x1, y1, z1)
def add_sphere(polygons, cx, cy, cz, r, step ):
points = generate_sphere(cx, cy, cz, r, step)
lat_start = 0
lat_stop = step
longt_start = 0
longt_stop = step
step+= 1
for lat in range(lat_start, lat_stop):
for longt in range(longt_start, longt_stop):
p0 = lat * step + longt
p1 = p0+1
p2 = (p1+step) % (step * (step-1))
p3 = (p0+step) % (step * (step-1))
if longt != step - 2:
add_polygon( polygons, points[p0][0],
points[p0][1],
points[p0][2],
points[p1][0],
points[p1][1],
points[p1][2],
points[p2][0],
points[p2][1],
points[p2][2])
if longt != 0:
add_polygon( polygons, points[p0][0],
points[p0][1],
points[p0][2],
points[p2][0],
points[p2][1],
points[p2][2],
points[p3][0],
points[p3][1],
points[p3][2])
def generate_sphere( cx, cy, cz, r, step ):
points = []
rot_start = 0
rot_stop = step
circ_start = 0
circ_stop = step
for rotation in range(rot_start, rot_stop):
rot = rotation/float(step)
for circle in range(circ_start, circ_stop+1):
circ = circle/float(step)
x = r * math.cos(math.pi * circ) + cx
y = r * math.sin(math.pi * circ) * math.cos(2*math.pi * rot) + cy
z = r * math.sin(math.pi * circ) * math.sin(2*math.pi * rot) + cz
points.append([x, y, z])
#print 'rotation: %d\tcircle%d'%(rotation, circle)
return points
def add_torus(polygons, cx, cy, cz, r0, r1, step ):
points = generate_torus(cx, cy, cz, r0, r1, step)
lat_start = 0
lat_stop = step
longt_start = 0
longt_stop = step
for lat in range(lat_start, lat_stop):
for longt in range(longt_start, longt_stop):
p0 = lat * step + longt;
if (longt == (step - 1)):
p1 = p0 - longt;
else:
p1 = p0 + 1;
p2 = (p1 + step) % (step * step);
p3 = (p0 + step) % (step * step);
add_polygon(polygons,
points[p0][0],
points[p0][1],
points[p0][2],
points[p3][0],
points[p3][1],
points[p3][2],
points[p2][0],
points[p2][1],
points[p2][2] )
add_polygon(polygons,
points[p0][0],
points[p0][1],
points[p0][2],
points[p2][0],
points[p2][1],
points[p2][2],
points[p1][0],
points[p1][1],
points[p1][2] )
def generate_torus( cx, cy, cz, r0, r1, step ):
points = []
rot_start = 0
rot_stop = step
circ_start = 0
circ_stop = step
for rotation in range(rot_start, rot_stop):
rot = rotation/float(step)
for circle in range(circ_start, circ_stop):
circ = circle/float(step)
x = math.cos(2*math.pi * rot) * (r0 * math.cos(2*math.pi * circ) + r1) + cx;
y = r0 * math.sin(2*math.pi * circ) + cy;
z = -1*math.sin(2*math.pi * rot) * (r0 * math.cos(2*math.pi * circ) + r1) + cz;
points.append([x, y, z])
return points
def add_circle( points, cx, cy, cz, r, step ):
x0 = r + cx
y0 = cy
i = 1
while i <= step:
t = float(i)/step
x1 = r * math.cos(2*math.pi * t) + cx;
y1 = r * math.sin(2*math.pi * t) + cy;
add_edge(points, x0, y0, cz, x1, y1, cz)
x0 = x1
y0 = y1
i+= 1
def add_curve( points, x0, y0, x1, y1, x2, y2, x3, y3, step, curve_type ):
xcoefs = generate_curve_coefs(x0, x1, x2, x3, curve_type)[0]
ycoefs = generate_curve_coefs(y0, y1, y2, y3, curve_type)[0]
i = 1
while i <= step:
t = float(i)/step
x = t * (t * (xcoefs[0] * t + xcoefs[1]) + xcoefs[2]) + xcoefs[3]
y = t * (t * (ycoefs[0] * t + ycoefs[1]) + ycoefs[2]) + ycoefs[3]
#x = xcoefs[0] * t*t*t + xcoefs[1] * t*t + xcoefs[2] * t + xcoefs[3]
#y = ycoefs[0] * t*t*t + ycoefs[1] * t*t + ycoefs[2] * t + ycoefs[3]
add_edge(points, x0, y0, 0, x, y, 0)
x0 = x
y0 = y
i+= 1
def draw_lines( matrix, screen, zbuffer, color ):
if len(matrix) < 2:
print('Need at least 2 points to draw')
return
point = 0
while point < len(matrix) - 1:
draw_line( int(matrix[point][0]),
int(matrix[point][1]),
matrix[point][2],
int(matrix[point+1][0]),
int(matrix[point+1][1]),
matrix[point+1][2],
screen, zbuffer, color)
point+= 2
def add_edge( matrix, x0, y0, z0, x1, y1, z1 ):
add_point(matrix, x0, y0, z0)
add_point(matrix, x1, y1, z1)
def add_point( matrix, x, y, z=0 ):
matrix.append( [x, y, z, 1] )
def draw_line( x0, y0, z0, x1, y1, z1, screen, zbuffer, color ):
#swap points if going right -> left
if x0 > x1:
xt = x0
yt = y0
zt = z0
x0 = x1
y0 = y1
z0 = z1
x1 = xt
y1 = yt
z1 = zt
x = x0
y = y0
z = z0
A = 2 * (y1 - y0)
B = -2 * (x1 - x0)
wide = False
tall = False
if ( abs(x1-x0) >= abs(y1 - y0) ): #octants 1/8
wide = True
loop_start = x
loop_end = x1
dx_east = dx_northeast = 1
dy_east = 0
d_east = A
distance = x1 - x + 1
if ( A > 0 ): #octant 1
d = A + B/2
dy_northeast = 1
d_northeast = A + B
else: #octant 8
d = A - B/2
dy_northeast = -1
d_northeast = A - B
else: #octants 2/7
tall = True
dx_east = 0
dx_northeast = 1
distance = abs(y1 - y) + 1
if ( A > 0 ): #octant 2
d = A/2 + B
dy_east = dy_northeast = 1
d_northeast = A + B
d_east = B
loop_start = y
loop_end = y1
else: #octant 7
d = A/2 - B
dy_east = dy_northeast = -1
d_northeast = A - B
d_east = -1 * B
loop_start = y1
loop_end = y
dz = (z1 - z0) / distance if distance != 0 else 0
while ( loop_start < loop_end ):
plot( screen, zbuffer, color, x, y, z )
if ( (wide and ((A > 0 and d > 0) or (A < 0 and d < 0))) or
(tall and ((A > 0 and d < 0) or (A < 0 and d > 0 )))):
x+= dx_northeast
y+= dy_northeast
d+= d_northeast
else:
x+= dx_east
y+= dy_east
d+= d_east
z+= dz
loop_start+= 1
plot( screen, zbuffer, color, x, y, z )