summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-02 15:00:40 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-02 15:00:40 +0700
commitd618968b9577cbaf6306f42157be19bd2a6a6aa0 (patch)
tree4f94cde6fb180393f9d8eabc9f86730e51c42cb5
parentd0e7b955bc93423808990c3e86214340bbb28600 (diff)
upd gamma correction
-rw-r--r--src/camera.rs4
-rw-r--r--src/common.rs14
2 files changed, 16 insertions, 2 deletions
diff --git a/src/camera.rs b/src/camera.rs
index a082f34..63e0097 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -1,5 +1,5 @@
use crate::calculus::calculus::{sample_square, Point3, Ray, Vec3};
-use crate::common::{get_image_height, Color, DisplayBuffer, Pixel, ASPECT_RATIO, CAMERA_CENTER, FOCAL_LENGTH, IMG_WIDTH, SAMPLES_PER_PIXEL, VIEWPORT_HEIGHT};
+use crate::common::{get_image_height, Color, DisplayBuffer, Pixel, ASPECT_RATIO, CAMERA_CENTER, FOCAL_LENGTH, IMG_WIDTH, MAX_DEPTH, SAMPLES_PER_PIXEL, VIEWPORT_HEIGHT};
use crate::interval::Interval;
use crate::object::{HitRecord, Hittable, HittableList};
@@ -46,7 +46,7 @@ impl Camera {
&delta_pixel_u.add(&delta_pixel_u).scalar_mul(0.5)
);
- let max_depth = 50;
+ let max_depth = MAX_DEPTH;
Self {
aspect_ratio,
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)
}