def init_mouse(window): glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_DISABLED) mouse_button_state = defaultdict(int) def on_mousedown(window, button, action, mods): if action == glfw.PRESS: mouse_button_state[button] = True elif action == glfw.RELEASE: mouse_button_state[button] = False glfw.SetMouseButtonCallback(window, on_mousedown) cursor_pos = glfw.GetCursorPos(window) theta_x, theta_y = 0.0, 0.0 def process_mouse_input(dt, cue): pos = glfw.GetCursorPos(window) nonlocal cursor_pos lr, fb = pos[0] - cursor_pos[0], pos[1] - cursor_pos[1] cursor_pos = pos nonlocal theta_x, theta_y cue.velocity[2] = fb * MOUSE_CUE_MOVE_SPEED cue.velocity[0] = lr * MOUSE_CUE_MOVE_SPEED cue.position[:] += dt * cue.velocity return process_mouse_input
def init(self): assert not self._initialized glfw.SetMouseButtonCallback(self._window, self._on_mouse_button) self._double_clicks.set_double_click_callback( self._on_mouse_double_click) glfw.SetScrollCallback(self._window, self._on_scroll) glfw.SetCursorPosCallback(self._window, self._on_mouse_move) glfw.SetKeyCallback(self._window, self._on_keyboard) glfw.SetDropCallback(self._window, self._on_drop) glfw.SetWindowSizeCallback(self._window, self._on_resize) glfw.SetWindowCloseCallback(self._window, lambda window: self._fps_limiter.stop()) yield from self._gl_executor.init_gl_context() yield from self._renderer.init() self._initialized = True
def setup_loop(self, scene, camera): scene.set_camera(camera) scene.update_minmax() camera.compute_matrices() print("Starting rendering...") callbacks = EventCollection(scene, camera) # register key callbacks defined in # yt.visualization.volume_rendering.input_events callbacks.add_key_callback("close_window", "escape") callbacks.add_key_callback("zoomin", "w") callbacks.add_key_callback("zoomout", "s") callbacks.add_key_callback("closeup", "z") callbacks.add_key_callback("cmap_cycle", "c") callbacks.add_key_callback("reset", "r") callbacks.add_key_callback("camera_orto", "o") callbacks.add_key_callback("camera_proj", "p") callbacks.add_key_callback("shader_max", "1") callbacks.add_key_callback("shader_proj", "2") callbacks.add_key_callback("shader_test", "3") callbacks.add_key_callback("print_limits", "g") callbacks.add_key_callback("print_help", "h") callbacks.add_key_callback("debug_buffer", "d") callbacks.add_key_callback("cmap_max_up", "right_bracket") callbacks.add_key_callback("cmap_max_down", "left_bracket") callbacks.add_key_callback("cmap_min_up", "semicolon") callbacks.add_key_callback("cmap_min_down", "apostrophe") callbacks.add_key_callback("cmap_toggle_log", "l") mouse_callbacks = MouseRotation() callbacks.add_mouse_callback( mouse_callbacks.start_rotation, glfw.MOUSE_BUTTON_LEFT ) callbacks.add_mouse_callback( mouse_callbacks.stop_rotation, glfw.MOUSE_BUTTON_LEFT, action="release" ) callbacks.add_framebuffer_callback("framebuffer_size_callback") callbacks.add_render_callback(mouse_callbacks.do_rotation) glfw.SetFramebufferSizeCallback(self.window, callbacks.framebuffer_call) glfw.SetKeyCallback(self.window, callbacks.key_call) glfw.SetMouseButtonCallback(self.window, callbacks.mouse_call) callbacks.draw = True return callbacks
def setup_controls(window=None, camera_world_matrix=None, move_speed=None, turn_speed=0.5, screen_capture_prefix='screen-capture'): if move_speed is None: move_speed = 1.5 * abs(camera_world_matrix[3,3] / camera_world_matrix[2,2]) _logger.debug('move_speed = %s', move_speed) _logger.info(''' KEYBOARD CONTROLS: W/S/A/D ----------- move Fwd/Bwd/Lft/Rgt Q/Z --------------- move Up/Down <-/-> (arrow keys)- turn Lft/Rgt C ----------------- capture screenshot Esc --------------- quit ''') camera_position = camera_world_matrix[3, :3] camera_rotation = camera_world_matrix[:3, :3] dposition = np.zeros(3, dtype=np.float32) rotation = np.eye(3, dtype=np.float32) key_state = defaultdict(bool) key_state_chg = defaultdict(bool) def on_keydown(window, key, scancode, action, mods): if key == glfw.KEY_ESCAPE and action == glfw.PRESS: glfw.SetWindowShouldClose(window, gl.GL_TRUE) elif action == glfw.PRESS: if not key_state[key]: key_state_chg[key] = True else: key_state_chg[key] = False key_state[key] = True elif action == glfw.RELEASE: if key_state[key]: key_state_chg[key] = True else: key_state_chg[key] = False key_state[key] = False glfw.SetKeyCallback(window, on_keydown) def on_mousedown(window, button, action, mods): pass glfw.SetMouseButtonCallback(window, on_mousedown) _capture_wait = 0.0 _num_captures = 0 def process_input(dt): nonlocal _capture_wait, _num_captures glfw.PollEvents() if _capture_wait > 0: _capture_wait -= dt elif key_state[glfw.KEY_C]: _logger.info('''capturing screen... camera position: %s''', camera_position) save_screen(window, '.'.join([screen_capture_prefix, '%03d' % _num_captures, 'png'])) _num_captures += 1 _capture_wait = 0.25 dposition[:] = 0.0 if key_state[glfw.KEY_W]: dposition[2] -= dt * move_speed if key_state[glfw.KEY_S]: dposition[2] += dt * move_speed if key_state[glfw.KEY_A]: dposition[0] -= dt * move_speed if key_state[glfw.KEY_D]: dposition[0] += dt * move_speed if key_state[glfw.KEY_Q]: dposition[1] += dt * move_speed if key_state[glfw.KEY_Z]: dposition[1] -= dt * move_speed theta = 0.0 if key_state[glfw.KEY_LEFT]: theta -= dt * turn_speed if key_state[glfw.KEY_RIGHT]: theta += dt * turn_speed if theta: rotation[0,0] = np.cos(theta) rotation[2,2] = rotation[0,0] rotation[0,2] = np.sin(theta) rotation[2,0] = -rotation[0,2] camera_rotation[...] = rotation.dot(camera_world_matrix[:3,:3]) if dposition.any(): camera_position[:] += camera_rotation.T.dot(dposition) return process_input
def add_mouse_button_callback(self, callback): if not hasattr(self, '_mouse_button_callbacks'): self._mouse_button_callbacks = [] glfw.SetMouseButtonCallback(self._window, self.__mouse_button_callback__) self._mouse_button_callbacks.append(callback)
def view_gltf(gltf, uri_path, scene_name=None, openvr=False, window_size=None): if scene_name is None: scene_name = gltf['scene'] if window_size is None: window_size = [800, 600] window = setup_glfw(width=window_size[0], height=window_size[1], double_buffered=not openvr) def on_resize(window, width, height): window_size[0], window_size[1] = width, height glfw.SetWindowSizeCallback(window, on_resize) if openvr and OpenVRRenderer is not None: vr_renderer = OpenVRRenderer() # text_drawer = TextDrawer() gl.glClearColor(0.01, 0.01, 0.17, 1.0); shader_ids = gltfu.setup_shaders(gltf, uri_path) gltfu.setup_programs(gltf, shader_ids) gltfu.setup_textures(gltf, uri_path) gltfu.setup_buffers(gltf, uri_path) scene = gltf.scenes[scene_name] nodes = [gltf.nodes[n] for n in scene.nodes] for node in nodes: gltfu.update_world_matrices(node, gltf) camera_world_matrix = np.eye(4, dtype=np.float32) projection_matrix = np.array(matrix44.create_perspective_projection_matrix(np.rad2deg(55), window_size[0]/window_size[1], 0.1, 1000), dtype=np.float32) for node in nodes: if 'camera' in node: camera = gltf['cameras'][node['camera']] if 'perspective' in camera: perspective = camera['perspective'] projection_matrix = np.array(matrix44.create_perspective_projection_matrix(np.rad2deg(perspective['yfov']), perspective['aspectRatio'], perspective['znear'], perspective['zfar']), dtype=np.float32) elif 'orthographic' in camera: raise Exception('TODO') camera_world_matrix = node['world_matrix'] break camera_position = camera_world_matrix[3, :3] camera_rotation = camera_world_matrix[:3, :3] dposition = np.zeros(3, dtype=np.float32) rotation = np.eye(3, dtype=np.float32) key_state = defaultdict(bool) def on_keydown(window, key, scancode, action, mods): if key == glfw.KEY_ESCAPE and action == glfw.PRESS: glfw.SetWindowShouldClose(window, gl.GL_TRUE) elif action == glfw.PRESS: key_state[key] = True elif action == glfw.RELEASE: key_state[key] = False glfw.SetKeyCallback(window, on_keydown) def on_mousedown(window, button, action, mods): pass glfw.SetMouseButtonCallback(window, on_mousedown) move_speed = 2.0 turn_speed = 0.5 def process_input(dt): glfw.PollEvents() dposition[:] = 0.0 if key_state[glfw.KEY_W]: dposition[2] -= dt * move_speed if key_state[glfw.KEY_S]: dposition[2] += dt * move_speed if key_state[glfw.KEY_A]: dposition[0] -= dt * move_speed if key_state[glfw.KEY_D]: dposition[0] += dt * move_speed if key_state[glfw.KEY_Q]: dposition[1] += dt * move_speed if key_state[glfw.KEY_Z]: dposition[1] -= dt * move_speed theta = 0.0 if key_state[glfw.KEY_LEFT]: theta -= dt * turn_speed if key_state[glfw.KEY_RIGHT]: theta += dt * turn_speed rotation[0,0] = np.cos(theta) rotation[2,2] = rotation[0,0] rotation[0,2] = np.sin(theta) rotation[2,0] = -rotation[0,2] camera_rotation[...] = rotation.dot(camera_world_matrix[:3,:3]) camera_position[:] += camera_rotation.T.dot(dposition) # sort nodes from front to back to avoid overdraw (assuming opaque objects): nodes = sorted(nodes, key=lambda node: np.linalg.norm(camera_position - node['world_matrix'][3, :3])) _logger.info('starting render loop...') sys.stdout.flush() gltfu.num_draw_calls = 0 nframes = 0 lt = glfw.GetTime() dt_max = 0.0 while not glfw.WindowShouldClose(window): t = glfw.GetTime() dt = t - lt dt_max = max(dt, dt_max) lt = t process_input(dt) if openvr: vr_renderer.process_input() vr_renderer.render(gltf, nodes, window_size) else: gl.glViewport(0, 0, window_size[0], window_size[1]) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) view_matrix = np.linalg.inv(camera_world_matrix) gltfu.set_material_state.current_material = None gltfu.set_technique_state.current_technique = None for node in nodes: gltfu.draw_node(node, gltf, projection_matrix=projection_matrix, view_matrix=view_matrix) # text_drawer.draw_text("%f" % dt, color=(1.0, 1.0, 0.0, 0.0), # view_matrix=view_matrix, # projection_matrix=projection_matrix) if nframes == 0: _logger.info("num draw calls per frame: %d", gltfu.num_draw_calls) sys.stdout.flush() gltfu.num_draw_calls = 0 st = glfw.GetTime() nframes += 1 glfw.SwapBuffers(window) _logger.info('FPS (avg): %f', ((nframes - 1) / (t - st))) _logger.info('MAX FRAME RENDER TIME: %f', dt_max) sys.stdout.flush() if openvr: vr_renderer.shutdown() glfw.DestroyWindow(window) glfw.Terminate()
def __init__(self, window_width, window_height, samples=1, window_title="", monitor=1, show_at_center=True, offscreen=False): self.window_title = window_title assert glfw.Init(), "Glfw Init failed!" glfw.WindowHint(glfw.SAMPLES, samples) if offscreen: glfw.WindowHint(glfw.VISIBLE, False) mon = glfw.GetMonitors()[monitor] if monitor != None else None self.windowID = glfw.CreateWindow(window_width, window_height, self.window_title, mon) assert self.windowID, "Could not create Window!" glfw.MakeContextCurrent(self.windowID) if not glInitBindlessTextureNV(): raise RuntimeError("Bindless Textures not supported") self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize( self.windowID) self.framebuffer_size_callback = [] def framebuffer_size_callback(window, w, h): self.framebuf_width, self.framebuf_height = w, h for callback in self.framebuffer_size_callback: callback(w, h) glfw.SetFramebufferSizeCallback(self.windowID, framebuffer_size_callback) self.key_callback = [] def key_callback(window, key, scancode, action, mode): if action == glfw.PRESS: if key == glfw.KEY_ESCAPE: glfw.SetWindowShouldClose(window, True) for callback in self.key_callback: callback(key, scancode, action, mode) glfw.SetKeyCallback(self.windowID, key_callback) self.mouse_callback = [] def mouse_callback(window, xpos, ypos): for callback in self.mouse_callback: callback(xpos, ypos) glfw.SetCursorPosCallback(self.windowID, mouse_callback) self.mouse_button_callback = [] def mouse_button_callback(window, button, action, mods): for callback in self.mouse_button_callback: callback(button, action, mods) glfw.SetMouseButtonCallback(self.windowID, mouse_button_callback) self.scroll_callback = [] def scroll_callback(window, xoffset, yoffset): for callback in self.scroll_callback: callback(xoffset, yoffset) glfw.SetScrollCallback(self.windowID, scroll_callback) self.previous_second = glfw.GetTime() self.frame_count = 0.0 if show_at_center: monitors = glfw.GetMonitors() assert monitor >= 0 and monitor < len( monitors), "Invalid monitor selected." vidMode = glfw.GetVideoMode(monitors[monitor]) glfw.SetWindowPos( self.windowID, vidMode.width / 2 - self.framebuf_width / 2, vidMode.height / 2 - self.framebuf_height / 2, )
def mbtn_callback(self, value): self._mbtn_callback = value if self._win: glfw.SetMouseButtonCallback(self._win, self._mbtn_callback)
def __init__(self, width, height, title): glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 4) glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 1) glfw.WindowHint(glfw.OPENGL_FORWARD_COMPAT, 1) glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.WindowHint(glfw.RESIZABLE, True) glfw.WindowHint(glfw.RED_BITS, 8) glfw.WindowHint(glfw.GREEN_BITS, 8) glfw.WindowHint(glfw.BLUE_BITS, 8) # glfw.WindowHint(glfw.ALPHA_BITS, 8) glfw.WindowHint(glfw.DEPTH_BITS, 24) glfw.WindowHint(glfw.STENCIL_BITS, 8) self.window = glfw.CreateWindow(width, height, title) self.framebufferSize = (width, height) f = 240 pos = np.array([3, 3, 3]) dir = np.array([-1, -1, -1]) up = np.array([0, 0, 1]) near = 0.2 far = 100 self.playerCamera = PlayerCamera(f, self.framebufferSize, pos, dir, up, near, far) self.currentCamera = self.playerCamera self.cameraLocked = True self.mainRender = None self.cairoSavePath = None 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 # # print( # "keybrd: key=%s scancode=%s action=%s mods=%s" % # (key, scancode, action, mods)) def char_callback(window, char): pass # print("unichr: char=%s" % char) def scroll_callback(window, off_x, off_y): pass # print("scroll: x=%s y=%s" % (off_x, off_y)) def mouse_button_callback(window, button, action, mods): pass # print("button: button=%s action=%s mods=%s" % (button, action, mods)) def cursor_enter_callback(window, status): pass # print("cursor: status=%s" % status) def cursor_pos_callback(window, pos_x, pos_y): pass # print("curpos: x=%s y=%s" % (pos_x, pos_y)) def window_size_callback(window, wsz_w, wsz_h): pass # print("window: w=%s h=%s" % (wsz_w, wsz_h)) def window_pos_callback(window, pos_x, pos_y): pass # print("window: x=%s y=%s" % (pos_x, pos_y)) def window_close_callback(window): pass # print("should: %s" % self.should_close) def window_refresh_callback(window): pass # print("redraw") def window_focus_callback(window, status): pass # print("active: status=%s" % status) def window_iconify_callback(window, status): pass # print("hidden: status=%s" % status) def framebuffer_size_callback(window, fbs_x, fbs_y): print("buffer: x=%s y=%s" % (fbs_x, fbs_y)) self.framebufferSize = (fbs_x, fbs_y) self.playerCamera.framebufferSize = (fbs_x, fbs_y) glViewport(0, 0, self.currentCamera.framebufferSize[0], self.currentCamera.framebufferSize[1]) pass glfw.SetKeyCallback(self.window, key_callback) glfw.SetCharCallback(self.window, char_callback) glfw.SetScrollCallback(self.window, scroll_callback) glfw.SetMouseButtonCallback(self.window, mouse_button_callback) glfw.SetCursorEnterCallback(self.window, cursor_enter_callback) glfw.SetCursorPosCallback(self.window, cursor_pos_callback) glfw.SetWindowSizeCallback(self.window, window_size_callback) glfw.SetWindowPosCallback(self.window, window_pos_callback) glfw.SetWindowCloseCallback(self.window, window_close_callback) glfw.SetWindowRefreshCallback(self.window, window_refresh_callback) glfw.SetWindowFocusCallback(self.window, window_focus_callback) glfw.SetWindowIconifyCallback(self.window, window_iconify_callback) glfw.SetFramebufferSizeCallback(self.window, framebuffer_size_callback)