-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (75 loc) · 2.48 KB
/
Copy pathmain.py
File metadata and controls
101 lines (75 loc) · 2.48 KB
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
import pygame
import numpy as np
def init():
pygame.init()
tela = pygame.display.set_mode((900, 500), pygame.RESIZABLE)
pygame.display.set_caption("Cubo 3D Painter Algorithm")
clock = pygame.time.Clock()
return tela, clock
def multiplica_m(a, b):
return np.dot(a, b)
def rotacao_x(a):
#print(np.array([[1,0,0], [0, np.cos(a), -np.sin(a)], [0, np.sin(a), np.cos(a)]]))
return np.array([[1,0,0], [0, np.cos(a), -np.sin(a)], [0, np.sin(a), np.cos(a)]])
def rotacao_y(a):
return np.array([[np.cos(a), 0, np.sin(a)], [0, 1, 0], [-np.sin(a), 0, np.cos(a)]])
def rotacao_z(a):
return np.array([[np.cos(a), -np.sin(a), 0], [np.sin(a), np.cos(a), 0], [0, 0, 1]])
def render(tela, pontos_2d, faces_ordenadas):
tela.fill((20, 20, 30))
for _,face,cor in faces_ordenadas:
pygame.draw.polygon(tela, cor, [pontos_2d[i] for i in face])
pygame.display.update()
def main():
tela, clock = init()
pontos_cubo = np.array([[-1,-1,1], [1,-1,1], [1,1,1], [-1,1,1], [-1,-1,-1], [1,-1,-1], [1,1,-1], [-1,1,-1]])
faces = [([0,1,2,3], (200,50,50)), ([4,5,6,7], (50,50,200)), ([0,4,5,1], (50,200,50)), ([3,2,6,7], (200,200,50)), ([1,5,6,2], (50,200,200)), ([0,4,7,3], (200,50,200))]
escala = 200
ang_x = ang_y = ang_z = 0
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
#elif event.type == pygame.KEYDOWN:
# if event.key == pygame.K_UP:
# ang_x += 0.01
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
ang_x += 0.01
if keys[pygame.K_DOWN]:
ang_x -= 0.01
if keys[pygame.K_LEFT]:
ang_y += 0.01
if keys[pygame.K_RIGHT]:
ang_y -= 0.01
if keys[pygame.K_q]:
ang_z += 0.01
if keys[pygame.K_e]:
ang_z -= 0.01
#ang_x += 0.01
#ang_y += 0.013
#ang_z += 0.008
rx = rotacao_x(ang_x)
ry = rotacao_y(ang_y)
rz = rotacao_z(ang_z)
pontos_rot = []
for p in pontos_cubo:
pr = multiplica_m(rx, p)
pr = multiplica_m(ry, pr)
pr = multiplica_m(rz, pr)
pontos_rot.append(pr)
pontos_rot = np.array(pontos_rot)
pontos_2d = []
for p in pontos_rot:
x = int(p[0] * escala + tela.get_width() / 2)
y = int(p[1] * escala + tela.get_height() /2)
pontos_2d.append((x, y))
faces_ordenadas = []
for face, cor in faces:
z_medio = np.mean([pontos_rot[i][2] for i in face])
faces_ordenadas.append((z_medio, face, cor))
faces_ordenadas.sort(key=lambda f: f[0], reverse=True)
render(tela, pontos_2d, faces_ordenadas)
if __name__ == "__main__":
main()