def start(): pygame.init() screen = pygame.display.set_mode(SCREEN_SIZE, pygame.HWSURFACE|pygame.OPENGL|pygame.DOUBLEBUF) resize(*SCREEN_SIZE) init() clock = pygame.time.Clock() # This object renders the 'map' map = Map() # Camera transform matrix cam_matrix = Matrix44() cam_matrix.translate = (10.0, 0.6, 10.0) # Initialize speeds and directions rotation_direction = Vector3() rotation_speed = radians(90.0) movement_direction = Vector3() movement_speed = 5.0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE: pygame.quit() quit() # Clear the screen, and z-buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) time_passed = clock.tick() time_passed_seconds = time_passed / 1000.0 pressed_keys = pygame.key.get_pressed() # Reset rotation and movement directions rotation_direction.set(0.0, 0.0, 0.0) movement_direction.set(0.0, 0.0, 0.0) # Modify direction vectors for key presses if pressed_keys[pygame.K_LEFT]: rotation_direction.y = +1.0 elif pressed_keys[pygame.K_RIGHT]: rotation_direction.y = -1.0 if pressed_keys[pygame.K_UP]: rotation_direction.x = -1.0 elif pressed_keys[pygame.K_DOWN]: rotation_direction.x = +1.0 if pressed_keys[pygame.K_z]: rotation_direction.z = -1.0 elif pressed_keys[pygame.K_x]: rotation_direction.z = +1.0 if pressed_keys[pygame.K_q]: movement_direction.z = -1.0 elif pressed_keys[pygame.K_a]: movement_direction.z = +1.0 # Calculate rotation matrix and multiply by camera matrix rotation = rotation_direction * rotation_speed * time_passed_seconds rotation_matrix = Matrix44.xyz_rotation(*rotation) cam_matrix *= rotation_matrix # Calcluate movment and add it to camera matrix translate heading = Vector3(cam_matrix.forward) movement = heading * movement_direction.z * movement_speed cam_matrix.translate += movement * time_passed_seconds # Upload the inverse camera matrix to OpenGL glLoadMatrixd(cam_matrix.get_inverse().to_opengl()) # Light must be transformed as well glLight(GL_LIGHT0, GL_POSITION, (0, 1.5, 1, 0)) map.render() pygame.display.flip()
import math from gameobjects.matrix44 import Matrix44 identity = Matrix44() print(identity) p1 = (1.0, 2.0, 3.0) p2 = identity.transform(p1) print(p2) print(identity.get_column(2)) translation = Matrix44.translation(10, 5, 2) print(translation) scale = Matrix44.scale(2.0) print(scale) s = scale.transform(p1) print(s) z_rotate = Matrix44.z_rotation(math.radians(-45)) print(z_rotate) a = (0, 10, 0) b = z_rotate.transform(a) print(b) translation1 = Matrix44.translation(5, 10, 2)