diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-02 01:32:01 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-02 01:32:01 +0700 |
commit | da70c57d01cf6ffe7c1aab30921eb22cd5197dae (patch) | |
tree | cff1c2e09f92445efbfff5dccb7c10522f5b6c33 /src/calculus.rs | |
parent | 1207c2c55c772155d02a148d76616881f3f6c125 (diff) |
upd: diffuse material
Diffstat (limited to 'src/calculus.rs')
-rw-r--r-- | src/calculus.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/calculus.rs b/src/calculus.rs index a39c8a5..8865154 100644 --- a/src/calculus.rs +++ b/src/calculus.rs @@ -16,6 +16,43 @@ pub mod calculus { Self { x, y, z } } + fn random() -> Self { + let mut rng = rand::rng(); + Self::new( + rng.random(), + rng.random(), + rng.random(), + ) + } + + fn random_range(min: f32, max: f32) -> Self { + let mut rng = rand::rng(); + Self::new( + rng.random_range(min..max), + rng.random_range(min..max), + rng.random_range(min..max), + ) + } + + fn random_unit() -> Vec3 { + loop { + let p = Self::random_range(-1.0, 1.0); + let lensq = p.mag_sqr(); + if f32::EPSILON < lensq && lensq <= 1.0 { + return p.scalar_mul(1.0 / lensq.sqrt()) + } + } + } + + pub fn random_on_hemisphere(normal: &Vec3) -> Vec3 { + let on_unit_sphere = Self::random_unit(); + if on_unit_sphere.dot_prod(normal) > 0.0 { + on_unit_sphere + } else { + on_unit_sphere.scalar_mul(-1.0) + } + } + pub fn add(&self, other: &Self) -> Self { Self { x: self.x + other.x, |