-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (63 loc) · 2.49 KB
/
main.py
File metadata and controls
87 lines (63 loc) · 2.49 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
# main.py
import pygame
import numpy as np
import transformation
import shape
import constants
import rendering
def main():
# Initialize Pygame
pygame.init()
screen = pygame.display.set_mode((constants.WIDTH, constants.HEIGHT))
clock = pygame.time.Clock()
"""Main loop for rendering the cube."""
running = True
objects = list()
for i in range(9):
objects.append(shape.tetrahedron((i * 4, 5, 0)))
objects.append(shape.cube((i*4, 0, 0)))
objects.append(shape.pyramid((i*4, -5, 0)))
# Define camera position (starting at origin)
camera_pos = np.array([15.0, 0.0, -20.0]) # Start at x at 15 and z = -20 to see all objects
rotation_x, rotation_y, rotation_z = 0, 0, 0
while running:
screen.fill(constants.BLACK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Get user input for rotations
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
camera_pos[2] += constants.CAMERA_SPEED
if keys[pygame.K_s]:
camera_pos[2] -= constants.CAMERA_SPEED
if keys[pygame.K_a]:
camera_pos[0] -= constants.CAMERA_SPEED
if keys[pygame.K_d]:
camera_pos[0] += constants.CAMERA_SPEED
if keys[pygame.K_q]:
camera_pos[1] -= constants.CAMERA_SPEED
if keys[pygame.K_e]:
camera_pos[1] += constants.CAMERA_SPEED
rotation_x += constants.ROTATION_SPEED
rotation_y += constants.ROTATION_SPEED
rotation_z += constants.ROTATION_SPEED
# Wrap the angles to keep them in the range [0, 2π]
rotation_x %= constants.MAX_ROTATION
rotation_y %= constants.MAX_ROTATION
rotation_z %= constants.MAX_ROTATION
# Apply rotations to each object
for object in objects:
vertices, edges, position = object
# Apply rotation in order X -> Y
rotated_vertices = transformation.rotate_x(vertices, rotation_x)
rotated_vertices = transformation.rotate_y(rotated_vertices, rotation_y)
# Create the transformed object with rotated vertices
transformed_object = (rotated_vertices, edges, position)
# Draw the transformed object
rendering.draw_object(transformed_object, camera_pos, screen)
pygame.display.flip()
clock.tick(60) # 60 FPS
pygame.quit()
if __name__ == "__main__":
main()