def mouse_drag_orbit_controls(self, point, d_point, buttons, modifiers): # Left click drag. if buttons == 1: # Translate to target the origin and rotate around the z axis. self.camera.model_matrix = (opengl.rotation_matrix( z=-d_point[0]) @ opengl.translation_matrix( *-self.camera_target) @ self.camera.model_matrix) # Rotation off of the z axis. camera_position = self.camera.get_position() camera_y_axis = self.camera.model_matrix[:3, 1] axis_of_rotation = space_ops.normalize( np.cross(camera_y_axis, camera_position)) rotation_matrix = space_ops.rotation_matrix(d_point[1], axis_of_rotation, homogeneous=True) maximum_polar_angle = PI / 2 minimum_polar_angle = -PI / 2 potential_camera_model_matrix = rotation_matrix @ self.camera.model_matrix potential_camera_location = potential_camera_model_matrix[:3, 3] potential_camera_y_axis = potential_camera_model_matrix[:3, 1] sign = (np.sign(potential_camera_y_axis[2]) if potential_camera_y_axis[2] != 0 else 1) potential_polar_angle = sign * np.arccos( potential_camera_location[2] / np.linalg.norm(potential_camera_location)) if minimum_polar_angle <= potential_polar_angle <= maximum_polar_angle: self.camera.model_matrix = potential_camera_model_matrix else: sign = np.sign( camera_y_axis[2]) if camera_y_axis[2] != 0 else 1 current_polar_angle = sign * np.arccos( camera_position[2] / np.linalg.norm(camera_position)) if potential_polar_angle > maximum_polar_angle: polar_angle_delta = maximum_polar_angle - current_polar_angle else: polar_angle_delta = minimum_polar_angle - current_polar_angle rotation_matrix = space_ops.rotation_matrix(polar_angle_delta, axis_of_rotation, homogeneous=True) self.camera.model_matrix = rotation_matrix @ self.camera.model_matrix # Translate to target the original target. self.camera.model_matrix = (opengl.translation_matrix( *self.camera_target) @ self.camera.model_matrix) # Right click drag. elif buttons == 4: camera_x_axis = self.camera.model_matrix[:3, 0] horizontal_shift_vector = -d_point[0] * camera_x_axis vertical_shift_vector = -d_point[1] * np.cross(OUT, camera_x_axis) total_shift_vector = horizontal_shift_vector + vertical_shift_vector self.camera.model_matrix = (opengl.translation_matrix( *total_shift_vector) @ self.camera.model_matrix) self.camera_target += total_shift_vector
def mouse_scroll_orbit_controls(self, point, offset): camera_to_target = self.camera_target - self.camera.get_position() camera_to_target *= np.sign(offset[1]) shift_vector = 0.01 * camera_to_target self.camera.model_matrix = ( opengl.translation_matrix(*shift_vector) @ self.camera.model_matrix )