nalgebra/examples/screen_to_view_coords.rs

25 lines
896 B
Rust
Raw Normal View History

2017-02-13 01:17:09 +08:00
#![allow(unused_variables)]
extern crate nalgebra as na;
2018-02-02 19:26:35 +08:00
use na::{Perspective3, Point2, Point3, Unit};
2021-08-16 00:12:17 +08:00
use std::f32::consts;
2017-02-13 01:17:09 +08:00
fn main() {
2021-08-16 00:12:17 +08:00
let projection = Perspective3::new(800.0 / 600.0, consts::PI / 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);
}