def scene2(): scene = Scene() obj = GameObject('0') obj.load_mesh('../data/camero.obj') obj.load_texture('../data/camero.png', 256) obj.set_scale(20) obj.rotate_y(-45) scene.objects += [obj] scene.light = Light() scene.light.shadow_map_dim = 512 scene.light.shadow_map_bias = 1 scene.light.translate_z(1000) scene.light.translate_y(1500) scene.light.translate_x(2000) scene.light.rotate_y(100) scene.light.rotate_x(45) image_width = 640 image_height = 480 scene.camera = Camera(0.98, 0.735, image_width, image_height, 1, 10000, 20, np.eye(4)) scene.camera.translate_y(40) scene.camera.translate_z(60) scene.camera.rotate_x(35) return scene
def test_render(self): renderer = LuxRenderer(samples_per_pixel=1000) scene = Scene() scene.camera = Camera(loc=(0, -10, 0), to=(0, 0, 0)) scene.objects.append(Sphere()) scene.objects.append(Sphere(center=(1, -1, 0.5), radius=0.5, light=AreaLight(color=(1, 1, 1)))) renderer.render(scene, generate_only=True)
def test_render(self): renderer = LuxRenderer(samples_per_pixel=1000) scene = Scene() scene.camera = Camera(loc=(0, -10, 0), to=(0, 0, 0)) scene.objects.append(Sphere()) scene.objects.append( Sphere(center=(1, -1, 0.5), radius=0.5, light=AreaLight(color=(1, 1, 1)))) renderer.render(scene, generate_only=True)
def scene1(): scene = Scene() obj = GameObject('0') obj.load_mesh('../data/deer.obj') obj.set_scale(1 / 30) obj.translate_x(10) obj.rotate_y(-35) scene.objects += [obj] obj = GameObject('1') obj.load_mesh('../data/cube.obj') obj.set_scale(100) obj.translate_x(-50) obj.translate_y(-100) obj.translate_z(-50) scene.objects += [obj] obj = GameObject('2') obj.load_mesh('../data/camero.obj') obj.load_texture('../data/camero.png', 64) obj.set_scale(10) obj.translate_x(-20) obj.translate_z(8) obj.rotate_y(-45) scene.objects += [obj] scene.light = Light() scene.light.shadow_map_dim = 64 scene.light.shadow_map_bias = 1 scene.light.translate_z(1000) scene.light.translate_y(1500) scene.light.translate_x(2000) scene.light.rotate_y(100) scene.light.rotate_x(35) image_width = 320 image_height = 240 scene.camera = Camera(0.98, 0.735, image_width, image_height, 1, 10000, 20, np.eye(4)) scene.camera.translate_y(60) scene.camera.translate_z(60) scene.camera.rotate_x(35) print(scene.camera.world_to_camera) return scene
def main(): """Main function, it implements the application loop""" # 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 sphere and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") obj1.scale = Vector3(1, 1, 1) obj1.position = Vector3(0, 0, 0) obj1.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 0 axis = Vector3(0, 1, 0) axis.normalize() # Timer delta_time = 0 prev_time = time.time() pygame.mouse.set_visible(True) pygame.event.set_grab(False) # 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 = Quaternion.AngleAxis(axis, math.radians(angle) * delta_time) 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() #Variavel que vai guardar a tecla pressionada k = pygame.key.get_pressed() #Cada tecla tem a sua própria ação if (k[pygame.K_UP]): angle = 15 axis = Vector3(1, 0, 0) elif (k[pygame.K_DOWN]): angle = -15 axis = Vector3(1, 0, 0) elif (k[pygame.K_LEFT]): angle = 15 axis = Vector3(0, 1, 0) elif (k[pygame.K_RIGHT]): angle = -15 axis = Vector3(0, 1, 0) elif (k[pygame.K_PAGEUP]): angle = 15 axis = Vector3(0, 0, 1) elif (k[pygame.K_PAGEDOWN]): angle = -15 axis = Vector3(0, 0, 1) elif (k[pygame.K_w]): obj1.position.y = obj1.position.y + 0.001 elif (k[pygame.K_s]): obj1.position.y = obj1.position.y - 0.001 elif (k[pygame.K_a]): obj1.position.x = obj1.position.x - 0.001 elif (k[pygame.K_d]): obj1.position.x = obj1.position.x + 0.001 elif (k[pygame.K_q]): obj1.position.z = obj1.position.z + 0.001 elif (k[pygame.K_e]): obj1.position.z = obj1.position.z - 0.001 else: angle = 0
def main(): """Main function, it implements the application loop""" # 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, 4) # Create a sphere and place it in a scene, at position (0,0,0) ''' obj1 = Object3d("TestObject") obj1.scale = Vector3(1, 1, 1) obj1.position = Vector3(0, 0, 0) obj1.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) ''' # puts name of file that you wanna load # if you dont want to load a file leave it as "" fileloaded = "teste2.obj" #if theres so file to be loaded if fileloaded == "": # Create a pyramid and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") obj1.scale = Vector3(1, 1, 1) obj1.position = Vector3(0, 0, 0) obj1.mesh = Mesh.create_pyramid() obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) else: # Create a object chosen by the user and place it in a scene, at position (0,0,0) obj1 = Object3d("TestObject") obj1.scale = Vector3(1, 1, 1) obj1.position = Vector3(0, 0, 0) #function to enable objects to be loaded from terminal if len(sys.argv) == 2: print(True) obj1.mesh = Mesh.create_mesh(sys.argv[1]) #if nothing at the terminal just run normaly with the name of the file in code else: obj1.mesh = Mesh.create_mesh(fileloaded) # add object to viewer obj1.material = Material(Color(1, 0, 0, 1), "TestMaterial1") scene.add_object(obj1) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 50 axis = Vector3(1, 0.7, 0.2) axis.normalize() # Timer delta_time = 0 prev_time = time.time() pygame.mouse.set_visible(True) pygame.event.set_grab(False) # 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 #if your pressing a key elif event.type == pygame.KEYDOWN: #exit the game if event.key == pygame.K_ESCAPE: return # rotate object in y axis if event.key == pygame.K_LEFT: axis = Vector3(0, 1, 0) if event.key == pygame.K_RIGHT: axis = Vector3(0, -1, 0) # rotate object in x axis if event.key == pygame.K_UP: axis = Vector3(1, 0, 0) if event.key == pygame.K_DOWN: axis = Vector3(-1, 0, 0) # rotate object in z axis if event.key == pygame.K_PAGEUP: axis = Vector3(0, 0, 1) if event.key == pygame.K_PAGEDOWN: axis = Vector3(0, 0, -1) #normalize axis axis.normalize() # 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) ax = (axis * math.radians(angle) * delta_time) q = Quaternion.AngleAxis(axis, math.radians(angle) * delta_time) #Object movement keys = pygame.key.get_pressed() # movementing object up down right and left if keys[pygame.K_LEFT]: obj1.rotation = q * obj1.rotation if keys[pygame.K_RIGHT]: obj1.rotation = q * obj1.rotation if keys[pygame.K_UP]: obj1.rotation = q * obj1.rotation if keys[pygame.K_DOWN]: obj1.rotation = q * obj1.rotation #functions to make the rotations work if keys[pygame.K_PAGEUP]: obj1.rotation = q * obj1.rotation if keys[pygame.K_PAGEDOWN]: obj1.rotation = q * obj1.rotation if keys[pygame.K_w]: obj1.position = Vector3(obj1.position.x, obj1.position.y + 0.006, obj1.position.z) if keys[pygame.K_s]: obj1.position = Vector3(obj1.position.x, obj1.position.y - 0.006, obj1.position.z) if keys[pygame.K_a]: obj1.position = Vector3(obj1.position.x - 0.006, obj1.position.y, obj1.position.z) if keys[pygame.K_d]: obj1.position = Vector3(obj1.position.x + 0.006, obj1.position.y, obj1.position.z) if keys[pygame.K_q]: obj1.position = Vector3(obj1.position.x, obj1.position.y, obj1.position.z - 0.006) if keys[pygame.K_e]: obj1.position = Vector3(obj1.position.x, obj1.position.y, obj1.position.z + 0.006) #rendering objects on to screen 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()
def main(): """Main function, it implements the application loop""" # Initialize pygame, with the default parameters pygame.init() # Define the size/resolution of our window res_x = 1280 res_y = 720 # 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, 0) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, every second axis = scene.camera.up() axis.normalize() # Timer delta_time = 0 prev_time = time.time() # Show mouse cursor pygame.mouse.set_visible(False) # Don't lock the mouse cursor to the game window pygame.event.set_grab(True) #Clean count to make a max nuber of cubes count = 0 #Number of cubes in scene while count < 72: #function to add 72 cubes in scene at random new_cube = RandomCube() scene.add_object(new_cube) count += 1 # 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 axis.normalize() # Clears the screen with a very dark blue (0, 0, 20) screen.fill((0, 0, 20)) keys=pygame.key.get_pressed() # Confine the mouse to the center of the screen pygame.mouse.set_pos(res_x/2, res_y/2) # Get the mouse movement from the last frmae rel = pygame.mouse.get_rel() # Gets the rotation angle rotAngle = rel[0] * 4 if rel[0] != 0: # Rotates the object, considering the time passed (not linked to frame rate) q = Quaternion.AngleAxis(axis, math.radians(rotAngle) * delta_time) #camera rotation scene.camera.rotation = q * scene.camera.rotation ''' Movement Stuff''' #going forwards if keys[pygame.K_w]: scene.camera.position += scene.camera.forward() * delta_time * 4 #going backwards if keys[pygame.K_s]: scene.camera.position -= scene.camera.forward() * delta_time * 4 #going left if keys[pygame.K_a]: scene.camera.position -= scene.camera.right() * delta_time * 4 #going right if keys[pygame.K_d]: scene.camera.position += scene.camera.right() * delta_time * 4 # Render scene scene.render(screen) # Swaps the back and front buffer, effectively displaying what we rendered pygame.display.flip() #Limit fps to 144 while time.time() - prev_time < 0.00694: continue # 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()
def main(): """Main function, it implements the application loop""" # Initialize pygame, with the default parameters pygame.init() # Define the size/resolution of our window res_x = 1280 res_y = 720 # 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, 0) # Creates the terrain meshes and materials terrain_object = create_terrain() scene.add_object(terrain_object) # Game variables # A missile spawns every 2 seconds (this time gets smaller every missile, to # make the game harder with time) missile_spawn_time = 2 # Time until the next missile is shot missile_timer = missile_spawn_time # Storage for all missiles active in the game missiles = [] # Flashing effect Color flash_color = Color(0, 0, 0, 0) # Total time of the current flash total_flash_time = 0 # Time elapsed for the current flash flash_timer = 0 # Minimum time between player shots shot_cooldown = 0.2 # Timer for the shot cooldown shot_timer = 0 # Storage for all the shots active in the game shots = [] # Timer delta_time = 0 prev_time = time.time() # Show mouse cursor pygame.mouse.set_visible(True) # Don't lock the mouse cursor to the game window pygame.event.set_grab(False) # 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 ESC is pressed exit the application if event.key == pygame.K_ESCAPE: return elif event.type == pygame.MOUSEBUTTONDOWN: # If the user has the mouse button down if shot_timer <= 0: # We're not on cooldown, so we can shoot # Get the mouse position and convert it to NDC (normalized # device coordinates) mouse_pos = pygame.mouse.get_pos() mouse_pos = ((mouse_pos[0] / res_x) * 2 - 1, (mouse_pos[1] / res_y) * 2 - 1) # Create a shot shot = Shot() # Initialize the position and direction of the shot, based on the # mouse position on the screen. For this, we request the scene camera the # ray corresponding to the mouse position shot.position, shot.direction = scene.camera.ray_from_ndc( mouse_pos) # Add the shot to the scene (for rendering) and on the shot storage, for # all other operations scene.add_object(shot) shots.append(shot) # Reset the cooldown shot_timer = shot_cooldown # Clears the screen with a very dark blue (0, 0, 20) screen.fill((0, 0, 0)) # Spawn new missiles missile_timer -= delta_time if missile_timer < 0: # It's time to spawn a new missile # Reduce time until the next missile, but clamp it so we don't have more than # one missile every half a second missile_spawn_time -= 0.025 if missile_spawn_time < 0.5: missile_spawn_time = 0.5 # Reset the timer missile_timer = missile_spawn_time # Create a new missile new_missile = Missile() # Add the missile to the scene (for rendering) and on the missile storage, # for all other operations scene.add_object(new_missile) missiles.append(new_missile) # Animate missiles # We need the following to keep track of all missiles we'll have to destroy, # since we can't change the storage while we're iterating it missiles_to_destroy = [] for missile in missiles: # Update the missile missile.update(delta_time) # Check if the missile is close to the player plane (z < 0.1) if missile.position.z < 0.1: # Check if the missile is close enough to the player to hit him if missile.position.magnitude() < 0.25: # It has hit the player, flash the screen red for one second flash_color = Color(1, 0, 1, 1) total_flash_time = flash_timer = 1 # Destroy missile (remove it from the scene and add it to the destruction # list so we can remove it in a bit) missiles_to_destroy.append(missile) scene.remove_object(missile) # Animate shots # We need the following to keep track of all shots we'll have to destroy, # since we can't change the storage while we're iterating it shots_to_destroy = [] for shot in shots: # Update the shot shot.update(delta_time) # Check if the shot is too far away, and add it to the destruction list, # besides removing it from the scene if shot.position.z > 12: # Destroy shot shots_to_destroy.append(shot) scene.remove_object(shot) # Update shot cooldown shot_timer -= delta_time # Check collisions for shot in shots: # Check each shot with each missile for missile in missiles: # Check the distance between the shot and the missile, it it is below # a threshould (in this case 0.5), destroy the missile and the shot distance = Vector3.distance(shot.position, missile.position) if distance < 0.5: # Add it to the missile destruction list, and remove it from the scene missiles_to_destroy.append(missile) scene.remove_object(missile) # Add it to the shot destruction list, and remove it from the scene shots_to_destroy.append(shot) scene.remove_object(shot) # Flash the screen cyan for 0.5 seconds flash_color = Color(0, 1, 1, 1) total_flash_time = flash_timer = 0.5 # Actually delete objects missiles = [x for x in missiles if x not in missiles_to_destroy] shots = [x for x in shots if x not in shots_to_destroy] # Render scene scene.render(screen) # Render screen flash if flash_timer > 0: flash_timer -= delta_time if flash_timer > 0: flash_color.a = flash_timer / total_flash_time # Render the full screen rectangle, using additive blend screen.fill(flash_color.premult_alpha().tuple3(), None, pygame.BLEND_RGB_ADD) # 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()
def main(): # Initialize pygame, with the default parameters pygame.init() # Define the size/resolution of our window screenX = 640 screenY = 480 # Create a window and a display surface screen = pygame.display.set_mode((screenX, screenY)) # Create a scene scene = Scene("TesteCena") scene.camera = Camera(False, screenX, screenY) # Moves the camera back 2 units scene.camera.position -= Vector3(0, 0, 2) # Create a sphere and place it in a scene, at position (0,0,0) obj = Object3d("Object") obj.scale = Vector3(1, 1, 1) obj.position = Vector3(0, 0, 0) #Number of sides of the cilinder sides = 12 #Create a cilinder obj.mesh = Mesh.create_cylinder(sides, 1, 1, None) #Create a sphere #obj.mesh = Mesh.create_sphere((1, 1, 1), 12, 12) #Create a cube #obj.mesh = Mesh.create_cube((5, 5, 5)) obj.material = Material(Color(1, 0, 0, 1), "Material1") scene.add_object(obj) # Timer delta_time = 0 prev_time = time.time() pygame.mouse.set_visible(True) pygame.event.set_grab(False) # Game loop, runs forever while True: #Busca o evento events = pygame.event.get() key = pygame.key.get_pressed() ang = 20 #Quando é carregada a tecla W if key[pygame.K_w]: movement = Vector3(0, 1, 0) movement.normalize() obj.position = obj.position + movement * delta_time #Quando é carregada a tecla S if key[pygame.K_s]: movement = Vector3(0, -1, 0) movement.normalize() obj.position = obj.position + movement * delta_time #Quando é carregada a tecla A if key[pygame.K_a]: movement = Vector3(-1, 0, 0) movement.normalize() obj.position = obj.position + movement * delta_time #Quando é carregada a tecla D if key[pygame.K_d]: movement = Vector3(1, 0, 0) movement.normalize() obj.position = obj.position + movement * delta_time #Quando é carregada a tecla Q if key[pygame.K_q]: movement = Vector3(0, 0, 1) movement.normalize() obj.position = obj.position + movement * delta_time #Quando é carregada a tecla E if key[pygame.K_e]: movement = Vector3(0, 0, -1) movement.normalize() obj.position = obj.position + movement * delta_time #Quando é carregada a seta para a direita if key[pygame.K_RIGHT]: rot = Vector3(0, -1, 0) q = Quaternion.AngleAxis(rot.normalized(), math.radians(ang) * delta_time) obj.rotation = q * obj.rotation #Quando é carregada a seta para a esquerda if key[pygame.K_LEFT]: rot = Vector3(0, 1, 0) q = Quaternion.AngleAxis(rot.normalized(), math.radians(ang) * delta_time) obj.rotation = q * obj.rotation #Quando é carregada a seta para baixo if key[pygame.K_DOWN]: rot = Vector3(-1, 0, 0) q = Quaternion.AngleAxis(rot.normalized(), math.radians(ang) * delta_time) obj.rotation = q * obj.rotation #Quando é carregada a seta para cima if key[pygame.K_UP]: rot = Vector3(1, 0, 0) q = Quaternion.AngleAxis(rot.normalized(), math.radians(ang) * delta_time) obj.rotation = q * obj.rotation #Quando é carregada a tecla PgUp if key[pygame.K_PAGEUP]: rot = Vector3(0, 0, 1) q = Quaternion.AngleAxis(rot.normalized(), math.radians(ang) * delta_time) obj.rotation = q * obj.rotation #Quando é carregada a tecla PgDn if key[pygame.K_PAGEDOWN]: rot = Vector3(0, 0, -1) q = Quaternion.AngleAxis(rot.normalized(), math.radians(ang) * delta_time) obj.rotation = q * obj.rotation for event in events: #Quando é carregada a tecla ESC if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: exit() #Quando é carregado o botão para sair do programa if event.type == pygame.QUIT: exit() # Clears the screen with a very dark blue (0, 0, 20) screen.fill((0, 0, 0)) 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()
def main(): """Main function, it implements the application loop""" # Initialize pygame, with the default parameters pygame.init() # Define the size/resolution of our window res_x = 1280 res_y = 720 # 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, 4) scene.camera.rotation = Quaternion.AngleAxis(Vector3(1, 0, 0), math.radians(-15)) # Creates the terrain meshes and materials terrain_meshes, terrain_materials = create_terrain() # Create container object for all the terrain sub-objects master_object = Object3d("TerrainObject") master_object.position = Vector3(0, -1, 0) scene.add_object(master_object) # Create the terrain objects and place it in a scene, at position (0,0,0) for _, (mesh, material) in enumerate(zip(terrain_meshes, terrain_materials)): obj = Object3d("TerrainObject") obj.scale = Vector3(1, 1, 1) obj.position = Vector3(0, 0, 0) obj.mesh = mesh obj.material = material master_object.add_child(obj) # Specify the rotation of the object. It will rotate 15 degrees around the axis given, # every second angle = 15 axis = Vector3(0, 1, 0) axis.normalize() # Timer delta_time = 0 prev_time = time.time() # Show mouse cursor pygame.mouse.set_visible(True) # Don't lock the mouse cursor to the game window pygame.event.set_grab(False) # 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 = Quaternion.AngleAxis(axis, math.radians(angle) * delta_time) master_object.rotation = q * master_object.rotation #Commented code serves to make benchmarks Mesh.stat_vertex_count = 0 Mesh.stat_transform_time = 0 Mesh.stat_render_time = 0 scene.render(screen) #Writes the benchmarks results print("Frame stats:") print(f"Vertex count = {Mesh.stat_vertex_count}") print(f"Transform time = {Mesh.stat_transform_time}s") print(f"Render time = {Mesh.stat_render_time}s") # 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() print(f"Frame time = {delta_time}s")
def main(): """Main function, it implements the application loop""" # Initialize pygame, with the default parameters pygame.init() # Define the size/resolution of our window res_x = 1280 res_y = 720 # 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, 0) # Creates the terrain meshes and materials terrain_object = create_terrain() scene.add_object(terrain_object) # Timer delta_time = 0 prev_time = time.time() # 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 key_state = pygame.key.get_pressed() if key_state[pygame.K_ESCAPE]: return if key_state[pygame.K_d]: scene.camera.position = Vector3(scene.camera.position.x + 0.1, scene.camera.position.y, scene.camera.position.z) if key_state[pygame.K_a]: scene.camera.position = Vector3(scene.camera.position.x - 0.1, scene.camera.position.y, scene.camera.position.z) if key_state[pygame.K_w]: scene.camera.position = Vector3(scene.camera.position.x, scene.camera.position.y, scene.camera.position.z + 0.1) if key_state[pygame.K_s]: scene.camera.position = Vector3(scene.camera.position.x, scene.camera.position.y, scene.camera.position.z - 0.1) # Clears the screen with a very dark blue (0, 0, 20) screen.fill((0, 0, 20)) # Render scene 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()
def main(): """Main function, it implements the application loop""" # 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) obj1 = Object3d("TestObject") obj1.scale = Vector3(1, 1, 1) obj1.position = Vector3(0, 0, 2) 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: # If ESC is pressed exit the application return # Clears the screen with a very dark blue (0, 0, 20) screen.fill((0, 0, 20)) # Rotates the object, considering the time passed (not linked to frame rate) q = Quaternion.AngleAxis(axis, math.radians(angle) * delta_time) 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()
def main(): """Main function, it implements the application loop""" # 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 the cube mesh we're going to use for every single object cube_mesh = Mesh.create_cube((1, 1, 1)) # Spawn rate is one cube every 25 ms spawn_rate = 0.025 # Keep a timer for the cube spawn cube_spawn_time = spawn_rate # Storage for all the objects created this way falling_objects = [] # Timer delta_time = 0 prev_time = time.time() # Show mouse cursor pygame.mouse.set_visible(True) # Don't lock the mouse cursor to the game window pygame.event.set_grab(False) # 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: # If ESC is pressed exit the application return # Clears the screen with a very dark blue (0, 0, 20) screen.fill((0, 0, 0)) # Update the cube spawn timer cube_spawn_time = cube_spawn_time - delta_time if cube_spawn_time < 0: # It's time to spawn a new cube cube_spawn_time = spawn_rate # Create a new cube, and add it to the scene new_cube = FallingCube(cube_mesh) scene.add_object(new_cube) # Add the new cube to the storage, so it can be updated falling_objects.append(new_cube) # Update the cubes for falling_object in falling_objects: falling_object.update(delta_time) # Is the cube fallen too far? if falling_object.position.y < -8: # Remove cube from scene scene.remove_object(falling_object) # Update the storage, so that all cubes that have fallen too far disappear falling_objects = [x for x in falling_objects if x.position.y > -8] # Render the scene 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()
def main(): # Initialize pygame, with the default parameters pygame.init() # Define the size/resolution of our window res_x = 1280 res_y = 720 # 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, 0) # Creates the terrain meshes and materials terrain_object = create_terrain() scene.add_object(terrain_object) # Minimum time between player shots shot_cooldown = 0.2 # Timer for the shot cooldown shot_timer = 0 # Storage for all the shots active in the game shots = [] # Timer delta_time = 0 prev_time = time.time() # Don't show mouse cursor pygame.mouse.set_visible(False) # Lock the mouse cursor to the game window pygame.event.set_grab(True) # Game loop, runs forever while True: #Busca o evento events = pygame.event.get() key = pygame.key.get_pressed() speed = 0.05 ang = 20 #Manter o rato no centro do ecrã displayCenter = [screen.get_size()[i] // 2 for i in range(2)] mouseMove = (0, 0) pygame.mouse.set_pos(displayCenter) #Buscar o movimento do rato e devolve (x, y) mouseMove = pygame.mouse.get_rel() #Código para movimentação da câmara a partir do movimento do rato rot = Vector3(mouseMove[1], mouseMove[0], 0) scene.camera.rotation *= Quaternion.AngleAxis( rot, math.radians(ang) * delta_time) #Para impedir que a câmara rode no eixo do Z scene.camera.rotation.z = 0 #Quando é carregada a tecla W if key[pygame.K_w]: movement = Vector3(0, 0, 1) movement.normalize() scene.camera.position += movement * delta_time + scene.camera.forward( ) * speed #Quando é carregada a tecla S if key[pygame.K_s]: movement = Vector3(0, 0, -1) movement.normalize() scene.camera.position += movement * delta_time - scene.camera.forward( ) * speed #Quando é carregada a tecla A if key[pygame.K_a]: movement = Vector3(-1, 0, 0) movement.normalize() scene.camera.position += movement * delta_time - scene.camera.right( ) * speed #Quando é carregada a tecla D if key[pygame.K_d]: movement = Vector3(1, 0, 0) movement.normalize() scene.camera.position += movement * delta_time + scene.camera.right( ) * speed # Process OS events for event in events: # Checks if the user closed the window if event.type == pygame.QUIT: # Exits the application immediately return elif event.type == pygame.KEYDOWN: # If ESC is pressed exit the application if event.key == pygame.K_ESCAPE: return elif event.type == pygame.MOUSEBUTTONDOWN: # If the user has the mouse button down if shot_timer <= 0: # We're not on cooldown, so we can shoot # Get the mouse position and convert it to NDC (normalized # device coordinates) mouse_pos = pygame.mouse.get_pos() mouse_pos = ((mouse_pos[0] / res_x) * 2 - 1, (mouse_pos[1] / res_y) * 2 - 1) # Create a shot shot = Shot() # Initialize the position and direction of the shot, based on the # mouse position on the screen. For this, we request the scene camera the # ray corresponding to the mouse position shot.position, shot.direction = scene.camera.ray_from_ndc( mouse_pos) # Add the shot to the scene (for rendering) and on the shot storage, for # all other operations scene.add_object(shot) shots.append(shot) # Reset the cooldown shot_timer = shot_cooldown # Clears the screen with a black (0, 0, 0) screen.fill((0, 0, 0)) # Animate shots # We need the following to keep track of all shots we'll have to destroy, # since we can't change the storage while we're iterating it shots_to_destroy = [] for shot in shots: # Update the shot shot.update(delta_time) # Check if the shot is too far away, and add it to the destruction list, # besides removing it from the scene if shot.position.z > 12: # Destroy shot shots_to_destroy.append(shot) scene.remove_object(shot) # Update shot cooldown shot_timer -= delta_time # Actually delete objects shots = [x for x in shots if x not in shots_to_destroy] # Render scene 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()