102 lines
2.3 KiB
Python
102 lines
2.3 KiB
Python
|
# 3D cube
|
||
|
# by mibi88
|
||
|
|
||
|
from PLANE.line import *
|
||
|
from micropython import *
|
||
|
import math, time
|
||
|
|
||
|
opt_level(3)
|
||
|
|
||
|
#width(3)
|
||
|
|
||
|
# unsed some code from https://www.mfitzp.com/creating-a-3d-rotating-cube-with-micropython-and-oled-display/
|
||
|
|
||
|
vertices = [
|
||
|
[1,0,1],
|
||
|
[2,0,-1],
|
||
|
[0,0,-1],
|
||
|
[1,1,-1],
|
||
|
]
|
||
|
|
||
|
edges = [
|
||
|
[0, 1, 2],
|
||
|
[0, 1, 3],
|
||
|
[1, 2, 3],
|
||
|
[0, 2, 3]
|
||
|
]
|
||
|
|
||
|
#vertices=memoryview(vertices)
|
||
|
#edges=memoryview(edges)
|
||
|
|
||
|
a=0
|
||
|
|
||
|
|
||
|
|
||
|
def rotate_x(sx, sy, sz, deg):
|
||
|
rad = deg * math.pi / 180
|
||
|
cosa = math.cos(rad)
|
||
|
sina = math.sin(rad)
|
||
|
y = sy * cosa - sz * sina
|
||
|
z = sy * sina + sz * cosa
|
||
|
return [sx, y, z]
|
||
|
|
||
|
def rotate_y(sx, sy, sz, angle):
|
||
|
rad = angle * math.pi / 180
|
||
|
cosa = math.cos(rad)
|
||
|
sina = math.sin(rad)
|
||
|
z = sz * cosa - sx * sina
|
||
|
x = sz * sina + sx * cosa
|
||
|
return [x, sy, z]
|
||
|
|
||
|
def rotate_z(sx, sy, sz, angle):
|
||
|
rad = angle * math.pi / 180
|
||
|
cosa = math.cos(rad)
|
||
|
sina = math.sin(rad)
|
||
|
x = sx * cosa - sy * sina
|
||
|
y = sx * sina + sy * cosa
|
||
|
return [x, y, sz]
|
||
|
|
||
|
def project(sx, sy, sz, px, py, pz, fov = 60, viewer_distance = 8, w=128, h=64):
|
||
|
factor = fov / (viewer_distance + (sz+pz))
|
||
|
x = (sx+px) * factor + (w/2)
|
||
|
y = -(sy+py) * factor + (h/2)
|
||
|
return [x, y]
|
||
|
|
||
|
def render(vertices, triangles, rx, ry, rz, px, py, pz, fov = 60, viewer_distance = 6, w=128, h=64):
|
||
|
rx = rx%360
|
||
|
ry = ry%360
|
||
|
rz = rz%360
|
||
|
points = []
|
||
|
for i in vertices:
|
||
|
i = rotate_x(i[0], i[1], i[2], rx)
|
||
|
i = rotate_y(i[0], i[1], i[2], ry)
|
||
|
i = rotate_z(i[0], i[1], i[2], rz)
|
||
|
pos = project(i[0], i[1], i[2], px, py, pz, fov, viewer_distance, w, h)
|
||
|
#print(pos)
|
||
|
points.append(pos)
|
||
|
#clear()
|
||
|
for i in triangles:
|
||
|
line(points[i[0]], points[i[1]])
|
||
|
line(points[i[1]], points[i[2]])
|
||
|
line(points[i[0]], points[i[2]])
|
||
|
|
||
|
def draw():
|
||
|
global a
|
||
|
global vertices, edges
|
||
|
#render(vertices, edges, 345, a, 0, 32, 16)
|
||
|
start=time.time()
|
||
|
clear_screen()
|
||
|
render(vertices, edges, a, a, 0, 0, 0, 0)
|
||
|
show_screen()
|
||
|
tps=(1/(time.time()-start)*4)
|
||
|
print(tps)
|
||
|
a+=1
|
||
|
|
||
|
while 1:
|
||
|
try:
|
||
|
draw()
|
||
|
except:
|
||
|
break
|
||
|
"""for i in range(1000):
|
||
|
pass"""
|
||
|
"""draw()"""
|