def save_screen(window, filepath='screenshot.png'): from PIL import Image w, h = glfw.GetWindowSize(window) pixels = gl.glReadPixels(0, 0, w, h, gl.GL_RGB, gl.GL_UNSIGNED_BYTE) pil_image = Image.frombytes('RGB', (w, h), pixels).transpose(Image.FLIP_TOP_BOTTOM) pil_image.save(filepath) _logger.info('...saved %s', filepath)
def start_rotation(self, event_coll, event): start_screen = glfw.GetCursorPos(event.window) # Screen coordinates window_size = glfw.GetWindowSize(event.window) norm_x = -1.0 + 2.0 * start_screen[0] / window_size[0] norm_y = 1.0 - 2.0 * start_screen[1] / window_size[1] self.start = (norm_x, norm_y) self.rotation = True return False
def stop_rotation(self, event_coll, event): end_screen = glfw.GetCursorPos(event.window) window_size = glfw.GetWindowSize(event.window) norm_x = -1.0 + 2.0 * end_screen[0] / window_size[0] norm_y = 1.0 - 2.0 * end_screen[1] / window_size[1] end = (norm_x, norm_y) event_coll.camera.update_orientation(self.start[0], self.start[1], end[0], end[1]) self.rotation = False return True
def do_rotation(self, event_coll, event): if not self.rotation: return False new_end_screen = glfw.GetCursorPos(event.window) window_size = glfw.GetWindowSize(event.window) norm_x = -1.0 + 2.0 * new_end_screen[0] / window_size[0] norm_y = 1.0 - 2.0 * new_end_screen[1] / window_size[1] new_end = (norm_x, norm_y) event_coll.camera.update_orientation(self.start[0], self.start[1], new_end[0], new_end[1]) self.start = new_end return True
def capture_window(window, filename='screenshot.png'): import PIL import os.path if not filename.endswith('.png'): filename += '.png' _logger.info('saving screen capture...') mWidth, mHeight = glfw.GetWindowSize(window) gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1) pixels = gl.glReadPixels(0, 0, mWidth, mHeight, gl.GL_RGB, gl.GL_UNSIGNED_BYTE) pil_image = PIL.Image.frombytes('RGB', (mWidth, mHeight), pixels) pil_image = pil_image.transpose(PIL.Image.FLIP_TOP_BOTTOM) dire = os.path.dirname(os.path.abspath(filename)) if not os.path.exists(dire): from os import makedirs makedirs(dire) pil_image.save(filename) _logger.info('...saved screen capture to "%s"', filename)
def key_callback(window, key, scancode, action, mods): if (key == glfw.KEY_ESCAPE and action == glfw.PRESS): glfw.SetWindowShouldClose(window, True) if (action == glfw.PRESS and key == glfw.KEY_L): self.cameraLocked = not self.cameraLocked print 'cameraLocked:', self.cameraLocked glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_NORMAL if self.cameraLocked else glfw.CURSOR_DISABLED) # if not self.cameraLocked: # Ensure that locking/unlocking doesn't move the view: windowWidth, windowHeight = glfw.GetWindowSize(window) glfw.SetCursorPos(window, windowWidth / 2, windowHeight / 2) self.currentCamera.lastCursor = np.array(glfw.GetCursorPos(window), np.float32) self.currentCamera.lastUpdateTime = glfw.GetTime() if (action == glfw.PRESS and key == glfw.KEY_R): self.mainRender.renderCairo(self.playerCamera, self.cairoSavePath) pass
def show(game, title='poolvr.py *** GLViewer', window_size=(800, 600), gl_clear_color=(0.24, 0.18, 0.08, 0.0), before_frame_cb=None, after_frame_cb=None, double_buffered=True, playback_rate=1.0, screenshots_dir='', use_billboards=False): if not glfw.Init(): raise Exception('failed to initialize glfw') if not double_buffered: glfw.WindowHint(glfw.DOUBLEBUFFER, False) glfw.SwapInterval(0) window = glfw.CreateWindow(window_size[0], window_size[1], title) if not window: raise Exception('failed to create glfw window') glfw.MakeContextCurrent(window) _logger.info('GL_VERSION: %s', gl.glGetString(gl.GL_VERSION)) renderer = OpenGLRenderer(window_size=window_size, znear=0.1, zfar=1000) def on_resize(window, width, height): gl.glViewport(0, 0, width, height) renderer.window_size = (width, height) renderer.update_projection_matrix() glfw.SetWindowSizeCallback(window, on_resize) table = game.table physics = game.physics ball_meshes = table.setup_balls(game.ball_radius, game.ball_colors[:9], game.ball_positions, striped_balls=set(range(9, game.num_balls)), use_bb_particles=use_billboards) ball_shadow_meshes = [mesh.shadow_mesh for mesh in ball_meshes] camera_world_matrix = renderer.camera_matrix camera_position = camera_world_matrix[3, :3] camera_position[1] = table.height + 0.19 camera_position[2] = 0.183 * table.length cue = PoolCue() cue.position[1] = table.height + 0.1 process_keyboard_input = init_keyboard(window) meshes = [table.mesh, floor_mesh, skybox_mesh] + ball_meshes #+ [cue] for mesh in meshes: mesh.init_gl(force=True) ball_positions = game.ball_positions ball_quaternions = game.ball_quaternions ball_mesh_positions = [mesh.world_matrix[3, :3] for mesh in ball_meshes] ball_mesh_rotations = [mesh.world_matrix[:3, :3].T for mesh in ball_meshes] ball_shadow_mesh_positions = [ mesh.world_matrix[3, :3] for mesh in ball_shadow_meshes ] gl.glViewport(0, 0, window_size[0], window_size[1]) gl.glClearColor(*gl_clear_color) gl.glEnable(gl.GL_DEPTH_TEST) _logger.info('entering render loop...') stdout.flush() nframes = 0 max_frame_time = 0.0 lt = glfw.GetTime() t0 = physics.events[0].t t1 = physics.events[-1].t + min(2.0, physics.events[-1].T) pt = t0 while not glfw.WindowShouldClose(window) and pt <= t1: t = glfw.GetTime() dt = t - lt lt = t glfw.PollEvents() process_keyboard_input(dt, camera_world_matrix, cue=cue) renderer.process_input() with renderer.render(meshes=meshes) as frame_data: camera_position = frame_data.get('camera_position', renderer.camera_position) physics.eval_positions(pt, out=ball_positions) physics.eval_quaternions(pt, out=ball_quaternions) ball_positions[ ~physics. on_table] = camera_position # hacky way to only show balls that are on table for i, pos in enumerate(ball_positions): ball_mesh_positions[i][:] = pos ball_shadow_mesh_positions[i][0::2] = pos[0::2] for i, quat in enumerate(ball_quaternions): set_matrix_from_quaternion(quat, ball_mesh_rotations[i]) physics.step(dt) max_frame_time = max(max_frame_time, dt) if nframes == 0: st = glfw.GetTime() nframes += 1 pt += dt * playback_rate glfw.SwapBuffers(window) _logger.info( '...exited render loop: average FPS: %f, maximum frame time: %f', (nframes - 1) / (t - st), max_frame_time) mWidth, mHeight = glfw.GetWindowSize(window) gl.glPixelStorei(gl.GL_PACK_ALIGNMENT, 1) pixels = gl.glReadPixels(0, 0, mWidth, mHeight, gl.GL_RGB, gl.GL_UNSIGNED_BYTE) pil_image = PIL.Image.frombytes('RGB', (mWidth, mHeight), pixels) pil_image = pil_image.transpose(PIL.Image.FLIP_TOP_BOTTOM) filename = title.replace(' ', '_') + '-screenshot.png' filepath = os.path.join(screenshots_dir, filename) pil_image.save(filepath) _logger.info('..saved screen capture to "%s"', filepath) try: renderer.shutdown() _logger.info('...shut down renderer') except Exception as err: _logger.error(err) glfw.DestroyWindow(window) glfw.Terminate()
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, version[1]) glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, 1) glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) windows = [ glfw.CreateWindow(640, 480, 'Hello World {}'.format(count)) for count in range(2) ] if not windows: glfw.Terminate() print('Failed to create window') exit() glfw.SetWindowPos(windows[0], 0, 0) w, h = glfw.GetWindowSize(windows[0]) glfw.SetWindowPos(windows[1], w, 0) def update_windows(): for window in windows: if glfw.WindowShouldClose(window): return glfw.MakeContextCurrent(window) GL.glClearColor(0.2, 0.2, 0.2, 1.0) GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) glfw.SwapBuffers(window) glfw.MakeContextCurrent(windows[0])
def width(self): if self._win: return glfw.GetWindowSize(self._win)[0] return 0.0
def height(self): if self._win: return glfw.GetWindowSize(self._win)[1] return 0.0