forked from condmaker/imfj1_2019_projecto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.py
91 lines (72 loc) · 2.71 KB
/
sample.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
# Import pygame into our program
import pygame
import pygame.freetype
import time
from scene import *
from object3d import *
from mesh import *
from material import *
from color import *
# Define a main function, just to keep things nice and tidy
def main():
# Initialize pygame, with the default parameters
pygame.init()
# Define the size/resolution of our window
res_x = 640
res_y = 480
# Create a window and a display surface
screen = pygame.display.set_mode((res_x, res_y))
# Create a scene
scene = Scene("TestScene")
scene.camera = Camera(False, res_x, res_y)
# Moves the camera back 2 units
scene.camera.position -= vector3(0,0,2)
# Create a cube and place it in a scene, at position (0,0,0)
# This cube has 1 unit of side, and is red
obj1 = Object3d("TestObject")
obj1.scale = vector3(1, 1, 1)
obj1.position = vector3(0, -1, 0)
obj1.mesh = Mesh.create_cube((1, 1, 1))
obj1.material = Material(color(1,0,0,1), "TestMaterial1")
scene.add_object(obj1)
# Create a second object, and add it as a child of the first object
# When the first object rotates, this one will also mimic the transform
obj2 = Object3d("ChildObject")
obj2.position += vector3(0, 0.75, 0)
obj2.mesh = Mesh.create_cube((0.5, 0.5, 0.5))
obj2.material = Material(color(0,1,0,1), "TestMaterial2")
obj1.add_child(obj2)
# Specify the rotation of the object. It will rotate 15 degrees around the axis given,
# every second
angle = 15
axis = vector3(1,0.7,0.2)
axis.normalize()
# Timer
delta_time = 0
prev_time = time.time()
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
# Game loop, runs forever
while (True):
# Process OS events
for event in pygame.event.get():
# Checks if the user closed the window
if (event.type == pygame.QUIT):
# Exits the application immediately
return
elif (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_ESCAPE):
return
# Clears the screen with a very dark blue (0, 0, 20)
screen.fill((0,0,0))
# Rotates the object, considering the time passed (not linked to frame rate)
q = from_rotation_vector((axis * math.radians(angle) * delta_time).to_np3())
obj1.rotation = q * obj1.rotation
scene.render(screen)
# Swaps the back and front buffer, effectively displaying what we rendered
pygame.display.flip()
# Updates the timer, so we we know how long has it been since the last frame
delta_time = time.time() - prev_time
prev_time = time.time()
# Run the main function
main()