From ef6ce4bbde3a1397832a58fdbd0aa46a5f2e6bd4 Mon Sep 17 00:00:00 2001 From: bijan2005 Date: Mon, 30 Nov 2020 12:00:43 -0500 Subject: [PATCH] Refactor camera code to use aspect ratio --- src/camera.rs | 39 +++++++++++++++++---------------------- src/main.rs | 2 +- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 8d9e776..ad82583 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -19,22 +19,22 @@ pub struct Camera { impl Camera { // Constructs a new camera from a position and viewing direction. - pub fn new_(pos: Point3, dir: Vector3, up: Vector3, focal_length: f32, - canvas_x: f32, canvas_y: f32, image_x: u32, image_y: u32) -> Self { + pub fn new_(pos: Point3, dir: Vector3, up: Vector3, + focal_length: f32, aspect_ratio: f32, canvas_x: f32, image_x: u32) -> Self { let iso = Isometry3::face_towards(&pos, &(pos + dir), &up); Camera { matrix: iso, focal_length: focal_length, - canvas_size: Vector2::new(canvas_x, canvas_y), - image_size: Vector2::new(image_x, image_y) + canvas_size: Vector2::new(canvas_x, canvas_x * aspect_ratio), + image_size: Vector2::new(image_x, (image_x as f32 * aspect_ratio) as u32) } } // Constructs a new camera from a position and viewing direction // (assuming the camera is oriented upright). - pub fn new(pos: Point3, dir: Vector3, focal_length: f32, - canvas_x: f32, canvas_y: f32, image_x: u32, image_y: u32) -> Self - { Camera::new_(pos, dir, Vector3::y(), focal_length, canvas_x, canvas_y, image_x, image_y) } + pub fn new(pos: Point3, dir: Vector3, + focal_length: f32, aspect_ratio: f32, canvas_x: f32, image_x: u32) -> Self + { Camera::new_(pos, dir, Vector3::y(), focal_length, aspect_ratio, canvas_x, image_x) } pub fn pos(&self) -> Point3 { Point3::from(self.matrix.translation.vector) } @@ -70,9 +70,8 @@ mod tests { fn camera_pos() { let camera: Camera = Camera::new(Point3::new(-5.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0), - 1.0, - 2.0, 2.0, - 800, 800); + 1.0, 1.0, + 2.0, 800); assert_eq!(camera.pos(), Point3::new(-5.0, 0.0, 0.0)); } @@ -81,9 +80,8 @@ mod tests { fn camera_matrix1() { let camera: Camera = Camera::new(Point3::new(-5.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0), - 1.0, - 2.0, 2.0, - 800, 800); + 1.0, 1.0, + 2.0, 800); let point = Point3::new(0.0, 0.0, 4.0); let point = camera.matrix * point; @@ -95,9 +93,8 @@ mod tests { fn camera_matrix2() { let camera: Camera = Camera::new(Point3::new(-5.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0), - 1.0, - 2.0, 2.0, - 800, 800); + 1.0, 1.0, + 2.0, 800); let point = Point3::new(4.0, 0.0, 0.0); let point = camera.matrix * point; @@ -109,9 +106,8 @@ mod tests { fn camera_project1() { let camera: Camera = Camera::new(Point3::new(-5.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0), - 1.0, - 2.0, 2.0, - 800, 800); + 1.0, 1.0, + 2.0, 800); let point = camera.project(400, 400); let point = round(point); // round to avoid errors @@ -122,9 +118,8 @@ mod tests { fn camera_project2() { let camera: Camera = Camera::new(Point3::new(-5.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0), - 1.0, - 2.0, 2.0, - 800, 800); + 1.0, 1.0, + 2.0, 800); let point = camera.project(0, 0); let point = round(point); // round to avoid errors diff --git a/src/main.rs b/src/main.rs index 5a0036d..2ac682f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,7 @@ fn render(camera: &Camera, scene: &Scene, filename: &str) -> std::io::Result<()> fn main() -> std::io::Result<()> { - let camera = Camera::new(Point3::new(0.0,1.0,0.0), Vector3::new(0.0,0.0,1.0), 1.0, 2.0, 2.0, 500, 500); + let camera = Camera::new(Point3::new(0.0,1.0,0.0), Vector3::new(0.0,0.0,1.0), 1.0, 1.0, 2.0, 500); let scene = vec![ Object::new(Plane::xz(|x, y| Color::new(y.sin(), x.cos(), 0.0))),