-
Notifications
You must be signed in to change notification settings - Fork 0
/
echoView_v2.py
342 lines (291 loc) · 6.06 KB
/
echoView_v2.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
from subprocess import call
import math
import time
width = 143 * 2
height = 38 * 2
global frame
frame = [0]*(width*height)
global depth
depth = [1000]*(width*height)
global xDir
xDir = 1
global yDir
yDir = 1
global zDir
zDir = 1
global xP
xP = 0
global yP
yP = 0
global zP
zP = 0
vertex = [
[-1,-1,-1],
[-1,-1,1],
[1,-1,1],
[1,-1,-1],
[-1,1,-1],
[-1,1,1],
[1,1,1],
[1,1,-1]
# [-1,-1,-1],
# [1,0.5,-1],
# [-1,1,-1],
# [0.5,0.5,1]
]
def normalize(vec):
mag = math.sqrt(vec[0]**2 + vec[1]**2 + vec[2]**2)
return [vec[0]/mag,vec[1]/mag,vec[2]/mag]
normals = []
for i in vertex:
normals.append(normalize(i))
faces = [
[0,1,2, 0.55],
[2,3,0, 0.55],
[4,5,6, 0.55],
[6,7,4, 0.55],
[0,1,4, 0.55],
[4,5,1, 0.55],
[2,3,6, 0.55],
[6,7,3, 0.55],
[1,2,5, 0.55],
[5,6,2, 0.55],
[3,0,7, 0.55],
[7,4,0, 0.55]
# [0,1,2, 0.35],
# [0,2,3, 0.55],
# [0,1,3, 0.75],
# [1,2,3, 0.95]
]
def setColor(i):
val = int(math.ceil(232 + i * (255-232)))
return "\\e[48;5;"+str(val)+"m"
def lerp(p,A,B):
r=[A[0]+p*(B[0]-A[0]),
A[1]+p*(B[1]-A[1]),
A[2]+p*(B[2]-A[2])]
return normalize(r)
def dot(a,b):
return a[0]*b[0]+a[1]*b[1]+a[2]*b[2]
def setPixel(x,y,z,n,color):
global frame
global depth
if (y<0 or x<0 or x>width-1 or y>height-1):
return
if ( -z > depth[width*y+x] ):
return
dL = dot(n,[1,1,1])
if dL<0:
dL =0
light = color*(dL+0.5)
if light>1:
light =1
frame[width*y+x] = light
depth[width*y+x] = -z
def drawLine(x1,y1,z1,x2,y2,z2,color):
dx = x2-x1
dy = y2-y1
mag = math.sqrt(dx*dx+dy*dy)
setPixel(int(x1),int(y1),z1,color)
if (mag==0):
return
dx /= mag
dy /= mag
for i in range(int(mag)):
z = float(i+0.5)/mag * (z2-z1) + z1
setPixel(int(x1),int(y1),z,color)
x1 += dx
y1 += dy
return
def fillTriangle(pt1,pt2,pt3,color):
A = pt1
B = pt2
C = pt3
if ( pt2[0] > pt3[0] ):
B = pt3
C = pt2
if ( pt2[1]< pt1[1] and pt2[1] < pt3[1] ):
A = pt2
if ( pt1[0] > pt3[0] ):
B = pt3
C = pt1
else:
B = pt1
C = pt3
elif ( pt3[1] < pt1[1] and pt3[1] < pt2[1]):
A = pt3
if ( pt1[0] > pt2[0] ):
B = pt2
C = pt1
else:
B = pt1
C = pt2
ACx = C[0]-A[0]
ACy = C[1]-A[1]
ABx = B[0]-A[0]
ABy = B[1]-A[1]
if ( ABx*ACy - ABy*ACx > 0 ):
T = B
B = C
C = T
xL = A[0]
xR = A[0]
y = A[1]
yMax = B[1]
if ( C[1] < yMax ):
yMax = C[1]
while y <= yMax:
pAB = float(y-A[1])/(B[1]-A[1]+0.001)
pAC = float(y-A[1])/(C[1]-A[1]+0.001)
nAB = lerp(pAB,A[3],B[3])
nAC = lerp(pAC,A[3],C[3])
xL = A[0] + pAB*(B[0]-A[0])
xR = A[0] + pAC*(C[0]-A[0])
zB = A[2] + pAB*(B[2]-A[2])
zC = A[2] + pAC*(C[2]-A[2])
x = xL
while x <= xR:
pBC = float(x-xL)/(xR-xL+0.001)
nBC = lerp(pBC,nAB,nAC)
z = zB + pBC*(zC-zB)
setPixel(int(x),int(y),z,nBC,color)
x += 1
y+=1
#other half
if ( C[1] < B[1] ):
y = C[1]
while y <= B[1]:
pAB = float(y-A[1])/(B[1]-A[1]+0.001)
pCB = float(y-C[1])/(B[1]-C[1]+0.001)
nAB = lerp(pAB,A[3],B[3])
nCB = lerp(pCB,C[3],B[3])
xL = A[0] + pAB*(B[0]-A[0])
xR = C[0] + pCB*(B[0]-C[0])
zA = A[2] + pAB*(B[2]-A[2])
zC = C[2] + pCB*(B[2]-C[2])
x = xL
while x <= xR:
pAC = float(x-xL)/(xR-xL+0.001)
nAC = lerp(pAC,nAB,nCB)
z = zA + pAC*(zC-zA)
setPixel(int(x),int(y),z,nAC,color)
x += 1
y += 1
else:
y = B[1]
while y <= C[1]:
pBC = float(y-B[1])/(C[1]-B[1]+0.001)
pAC = float(y-A[1])/(C[1]-A[1]+0.001)
nBC = lerp(pBC,B[3],C[3])
nAC = lerp(pAC,A[3],C[3])
xL = B[0] + pBC*(C[0]-B[0])
xR = A[0] + pAC*(C[0]-A[0])
zA = A[2] + pAC*(C[2]-A[2])
zB = B[2] + pBC*(C[2]-B[2])
x = xL
while x <= xR:
pBA = float(x-xL)/(xR-xL+0.001)
nBA = lerp(pBA,nBC,nAC)
z = zB + pBA*(zA - zB)
setPixel(int(x),int(y),z,nBA,color)
x += 1
y += 1
return
def modelview(vec3,view):
CENTER = view['center']
ROT = view['rotation']
SCALE = view['scale']
angle = ROT[0]
angle2 = ROT[1]
pos = [math.cos(angle)*vec3[0] - math.sin(angle)*vec3[2],
vec3[1],
math.sin(angle)*vec3[0] + math.cos(angle)*vec3[2]]
pos = [math.cos(angle2)*pos[0] - math.sin(angle2)*pos[1],
math.sin(angle2)*pos[0] + math.cos(angle2)*pos[1],
pos[2]]
pos[0] *= SCALE[0]
pos[1] *= SCALE[1]
pos[2] *= SCALE[2]
pos[0] += CENTER[0]
pos[1] += CENTER[1]
pos[2] += CENTER[2]
return pos
def project(vec3,TIME):
angle = TIME
FoV = 18
alpha = 0.5
camZ = -4
camX = 0
camY = 0
x = (vec3[0]+camX)/(vec3[2]*alpha+camZ) * FoV * 3.5 + width/2
y = (vec3[1]+camY)/(vec3[2]*alpha+camZ) * FoV * 1.5 + height/2
z = vec3[2]
return [x,y,z]
def update():
global frame
global vertex
global faces
global depth
global xP
global yP
global zP
global xDir
global yDir
global zDir
frame = [0]*(width*height)
depth = [1000]*(width*height)
TIME = time.time()
xP += xDir *.05
yP += yDir *.05
zP += zDir *.05
if xP>7 or xP<-7:
xDir *=-1
if yP>3 or yP<-3:
yDir *= -1
if zP>4 or zP<-8:
zDir *= -1
Views = [
{ 'center': [xP,yP,zP-5],
'rotation': [TIME,TIME*0.5],
'scale': [2,2,2] },
# { 'center': [-xP,-yP,-zP-5],
# 'rotation': [-TIME,-TIME/3],
# 'scale': [1.6,1,1.6] }
]
color = 1
for View in Views:
for face in faces:
mv1 = modelview(vertex[face[0]],View)
mv2 = modelview(vertex[face[1]],View)
mv3 = modelview(vertex[face[2]],View)
Norm = {
'center': [0,0,0],
'rotation': View['rotation'],
'scale': [1,1,1]
}
n1 = modelview(normals[face[0]],Norm)
n2 = modelview(normals[face[1]],Norm)
n3 = modelview(normals[face[2]],Norm)
pt1 = project(mv1,TIME)
pt2 = project(mv2,TIME)
pt3 = project(mv3,TIME)
color = face[3]
fillTriangle(
[pt1[0],pt1[1],pt1[2],n1],
[pt2[0],pt2[1],pt2[2],n2],
[pt3[0],pt3[1],pt3[2],n3],color)
# drawLine(pt1[0],pt1[1],pt1[2],pt2[0],pt2[1],pt2[2],color)
# drawLine(pt2[0],pt2[1],pt2[2],pt3[0],pt3[1],pt3[2],color)
# drawLine(pt3[0],pt3[1],pt3[2],pt1[0],pt1[1],pt1[2],color)
return
while True:
update()
acc = "\033[0;0f"
for i in range(width*height):
if (i)%width == 0:
acc += '\n'
if (frame[i]==0):
acc += " "
else:
acc += setColor(frame[i])+ " \\e[0m"
call(["echo","-e",acc])