diff options
| author | Sakarias Johansson <sakarias.johansson@goodbyekansas.com> | 2023-01-12 21:09:43 +0100 |
|---|---|---|
| committer | Sakarias Johansson <sakarias.johansson@goodbyekansas.com> | 2023-01-12 21:09:43 +0100 |
| commit | a6302805d19273c95278c8d792ffbd9b2633fe20 (patch) | |
| tree | 9e539215cce7ba2eed4f873c711282f9d40a2591 /racer-tracer/src/material/lambertian.rs | |
| parent | bbecf67545c2bd6822b0680673aa850c5ddef9f3 (diff) | |
| download | racer-tracer-a6302805d19273c95278c8d792ffbd9b2633fe20.tar.gz racer-tracer-a6302805d19273c95278c8d792ffbd9b2633fe20.tar.xz racer-tracer-a6302805d19273c95278c8d792ffbd9b2633fe20.zip | |
🖌️ Add materials
Diffstat (limited to 'racer-tracer/src/material/lambertian.rs')
| -rw-r--r-- | racer-tracer/src/material/lambertian.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/racer-tracer/src/material/lambertian.rs b/racer-tracer/src/material/lambertian.rs new file mode 100644 index 0000000..8356685 --- /dev/null +++ b/racer-tracer/src/material/lambertian.rs @@ -0,0 +1,33 @@ +use crate::{ + material::Material, + ray::Ray, + vec3::{random_unit_vector, Color, Vec3}, +}; + +pub struct Lambertian { + color: Color, +} + +impl Lambertian { + pub fn new(color: Color) -> Self { + Self { color } + } +} + +impl Material for Lambertian { + fn scatter( + &self, + ray: &crate::ray::Ray, + rec: &crate::geometry::HitRecord, + ) -> Option<(Ray, Color)> { + let mut scatter_direction = rec.normal + random_unit_vector(); + + // Catch bad scatter direction + if scatter_direction.near_zero() { + scatter_direction = rec.normal; + } + + let scattered = Ray::new(rec.point, scatter_direction); + Some((scattered, self.color)) + } +} |
