nalgebra/examples/screen_to_view_coords.rs

24 lines
886 B
Rust
Raw Normal View History

2017-02-13 01:17:09 +08:00
#![allow(unused_variables)]
extern crate nalgebra as na;
2020-10-12 21:29:20 +08:00
use na::{OpenGL, Perspective3, Point2, Point3, Unit};
2017-02-13 01:17:09 +08:00
fn main() {
2020-10-12 21:29:20 +08:00
let projection = Perspective3::<OpenGL>::new(800.0 / 600.0, 3.14 / 2.0, 1.0, 1000.0);
2017-02-13 01:17:09 +08:00
let screen_point = Point2::new(10.0f32, 20.0);
// Compute two points in clip-space.
// "ndc" = normalized device coordinates.
let near_ndc_point = Point3::new(screen_point.x / 800.0, screen_point.y / 600.0, -1.0);
2018-02-02 19:26:35 +08:00
let far_ndc_point = Point3::new(screen_point.x / 800.0, screen_point.y / 600.0, 1.0);
2017-02-13 01:17:09 +08:00
// Unproject them to view-space.
let near_view_point = projection.unproject_point(&near_ndc_point);
2018-02-02 19:26:35 +08:00
let far_view_point = projection.unproject_point(&far_ndc_point);
2017-02-13 01:17:09 +08:00
// Compute the view-space line parameters.
2018-02-02 19:26:35 +08:00
let line_location = near_view_point;
2017-02-13 01:17:09 +08:00
let line_direction = Unit::new_normalize(far_view_point - near_view_point);
}