Created
January 10, 2020 21:34
-
-
Save LaylBongers/cd7b81d2d4e462dbd6857444649958a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn on_pointer(&mut self, state: &mut ApplicationState, position: Point2<f32>) { | |
let projection_matrix = create_projection_matrix(self.last_size); | |
let projection_inverse = projection_matrix.try_inverse().unwrap(); | |
// Transform the window position to a 3D ray from the camera | |
let ray_clip = Vector3::new( | |
(2.0 * position.x) / self.last_size.x as f32 - 1.0, | |
(2.0 * position.y) / self.last_size.y as f32 - 1.0, | |
// Remember, inverse Z | |
1.0, | |
); | |
let mut ray_eye = projection_inverse.transform_vector(&ray_clip); | |
ray_eye.z = -1.0; | |
let ray_world = self.last_camera_model_matrix.transform_vector(&ray_eye); | |
ray_world.normalize(); | |
// Ray to plane test | |
let plane_origin = Point3::new(-2.0, 0.0, 0.0); | |
let plane_normal = Vector3::new(-1.0, 0.0, 0.0); // Pointing away from ray | |
let denom = plane_normal.dot(&ray_world); | |
if denom < 1e-6 { | |
// Ray is parallel with the plane or pointing away from it | |
return; | |
} | |
let origin_difference = plane_origin - self.last_camera_origin; | |
let distance = origin_difference.dot(&plane_normal) / denom; | |
if distance < 0.0 { | |
// Ray is facing away from the plane | |
return; | |
} | |
// Get the point on the plane, and turn it into a 2D position | |
let plane_position_world = self.last_camera_origin + (ray_world * distance); | |
println!("{:?}", plane_position_world); | |
let plane_position = | |
Point2::new(8.0 - plane_position_world.z, 8.0 - plane_position_world.y) * 64.0; | |
// Record pointer draw positions, we get multiple input events per frame so it needs to be | |
// batched. | |
let stroke = state.pending_stroke.get_or_insert(Vec::new()); | |
stroke.push(plane_position); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment