-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
566,261 additions
and
123,796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::{ray::*, vec3::*, objects::hittable::HitRecord}; | ||
|
||
pub trait Material { | ||
fn scatter(&self, r_in: Ray, rec: HitRecord, attenuation: &mut color, scattered: &mut Ray) -> bool; | ||
} | ||
#[derive(Copy, Clone)] | ||
pub struct Lambertian { | ||
pub albedo: color, | ||
} | ||
impl Lambertian { | ||
pub fn from(a: color) -> Self { | ||
Self {albedo: a} | ||
} | ||
} | ||
impl Material for Lambertian { | ||
fn scatter(&self, r_in: Ray, rec: HitRecord, attenuation: &mut color, scattered: &mut Ray) -> bool { | ||
let mut scatter_direction = rec.normal + color::random_unit_vector(); | ||
|
||
if scatter_direction.near_zero() { | ||
scatter_direction = rec.normal; | ||
} | ||
|
||
*scattered = Ray::new(rec.p, scatter_direction); | ||
*attenuation = self.albedo; | ||
true | ||
|
||
} | ||
} | ||
#[derive(Copy, Clone)] | ||
pub struct Metal { | ||
pub albedo: color, | ||
} | ||
impl Metal { | ||
pub fn from(a: color) -> Self { | ||
Self {albedo: a} | ||
} | ||
} | ||
impl Material for Metal { | ||
fn scatter(&self, r_in: Ray, rec: HitRecord, attenuation: &mut color, scattered: &mut Ray) -> bool { | ||
let reflected = reflect(unit_vector(r_in.direction()), rec.normal); | ||
*scattered = Ray::new(rec.p, reflected); | ||
*attenuation = self.albedo; | ||
dot(scattered.direction(), rec.normal) > 0. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod material; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters