From d618968b9577cbaf6306f42157be19bd2a6a6aa0 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Sun, 2 Mar 2025 15:00:40 +0700 Subject: upd gamma correction --- src/common.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/common.rs') diff --git a/src/common.rs b/src/common.rs index 3975609..a676939 100644 --- a/src/common.rs +++ b/src/common.rs @@ -11,12 +11,14 @@ pub const FOCAL_LENGTH: f32 = 1.0; pub const CAMERA_CENTER: Point3 = Point3 { x: 0f32, y: 0f32, z: 0f32 }; pub const SAMPLES_PER_PIXEL: usize = 100; +pub const MAX_DEPTH: usize = 50; 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 } } +#[derive(Debug, Clone)] pub struct Color { pub r: f32, pub g: f32, @@ -43,6 +45,16 @@ impl Color { b: self.b + other.b } } + + fn _linear_to_gamma(linear_comp: f32) -> f32 { + if linear_comp > 0.0 { linear_comp.sqrt() } else { 0.0 } + } + + pub fn linear_to_gamma(&mut self) { + self.r = Self::_linear_to_gamma(self.r); + self.g = Self::_linear_to_gamma(self.g); + self.b = Self::_linear_to_gamma(self.b); + } } @@ -64,6 +76,8 @@ impl Pixel { } pub fn from_color(color: &Color) -> Self { + let mut clone = color.clone(); + clone.linear_to_gamma(); let (r, g, b) = (color.r, color.g, color.b); Self::from_frac(r, g, b) } -- cgit v1.2.3-70-g09d2