Work on back projection, f32 -> f64

This commit is contained in:
Thomas Forgione 2018-07-23 10:12:37 +02:00
parent 674b49430f
commit 6807773b38
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
11 changed files with 89 additions and 109 deletions

View File

@ -12,13 +12,13 @@ use math::frustum::Frustum;
pub trait RenderCamera {
/// Returns the view matrix of the camera.
fn view(&self) -> Matrix4<f32>;
fn view(&self) -> Matrix4<f64>;
/// Returns the perspective matrix of the camera.
fn perspective(&self) -> Matrix4<f32>;
fn perspective(&self) -> Matrix4<f64>;
/// Returns the product of the perspective matrix and the view matrix.
fn model_view(&self) -> Matrix4<f32> {
fn model_view(&self) -> Matrix4<f64> {
self.perspective() * self.view()
}
@ -30,7 +30,7 @@ pub trait RenderCamera {
}
/// Creates the pose matrix, inverse of the look at matrix.
pub fn pose(position: Vector3<f32>, target: Vector3<f32>, up: Vector3<f32>) -> Matrix4<f32> {
pub fn pose(position: Vector3<f64>, target: Vector3<f64>, up: Vector3<f64>) -> Matrix4<f64> {
// This is the right way to do things
let e3 = (position - target).normalized();
@ -47,12 +47,12 @@ pub fn pose(position: Vector3<f32>, target: Vector3<f32>, up: Vector3<f32>) -> M
}
/// Creates a look at matrix from the center, the target pointed by the camera and the up vector.
pub fn look_at(position: Vector3<f32>, target: Vector3<f32>, up: Vector3<f32>) -> Matrix4<f32> {
pub fn look_at(position: Vector3<f64>, target: Vector3<f64>, up: Vector3<f64>) -> Matrix4<f64> {
pose(position, target, up).try_inverse().unwrap()
}
/// Creates a perspective matrix of a camera.
pub fn perspective(fov: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Matrix4<f32> {
pub fn perspective(fov: f64, aspect_ratio: f64, z_near: f64, z_far: f64) -> Matrix4<f64> {
let top = z_near * (fov / 2.0).tan();
let height = 2.0 * top;
@ -74,7 +74,7 @@ pub fn perspective(fov: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Matr
}
/// Returns the inverse of the perspective matrix.
pub fn perspective_inverse(fov: f32, aspect_ratio: f32, z_near: f32, z_far: f32) -> Matrix4<f32> {
pub fn perspective_inverse(fov: f64, aspect_ratio: f64, z_near: f64, z_far: f64) -> Matrix4<f64> {
let top = z_near * (fov / 2.0).tan();
let height = 2.0 * top;
@ -99,32 +99,32 @@ pub fn perspective_inverse(fov: f32, aspect_ratio: f32, z_near: f32, z_far: f32)
#[derive(Clone, Debug)]
pub struct Camera {
/// The position of the center of the camera.
pub position: Vector3<f32>,
pub position: Vector3<f64>,
/// The 3D point the camera is targetting.
pub target: Vector3<f32>,
pub target: Vector3<f64>,
/// The up vector of the camera.
pub up: Vector3<f32>,
pub up: Vector3<f64>,
/// The field of view of the camera.
pub fov: f32,
pub fov: f64,
/// The minimum depth for visible things.
pub z_near: f32,
pub z_near: f64,
/// The maximum depth for visible things.
pub z_far: f32,
pub z_far: f64,
/// The aspect ratio of the camera.
pub aspect_ratio: f32,
pub aspect_ratio: f64,
}
impl Camera {
/// Creates a new camera from its attributes.
pub fn new(position: Vector3<f32>, target: Vector3<f32>, up: Vector3<f32>) -> Camera {
use std::f32::consts::PI;
pub fn new(position: Vector3<f64>, target: Vector3<f64>, up: Vector3<f64>) -> Camera {
use std::f64::consts::PI;
Camera {
position: position,
@ -138,22 +138,22 @@ impl Camera {
}
/// Returns the pose matrix of the camera.
pub fn pose(&self) -> Matrix4<f32> {
pub fn pose(&self) -> Matrix4<f64> {
pose(self.position, self.target, self.up)
}
/// Returns the view matrix of the camera, inverse of the pose.
pub fn view(&self) -> Matrix4<f32> {
pub fn view(&self) -> Matrix4<f64> {
look_at(self.position, self.target, self.up)
}
/// Returns the perspective matrix of the camera.
pub fn perspective(&self) -> Matrix4<f32> {
pub fn perspective(&self) -> Matrix4<f64> {
perspective(self.fov, self.aspect_ratio, self.z_near, self.z_far)
}
/// Returns the inverse of the perspective matrix of the camera.
pub fn perspective_inverse(&self) -> Matrix4<f32> {
pub fn perspective_inverse(&self) -> Matrix4<f64> {
perspective_inverse(self.fov, self.aspect_ratio, self.z_near, self.z_far)
}
@ -165,7 +165,7 @@ impl Camera {
/// Unprojects a 2D point (x, y) in the 3D world.
///
/// The coordinates must be in [-1.0, 1.0]
pub fn unproject(&self, point: Vector2<f32>) -> Vector3<f32> {
pub fn unproject(&self, point: Vector2<f64>) -> Vector3<f64> {
let point = Vector4::new(point[0], point[1], 0.5, 1.0);
let v = self.pose() * self.perspective_inverse() * point;
@ -174,11 +174,11 @@ impl Camera {
}
impl RenderCamera for Camera {
fn view(&self) -> Matrix4<f32> {
fn view(&self) -> Matrix4<f64> {
self.view()
}
fn perspective(&self) -> Matrix4<f32> {
fn perspective(&self) -> Matrix4<f64> {
self.perspective()
}
}

View File

@ -1,6 +1,6 @@
//! This models contains structs that help move the camera in a user-friendly way.
const EPSILON: f32 = 0.001;
const EPSILON: f64 = 0.001;
use glium::glutin::{
Event,
@ -33,31 +33,31 @@ pub trait Controls {
/// Only object centered are supported.
pub struct OrbitControls {
/// The last position of the mouse.
mouse_position: Vector2<f32>,
mouse_position: Vector2<f64>,
/// Wether the left click of the mouse is pressed or not.
pressed: bool,
/// The theta angle of the position of the camera in spheric coordinates.
theta: f32,
theta: f64,
/// The phi angle of the position of the camera in spheric coordinates.
phi: f32,
phi: f64,
/// The distance between the camera and the origin.
distance: f32,
distance: f64,
/// The sensitiviy of the rotation of the mouse.
sensitivity: f32,
sensitivity: f64,
/// The center of the object.
center: Vector3<f32>,
center: Vector3<f64>,
}
impl OrbitControls {
/// Creates a new orbit controls, and initializes the camera.
pub fn new(center: Vector3<f32>, distance: f32, camera: &mut Camera) -> OrbitControls {
pub fn new(center: Vector3<f64>, distance: f64, camera: &mut Camera) -> OrbitControls {
let controls = OrbitControls {
mouse_position: Vector2::new(0.0, 0.0),
pressed: false,
@ -85,8 +85,8 @@ impl OrbitControls {
let distance = (bounding_box.max() - bounding_box.min()).norm();
OrbitControls::new(
Vector3::new(center.x() as f32, center.y() as f32, center.z() as f32),
distance as f32,
Vector3::new(center.x() as f64, center.y() as f64, center.z() as f64),
distance as f64,
camera
)
@ -109,7 +109,7 @@ impl Controls for OrbitControls {
Event::WindowEvent {
event: WindowEvent::Resized(width, height), ..
} => {
camera.aspect_ratio = width as f32 / height as f32;
camera.aspect_ratio = width as f64 / height as f64;
},
Event::WindowEvent {
@ -117,7 +117,7 @@ impl Controls for OrbitControls {
delta: MouseScrollDelta::LineDelta(_, y), ..
}, ..
} => {
self.distance -= y / self.sensitivity;
self.distance -= y as f64 / self.sensitivity;
*camera.position.x_mut() = self.distance * self.phi.cos() * self.theta.cos();
*camera.position.y_mut() = self.distance * self.phi.sin();
@ -132,7 +132,7 @@ impl Controls for OrbitControls {
position: (x, y), ..
}, ..
} => {
let current_position = Vector2::new(x as f32, y as f32);
let current_position = Vector2::new(x as f64, y as f64);
if self.pressed {
let difference = (current_position - self.mouse_position) / self.sensitivity;
@ -140,7 +140,7 @@ impl Controls for OrbitControls {
self.theta += difference.x();
self.phi += difference.y();
use std::f32::consts::PI;
use std::f64::consts::PI;
self.phi = self.phi.max(- PI/2.0 + EPSILON);
self.phi = self.phi.min( PI/2.0 - EPSILON);
@ -170,25 +170,25 @@ impl Controls for OrbitControls {
pub struct FirstPersonControls {
/// Theta angle of the spheric coordinates of the direction of the camera.
theta: f32,
theta: f64,
/// Phi angle of the spheric coordinates of the direction of the camera.
phi: f32,
phi: f64,
/// Current position of the camera.
position: Vector3<f32>,
position: Vector3<f64>,
/// Vector indicating the direction of the camera.
forward: Vector3<f32>,
forward: Vector3<f64>,
/// Vector indicating the left of the camera.
left: Vector3<f32>,
left: Vector3<f64>,
/// Speed of the camera.
speed: f32,
speed: f64,
/// Sensitivity of the mouse.
sensitivity: f32,
sensitivity: f64,
/// Wether the forward button is pressed or not.
forward_pressed: bool,
@ -241,7 +241,7 @@ impl Controls for FirstPersonControls {
Event::WindowEvent {
event: WindowEvent::Resized(width, height), ..
} => {
camera.aspect_ratio = width as f32 / height as f32;
camera.aspect_ratio = width as f64 / height as f64;
},
// On Z pressed
@ -307,14 +307,14 @@ impl Controls for FirstPersonControls {
} => {
let size = renderer.gl_window().window().get_inner_size().unwrap();
let center = Vector2::new(size.0 as f32 / 2.0, size.1 as f32 / 2.0);
let current_position = Vector2::new(x as f32, y as f32);
let center = Vector2::new(size.0 as f64 / 2.0, size.1 as f64 / 2.0);
let current_position = Vector2::new(x as f64, y as f64);
let difference = (current_position - center) / self.sensitivity;
self.theta += difference.x();
self.phi -= difference.y();
use std::f32::consts::PI;
use std::f64::consts::PI;
self.phi = self.phi.max(- PI/2.0 + EPSILON);
self.phi = self.phi.min( PI/2.0 - EPSILON);

View File

@ -126,10 +126,10 @@ macro_rules! impl_center {
}
impl_center!(BoundingBox2, Vector2, f32);
impl_center!(BoundingBox2, Vector2, f64);
impl_center!(BoundingBox3, Vector3, f32);
impl_center!(BoundingBox3, Vector3, f64);
impl_center!(BoundingBox4, Vector4, f32);
impl_center!(BoundingBox2, Vector2, f64);
impl_center!(BoundingBox3, Vector3, f64);
impl_center!(BoundingBox4, Vector4, f64);

View File

@ -26,7 +26,7 @@ impl Frustum {
}
/// Creates a frustum for a camera matrix.
pub fn from_matrix(m: &Matrix4<f32>) -> Frustum {
pub fn from_matrix(m: &Matrix4<f64>) -> Frustum {
// let m0 = m[(0, 0)]; let m1 = m[(0, 1)]; let m2 = m[(0, 2)]; let m3 = m[(0, 3)];
// let m4 = m[(1, 0)]; let m5 = m[(1, 1)]; let m6 = m[(1, 2)]; let m7 = m[(1, 3)];
@ -52,11 +52,11 @@ impl Frustum {
}
/// Returns true if the intersection of the frustum and the bounding box is not empty.
pub fn intersects_box(&self, bbox: BoundingBox3<f32>) -> bool {
pub fn intersects_box(&self, bbox: BoundingBox3<f64>) -> bool {
use num::Zero;
let mut p = Vector3::<f32>::zero();
let mut p = Vector3::<f64>::zero();
for plane in &self.planes {

View File

@ -6,15 +6,15 @@ use math::vector::Vector3;
#[derive(Copy, Clone)]
pub struct Plane {
/// The normal of the plane.
normal: Vector3<f32>,
normal: Vector3<f64>,
/// The constant, offset of the plane from the origin.
constant: f32,
constant: f64,
}
impl Plane {
/// Creates a new plane from its normal and its constant.
pub fn from_coordinates(a: f32, b: f32, c: f32, w: f32) -> Plane {
pub fn from_coordinates(a: f64, b: f64, c: f64, w: f64) -> Plane {
let mut p = Plane {
normal: Vector3::new(a, b, c),
constant: w,
@ -24,18 +24,18 @@ impl Plane {
}
/// Creates a new plane from its normal and its constant.
pub fn from_normal_and_constant(normal: Vector3<f32>, constant: f32) -> Plane {
pub fn from_normal_and_constant(normal: Vector3<f64>, constant: f64) -> Plane {
Plane::from_coordinates(normal.x(), normal.y(), normal.z(), constant)
}
/// Creates a new plane from its normal and a point of the plane.
pub fn from_normal_and_point(normal: Vector3<f32>, point: Vector3<f32>) -> Plane {
pub fn from_normal_and_point(normal: Vector3<f64>, point: Vector3<f64>) -> Plane {
Plane::from_normal_and_constant(normal, - point.dot(normal))
}
/// Creates a new plane from three points.
pub fn from_points(p1: Vector3<f32>, p2: Vector3<f32>, p3: Vector3<f32>) -> Plane {
pub fn from_points(p1: Vector3<f64>, p2: Vector3<f64>, p3: Vector3<f64>) -> Plane {
let p1p2 = p2 - p1;
let p1p3 = p3 - p1;
Plane::from_normal_and_point(p1p2.cross_product(p1p3), p1)
@ -49,17 +49,17 @@ impl Plane {
}
/// Returns the normal of the plane.
pub fn normal(&self) -> Vector3<f32> {
pub fn normal(&self) -> Vector3<f64> {
self.normal
}
/// Returns the constant of the plane.
pub fn constant(&self) -> f32 {
pub fn constant(&self) -> f64 {
self.constant
}
/// Returns the distance between the plane and the point.
pub fn distance_to_point(&self, point: Vector3<f32>) -> f32 {
pub fn distance_to_point(&self, point: Vector3<f64>) -> f64 {
self.normal.dot(point) + self.constant
}
}

View File

@ -247,9 +247,9 @@ make_vector!(Vector2, 2, (T, T), (x, x_mut, 0), (y, y_mut, 1));
make_vector!(Vector3, 3, (T, T, T), (x, x_mut, 0), (y, y_mut, 1), (z, z_mut, 2));
make_vector!(Vector4, 4, (T, T, T, T), (x, x_mut, 0), (y, y_mut, 1), (z, z_mut, 2), (t, t_mut, 3));
impl Vector2<f32> {
impl Vector2<f64> {
/// Returns a orthogonal vector to the one passed as parameter.
pub fn orthogonal(&self) -> Vector2<f32> {
pub fn orthogonal(&self) -> Vector2<f64> {
Vector2::new(
self.y(),
self.x() * -1.0,
@ -291,10 +291,10 @@ impl Vector3<f64> {
}
type Vector2f32 = Vector2<f32>; implement_vertex!(Vector2f32, data);
type Vector2f64 = Vector2<f64>; implement_vertex!(Vector2f64, data);
type Vector3f32 = Vector3<f32>; implement_vertex!(Vector3f32, data);
type Vector3f64 = Vector3<f64>; implement_vertex!(Vector3f64, data);
type Vector4f32 = Vector4<f32>; implement_vertex!(Vector4f32, data);
type Vector2f64 = Vector2<f64>; implement_vertex!(Vector2f64, data);
type Vector3f64 = Vector3<f64>; implement_vertex!(Vector3f64, data);
type Vector4f64 = Vector4<f64>; implement_vertex!(Vector4f64, data);
#[cfg(test)]

View File

@ -11,10 +11,10 @@ pub struct Material {
pub name: String,
/// The diffuse color of the material.
pub diffuse: Vector3<f32>,
pub diffuse: Vector3<f64>,
/// Map linking each texture map to its file path.
pub textures: HashMap<String, (String, Vector3<f32>)>,
pub textures: HashMap<String, (String, Vector3<f64>)>,
/// Instructions that are unknown.
///

View File

@ -47,10 +47,10 @@ pub enum Element {
/// First string is the name of the map.
/// Second string is the path to the image file.
/// Vector3 is the size of the texture.
Texture(String, String, Vector3<f32>),
Texture(String, String, Vector3<f64>),
/// Change the main color of the current material.
Diffuse(Vector3<f32>),
Diffuse(Vector3<f64>),
/// An unknown material instruction that will be copied into the mtl file.
UnknownMaterialInstruction(String),

View File

@ -46,9 +46,9 @@ impl LineParser for MtlParser {
"Kd" => {
let values = parse_values(line_number, line, path, 3)?;
Ok(Element::Diffuse(Vector3::new(
values[0] as f32,
values[1] as f32,
values[2] as f32,
values[0] as f64,
values[1] as f64,
values[2] as f64,
)))
},
@ -63,9 +63,9 @@ impl LineParser for MtlParser {
Ok(Element::Texture(first.to_owned(), full_path.to_str().unwrap().to_owned(), Vector3::new(1.0, 1.0, 1.0)))
} else if split[0] == "-s" {
let size = Vector3::new(
if let Ok(f) = split[1].parse::<f32>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[1].to_owned())); },
if let Ok(f) = split[2].parse::<f32>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[2].to_owned())); },
if let Ok(f) = split[3].parse::<f32>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[3].to_owned())); },
if let Ok(f) = split[1].parse::<f64>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[1].to_owned())); },
if let Ok(f) = split[2].parse::<f64>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[2].to_owned())); },
if let Ok(f) = split[3].parse::<f64>() { f } else { return Err(ParserError::ParseNumberError(path.to_owned(), line_number, split[3].to_owned())); },
);
let mut full_path = root.clone();
full_path.push(split[4].to_owned());

View File

@ -3,7 +3,6 @@ extern crate glium;
#[macro_use]
extern crate verbose_log;
extern crate model_converter;
extern crate nalgebra as na;
use std::process::exit;
use std::time::{Instant, Duration};
@ -82,10 +81,6 @@ fn main() {
}
}
let center = (bbox.min() + bbox.max()) / 2.0;
let center = Vector3::new(center.x() as f32, center.y() as f32, center.z() as f32);
let size = (bbox.max() - bbox.min()).norm() as f32;
let mut events_loop = EventsLoop::new();
let window = WindowBuilder::new().with_visibility(false);
let context = glutin::ContextBuilder::new().with_depth_buffer(24);
@ -98,7 +93,7 @@ fn main() {
for (name, mut model) in models {
log!("Scaling model {}...", name);
// model.center_and_scale_from_box(&bbox);
model.center_and_scale_from_box(&bbox);
log!("\nBuilding textures for model {}...", name);
@ -120,21 +115,19 @@ fn main() {
let mut closed = false;
let mut camera = Camera::new(
Vector3::new(1.0, 0.0, 0.0),
Vector3::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
Vector3::new( 0.0, 0.0, 0.0),
Vector3::new( 0.0, 0.0, 0.0),
Vector3::new( 0.0, 1.0, 0.0),
);
camera.z_near = 0.0001;
use model_converter::camera::RenderCamera;
let mut controls: Box<Controls> = if matches.is_present("first person") {
Box::new(FirstPersonControls::new())
} else {
Box::new(OrbitControls::new(
Vector3::new(0.0, 0.0, 0.0),
2.0,
1.0,
&mut camera
))
};
@ -176,33 +169,20 @@ fn main() {
}, ..
}, ..
} => {
// Go back in world coordinates
let position = camera.position;
// let position = position + center;
let target = camera.target;
// let target = target + center;
let up = camera.up;
println!("Camera:");
println!("\tPosition: ({}, {}, {})",
position.x(), position.y(), position.z());
camera.position.x(), camera.position.y(), camera.position.z());
println!("\tTarget: ({}, {}, {})",
target.x(), target.y(), target.z());
camera.target.x(), camera.target.y(), camera.target.z());
println!("\tUp: ({}, {}, {})",
up.x(), up.y(), up.z());
camera.up.x(), camera.up.y(), camera.up.z());
}
_ => (),
}
});
controls.update(&mut camera, &renderer);
renderer.render(&scene, &camera);
let elapsed = as_millis(Instant::now().duration_since(before));

View File

@ -162,11 +162,11 @@ impl Renderer {
NoIndices(PrimitiveType::TrianglesList),
&self.program,
&uniform!(
diffuse: Into::<[f32; 3]>::into(diffuse),
diffuse: Into::<[f64; 3]>::into(diffuse),
tex: texture,
perspective: Into::<[[f32; 4]; 4]>::into(perspective),
view: Into::<[[f32; 4]; 4]>::into(view),
texture_size: Into::<[f32; 3]>::into(size),
perspective: Into::<[[f64; 4]; 4]>::into(perspective),
view: Into::<[[f64; 4]; 4]>::into(view),
texture_size: Into::<[f64; 3]>::into(size),
),
&params,
).unwrap();
@ -178,7 +178,7 @@ impl Renderer {
}
/// Renders a part of a model.
fn get_texture_of_part<'a>(&self, model: &'a Model, part: &Part) -> Option<(&'a SrgbTexture2d, Vector3<f32>)> {
fn get_texture_of_part<'a>(&self, model: &'a Model, part: &Part) -> Option<(&'a SrgbTexture2d, Vector3<f64>)> {
if let Some(ref material_name) = part.material_name {
if let Some(ref material) = model.materials.get(material_name) {
if let Some((texture, size)) = material.textures.get("map_Kd") {