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 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 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
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 processLookInput(self, window, deltaTime): cursor = np.array(glfw.GetCursorPos(window), np.float32) if 'lastCursor' in dir(self): deltaMouse = cursor - self.lastCursor else: deltaMouse = np.zeros(2) self.lastCursor = cursor # windowWidth, windowHeight = glfw.GetWindowSize(window) # glfw.SetCursorPos(window, windowWidth / 2, windowHeight / 2) self.sphericalDir[1] -= float(deltaMouse[0]) * self.lookSpeed self.sphericalDir[2] -= float(deltaMouse[1]) * self.lookSpeed # self.sphericalDir[1:3] -= deltaMouse * self.lookSpeed self.sphericalDir[2] = min(max(-np.pi*0.5 * 0.9, self.sphericalDir[2]), np.pi*0.5 * 0.9)
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 _compute_matrices_from_input(self): old_focus = self._focus self._focus = glfw.GetWindowAttrib(self.window, glfw.FOCUSED) if old_focus != self._focus and self._focus: glfw.SetCursorPos(self.window, self._window_size[0] / 2, self._window_size[1] / 2) FoV = 45 mouse_speed = 0.001 speed = 5.0 # glfwGetTime is called only once, the first time this function is called if self._lastTime is None: self._lastTime = glfw.GetTime() currentTime = glfw.GetTime() if self._focus: if self._is_key_pressed(glfw.KEY_O): self._camera_position = self._org_camera_position.copy() self._horizontalAngle = self._org_angle[0] self._verticalAngle = self._org_angle[1] deltaTime = currentTime - self._lastTime #if deltaTime > 0.01: xpos, ypos = glfw.GetCursorPos(self.window) # Reset mouse position for next frame if xpos != self._window_size[0] / 2 or ypos != self._window_size[ 1] / 2: glfw.SetCursorPos(self.window, self._window_size[0] / 2, self._window_size[1] / 2) # Compute new orientation self._horizontalAngle += mouse_speed * float(self._window_size[0] / 2.0 - xpos) self._verticalAngle += mouse_speed * float(self._window_size[1] / 2.0 - ypos) # Direction : Spherical coordinates to Cartesian coordinates conversion direction = vec3( mathf.cos(self._verticalAngle) * mathf.sin(self._horizontalAngle), mathf.sin(self._verticalAngle), mathf.cos(self._verticalAngle) * mathf.cos(self._horizontalAngle)) # Right vector right = vec3(mathf.sin(self._horizontalAngle - 3.14 / 2.0), 0.0, mathf.cos(self._horizontalAngle - 3.14 / 2.0)) # Up vector up = vec3.cross(right, direction) if self._focus: # Move forward if self._is_key_pressed([glfw.KEY_W, glfw.KEY_UP]): self._camera_position += direction * deltaTime * speed # Move backward if self._is_key_pressed([glfw.KEY_S, glfw.KEY_DOWN]): self._camera_position -= direction * deltaTime * speed # Strafe right if self._is_key_pressed([glfw.KEY_D, glfw.KEY_RIGHT]): self._camera_position += right * deltaTime * speed # Strafe left if self._is_key_pressed([glfw.KEY_A, glfw.KEY_LEFT]): self._camera_position -= right * deltaTime * speed if self._is_key_pressed(glfw.KEY_C): new_press = glfw.GetTime() if new_press - self._last_key_pressed > 0.15: for object in self._render_objects: object.flip_collapse_mode() self._last_key_pressed = new_press if self._is_key_pressed(glfw.KEY_T): new_press = glfw.GetTime() if new_press - self._last_key_pressed > 0.15: for object in self._render_objects: object.flip_color_mode() self._last_key_pressed = new_press if self._is_key_pressed(glfw.KEY_U): new_press = glfw.GetTime() if new_press - self._last_key_pressed > 0.15: for object in self._render_objects: object.flip_unproject() self._last_key_pressed = new_press ProjectionMatrix = mat4.perspective( FoV, self._window_size[0] / float(self._window_size[1]), 0.1, 100.0) ViewMatrix = mat4.lookat(self._camera_position, self._camera_position + direction, up) vp = ProjectionMatrix * ViewMatrix glUniformMatrix4fv(self._mapping['V'], 1, GL_FALSE, ViewMatrix.data) self._lastTime = currentTime return vp