diff --git a/src/camera.rs b/src/camera.rs index 6040577..6f6a021 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::{BufWriter}; use crate::hittable::HittableList; -use crate::math_utils::clamp; +use crate::math_utils::{clamp, linear_to_gamma}; use crate::ppm_writer::PPMWriter; use crate::types_defined::{Camera, Color, Point, Ray, Vec3}; use rand::rngs::ThreadRng; @@ -83,12 +83,12 @@ impl<'a> Camera<'a> { // clamp color rgb let color_clamped = Color::new( - clamp(color.x, 0.0, 1.0), - clamp(color.y, 0.0, 1.0), - clamp(color.z, 0.0, 1.0), + clamp(linear_to_gamma(color.x), 0.0, 1.0), + clamp(linear_to_gamma(color.y), 0.0, 1.0), + clamp(linear_to_gamma(color.z), 0.0, 1.0), ); - self.ppm_file_writer.write(color.to_color()); + let _ = self.ppm_file_writer.write(color_clamped.to_color()); // content diff --git a/src/math_utils.rs b/src/math_utils.rs index 56a60cf..6c4a52a 100644 --- a/src/math_utils.rs +++ b/src/math_utils.rs @@ -7,4 +7,12 @@ pub fn clamp(value: f32, low: f32, high: f32) -> f32 { return high; } return value; +} + +pub fn linear_to_gamma(value: f32) -> f32 { + if value > 0.0 { + value.sqrt() + } else { + 0.0 + } } \ No newline at end of file