-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
139 lines (97 loc) · 4.06 KB
/
main.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
from __future__ import annotations
import random
import numpy as np
import pygame
from pygame.locals import *
import stl
from renderer import Renderer
from viewport import Viewport
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
pygame.init()
class MainWindow:
default_resolution = np.array([960, 720])
font = pygame.font.Font(pygame.font.get_default_font(), 12)
mouse_sensitivity = 1 / 100.0
rotation_speed = np.pi/4
def __init__(self):
camera = Viewport(self.default_resolution, fov=120)
model = stl.load("assets/untitled.stl")
print(len(model.triangles))
scene = [
model.set_center(np.array([0.0, 0.0, 5.0])).scale(2).rotate(np.array([0.0, 0.0, np.pi / 2])).flip(y=True)
]
self.renderer = Renderer(camera, scene)
self.camera = self.renderer.camera
self.screen = pygame.display.set_mode(self.default_resolution, pygame.RESIZABLE)
self.clock = pygame.time.Clock()
self.dt = 1.0
def handle_keypresses(self):
keys = pygame.key.get_pressed()
if keys[K_LCTRL]:
player_speed = 5.0
else:
player_speed = 2.0
if keys[K_z]:
self.camera.position += player_speed * self.dt * np.array(
[np.sin(self.camera.orientation[1]), 0.0, np.cos(self.camera.orientation[1])],
dtype=float)
if keys[K_s]:
self.camera.position += player_speed * self.dt * np.array(
[-np.sin(self.camera.orientation[1]), 0.0, -np.cos(self.camera.orientation[1])], dtype=float)
if keys[K_q]:
self.camera.position += player_speed * self.dt * np.array(
[-np.cos(self.camera.orientation[1]), 0.0, np.sin(self.camera.orientation[1])],
dtype=float)
if keys[K_d]:
self.camera.position += player_speed * self.dt * np.array(
[np.cos(self.camera.orientation[1]), 0.0, -np.sin(self.camera.orientation[1])],
dtype=float)
if keys[K_LSHIFT]:
self.camera.position += player_speed * self.dt * np.array([0, -1, 0])
if keys[K_SPACE]:
self.camera.position += player_speed * self.dt * np.array([0, 1, 0])
def draw_screen(self):
self.screen.fill(BLACK)
self.renderer.objects[0].rotate(np.array([0.0, self.rotation_speed * self.dt, 0.0]))
self.renderer.render(self.screen)
pygame.display.flip()
def update(self):
self.dt = self.clock.tick(60) / 1000
for event in pygame.event.get():
if event.type == QUIT:
return False
if event.type == MOUSEMOTION:
if pygame.mouse.get_pressed(3)[0]:
self.camera.orientation += np.array([
0.0,
event.rel[0] * self.mouse_sensitivity * self.camera.resolution[1] / self.camera.resolution[0],
0.0 # event.rel[1] * self.mouse_sensitivity,
])
if event.type == MOUSEWHEEL:
if event.y > 0:
self.camera.fov += 1
else:
self.camera.fov -= 1
if event.type == KEYUP:
if event.key == K_UP:
self.camera.focal_length *= 2.0
if event.key == K_DOWN:
self.camera.focal_length /= 2.0
if event.type == WINDOWSIZECHANGED:
self.camera.resolution = np.array([event.x, event.y])
# self.camera.fov = self.camera.fov
self.handle_keypresses()
self.draw_screen()
rounded_pos = list(map(lambda x: round(x, 2), self.camera.position))
pygame.display.set_caption(
f"Position: {rounded_pos} | FOV: {self.camera.fov[0]} | FPS: {round(self.clock.get_fps())} | Frametime: {round(self.dt * 1000)}ms")
return True
def mainloop(self):
running = True
while running:
running = self.update()
pygame.quit()
if __name__ == "__main__":
win = MainWindow()
win.mainloop()