From 5fb34460b02b2b151dd775e43675bd4f09d2633a Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Sat, 1 Mar 2025 23:07:43 +0700 Subject: refactor camera --- src/global.rs | 69 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 33 deletions(-) (limited to 'src/global.rs') diff --git a/src/global.rs b/src/global.rs index 9357920..fc9e0c9 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1,18 +1,48 @@ use crate::calculus::calculus::Point3; - - // "Ideal" aspect ratio, allowing fraction pub const ASPECT_RATIO: f32 = 16.0f32 / 9.0f32; -pub const IMG_WIDTH: usize = 400; -const IMG_HEIGHT_TMP: usize = (IMG_WIDTH as f32 / ASPECT_RATIO) as usize; -pub const IMG_HEIGHT: usize = if (IMG_HEIGHT_TMP < 1) { 1 } else { IMG_HEIGHT_TMP }; +pub const IMG_WIDTH: usize = 1000; +pub const IMG_HEIGHT: usize = get_image_height(IMG_WIDTH, ASPECT_RATIO); pub const VIEWPORT_HEIGHT: f32 = 2.0; -pub const VIEWPORT_WIDTH: f32 = VIEWPORT_HEIGHT * (IMG_WIDTH as f32 / IMG_HEIGHT as f32); pub const FOCAL_LENGTH: f32 = 1.0; pub const CAMERA_CENTER: Point3 = Point3 { x: 0f32, y: 0f32, z: 0f32 }; +pub const fn get_image_height(image_width: usize, aspect_ratio: f32) -> usize { + let image_height = (image_width as f32 / aspect_ratio) as usize; + if image_width < 1 { 1 } else { image_height } +} + +pub struct Color { + pub r: f32, + pub g: f32, + pub b: f32, +} + +impl Color { + pub fn new(r: f32, g: f32, b: f32) -> Self { + Self { r, g, b } + } + + pub fn mul_scalar(&self, scalar: f32) -> Color { + Self { + r: self.r * scalar, + g: self.g * scalar, + b: self.b * scalar, + } + } + + pub fn add(&self, other: &Self) -> Color { + Self { + r: self.r + other.r, + g: self.g + other.g, + b: self.b + other.b + } + } +} + + #[derive(Debug, Copy, Clone)] pub struct Pixel { pub r: u8, @@ -43,30 +73,3 @@ impl Default for Pixel { pub type DisplayBuffer = [[Pixel; IMG_WIDTH]; IMG_HEIGHT]; -pub struct Color { - pub r: f32, - pub g: f32, - pub b: f32, -} - -impl Color { - pub fn new(r: f32, g: f32, b: f32) -> Self { - Self { r, g, b } - } - - pub fn mul_scalar(&self, scalar: f32) -> Color { - Self { - r: self.r * scalar, - g: self.g * scalar, - b: self.b * scalar, - } - } - - pub fn add(&self, other: &Self) -> Color { - Self { - r: self.r + other.r, - g: self.g + other.g, - b: self.b + other.b - } - } -} -- cgit v1.2.3-70-g09d2