A function that create a ray from a perspective camera
| use cgmath::{Point2, Point3, Vector3, Vector4, Matrix4}; | |
| use cgmath::{SquareMatrix, InnerSpace}; | |
| use collision::{Ray, Ray3}; | |
| // http://antongerdelan.net/opengl/raycasting.html | |
| fn ray_from_cam(proj: Matrix4<f32>, view: Matrix4<f32>, point: Point2<f32>) -> Ray3<f32> { | |
| assert!(point.x >= -1.0 && point.x <= 1.0); | |
| assert!(point.y >= -1.0 && point.y <= 1.0); | |
| // 4d Homogeneous Clip Coordinates | |
| let ray_clip = Vector4::new(point.x, point.y, -1.0, 1.0); | |
| // 4d Eye (Camera) Coordinates | |
| let ray_eye = proj.invert().unwrap() * ray_clip; | |
| let ray_eye = Vector4::new(ray_eye.x, ray_eye.y, -1.0, 0.0); | |
| // 4d World Coordinates | |
| let ray_world = (view.invert().unwrap() * ray_eye).truncate(); // into Vector3 | |
| // don't forget to normalise the vector at some point | |
| let ray_world = ray_world.normalize(); | |
| Ray::new(Point3::new(0.0, 0.0, 0.0), ray_world) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment