aboutsummaryrefslogtreecommitdiff
path: root/src/material
diff options
context:
space:
mode:
authorlamp2024-01-21 17:03:07 +0000
committerlamp2024-01-21 17:03:07 +0000
commit45fafbcd4b41a5388ece377c4e051b5846407288 (patch)
tree2192c471ca3b47671f0906e27f6f6088f40b3d8a /src/material
parent89a5c9a8a0cdf627cda0e31da454f83ca21315ce (diff)
fmt and tidy
Diffstat (limited to 'src/material')
-rw-r--r--src/material/dielectric.rs33
-rw-r--r--src/material/diffuse_light.rs6
-rw-r--r--src/material/isotropic.rs33
-rw-r--r--src/material/lambertian.rs24
-rw-r--r--src/material/metal.rs18
-rw-r--r--src/material/mod.rs18
6 files changed, 95 insertions, 37 deletions
diff --git a/src/material/dielectric.rs b/src/material/dielectric.rs
index 1a49f73..af6f9b0 100644
--- a/src/material/dielectric.rs
+++ b/src/material/dielectric.rs
@@ -1,9 +1,9 @@
use std::ops::Neg;
use super::Material;
-use crate::{hittable::HitRecord, vec3::Vec3};
-use crate::vec3::Color;
use crate::ray::Ray;
+use crate::vec3::Color;
+use crate::{hittable::HitRecord, vec3::Vec3};
pub struct DielectricAttenuation {
pub albedo: Color,
@@ -25,7 +25,13 @@ impl Dielectric {
}
impl Material for Dielectric {
- fn scatter(&self, ray_in: &Ray, hit_record: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
+ fn scatter(
+ &self,
+ ray_in: &Ray,
+ hit_record: &HitRecord,
+ attenuation: &mut Color,
+ scattered: &mut Ray,
+ ) -> bool {
if let Some(props) = &self.attenuation {
let outward_normal = if hit_record.front_face {
hit_record.normal.clone()
@@ -40,9 +46,17 @@ impl Material for Dielectric {
*attenuation = props.albedo.clone();
}
} else {
- *attenuation = Color { x: 1.0, y: 1.0, z: 1.0 };
+ *attenuation = Color {
+ x: 1.0,
+ y: 1.0,
+ z: 1.0,
+ };
}
- let refraction_ratio = if hit_record.front_face { 1.0 / self.index_of_refraction } else { self.index_of_refraction };
+ let refraction_ratio = if hit_record.front_face {
+ 1.0 / self.index_of_refraction
+ } else {
+ self.index_of_refraction
+ };
let unit_direction = ray_in.direction.unit_vector();
let cos_theta = hit_record.normal.dot(&-&unit_direction).min(1.0);
let sin_theta = (1.0 - cos_theta * cos_theta).sqrt();
@@ -50,13 +64,18 @@ impl Material for Dielectric {
let cannot_refract = refraction_ratio * sin_theta > 1.0;
let direction: Vec3;
- if cannot_refract || Self::reflectance(cos_theta, refraction_ratio) > rand::random::<f64>() {
+ if cannot_refract || Self::reflectance(cos_theta, refraction_ratio) > rand::random::<f64>()
+ {
direction = unit_direction.reflect(&hit_record.normal)
} else {
direction = unit_direction.refract(&hit_record.normal, refraction_ratio)
}
- *scattered = Ray { origin: hit_record.p.clone(), direction, time: ray_in.time };
+ *scattered = Ray {
+ origin: hit_record.p.clone(),
+ direction,
+ time: ray_in.time,
+ };
true
}
}
diff --git a/src/material/diffuse_light.rs b/src/material/diffuse_light.rs
index fecbcff..4feca3b 100644
--- a/src/material/diffuse_light.rs
+++ b/src/material/diffuse_light.rs
@@ -1,10 +1,10 @@
use std::sync::Arc;
use super::Material;
-use crate::{hittable::HitRecord, texture::Texture, vec3::Point3};
-use crate::vec3::Color;
-use crate::texture::SolidColor;
use crate::ray::Ray;
+use crate::texture::SolidColor;
+use crate::vec3::Color;
+use crate::{hittable::HitRecord, texture::Texture, vec3::Point3};
pub struct DiffuseLight {
emit: Arc<dyn Texture>,
diff --git a/src/material/isotropic.rs b/src/material/isotropic.rs
index f59fa7d..9949abd 100644
--- a/src/material/isotropic.rs
+++ b/src/material/isotropic.rs
@@ -1,32 +1,39 @@
use std::sync::Arc;
use super::Material;
-use crate::{hittable::HitRecord, texture::Texture, vec3::Vec3};
-use crate::vec3::Color;
-use crate::texture::SolidColor;
use crate::ray::Ray;
+use crate::vec3::Color;
+use crate::{hittable::HitRecord, texture::Texture, vec3::Vec3};
pub struct Isotropic {
albedo: Arc<dyn Texture>,
}
impl Isotropic {
- pub fn from_color(color: Color) -> Self {
- Self {
- albedo: Arc::new(SolidColor::from_color(color)),
- }
- }
+ // pub fn from_color(color: Color) -> Self {
+ // Self {
+ // albedo: Arc::new(SolidColor::from_color(color)),
+ // }
+ // }
pub fn from_texture(texture: Arc<dyn Texture>) -> Self {
- Self {
- albedo: texture,
- }
+ Self { albedo: texture }
}
}
impl Material for Isotropic {
- fn scatter(&self, ray_in: &Ray, hit_record: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
- *scattered = Ray { origin: hit_record.p.clone(), direction: Vec3::random_in_unit_sphere(), time: ray_in.time };
+ fn scatter(
+ &self,
+ ray_in: &Ray,
+ hit_record: &HitRecord,
+ attenuation: &mut Color,
+ scattered: &mut Ray,
+ ) -> bool {
+ *scattered = Ray {
+ origin: hit_record.p.clone(),
+ direction: Vec3::random_in_unit_sphere(),
+ time: ray_in.time,
+ };
*attenuation = self.albedo.value(hit_record.u, hit_record.v, &hit_record.p);
true
}
diff --git a/src/material/lambertian.rs b/src/material/lambertian.rs
index 95f698e..fed26f9 100644
--- a/src/material/lambertian.rs
+++ b/src/material/lambertian.rs
@@ -1,10 +1,10 @@
use std::sync::Arc;
use super::Material;
-use crate::{hittable::HitRecord, texture::Texture, vec3::Vec3};
-use crate::vec3::Color;
-use crate::texture::SolidColor;
use crate::ray::Ray;
+use crate::texture::SolidColor;
+use crate::vec3::Color;
+use crate::{hittable::HitRecord, texture::Texture, vec3::Vec3};
pub struct Lambertian {
pub albedo: Arc<dyn Texture>,
@@ -13,13 +13,21 @@ pub struct Lambertian {
impl Lambertian {
pub fn from_color(color: Color) -> Self {
Self {
- albedo: Arc::new(SolidColor { color_value: color.clone() }),
+ albedo: Arc::new(SolidColor {
+ color_value: color.clone(),
+ }),
}
}
}
impl Material for Lambertian {
- fn scatter(&self, ray_in : &Ray, hit_record: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
+ fn scatter(
+ &self,
+ ray_in: &Ray,
+ hit_record: &HitRecord,
+ attenuation: &mut Color,
+ scattered: &mut Ray,
+ ) -> bool {
let mut scatter_direction = &hit_record.normal + Vec3::random_unit_vector();
// Catch zero-vector scatter directions that will generate issues later
@@ -27,7 +35,11 @@ impl Material for Lambertian {
scatter_direction = hit_record.normal.clone();
}
- *scattered = Ray { origin: hit_record.p.clone(), direction: scatter_direction, time: ray_in.time };
+ *scattered = Ray {
+ origin: hit_record.p.clone(),
+ direction: scatter_direction,
+ time: ray_in.time,
+ };
*attenuation = self.albedo.value(hit_record.u, hit_record.v, &hit_record.p);
true
}
diff --git a/src/material/metal.rs b/src/material/metal.rs
index 2865a2f..638e5e4 100644
--- a/src/material/metal.rs
+++ b/src/material/metal.rs
@@ -1,7 +1,7 @@
use super::Material;
-use crate::{hittable::HitRecord, vec3::Vec3};
-use crate::vec3::Color;
use crate::ray::Ray;
+use crate::vec3::Color;
+use crate::{hittable::HitRecord, vec3::Vec3};
pub struct Metal {
pub albedo: Color,
@@ -9,9 +9,19 @@ pub struct Metal {
}
impl Material for Metal {
- fn scatter(&self, ray_in: &Ray, hit_record: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
+ fn scatter(
+ &self,
+ ray_in: &Ray,
+ hit_record: &HitRecord,
+ attenuation: &mut Color,
+ scattered: &mut Ray,
+ ) -> bool {
let reflected = ray_in.direction.unit_vector().reflect(&hit_record.normal);
- *scattered = Ray { origin: hit_record.p.clone(), direction: reflected + self.fuzz * Vec3::random_in_unit_sphere(), time: ray_in.time };
+ *scattered = Ray {
+ origin: hit_record.p.clone(),
+ direction: reflected + self.fuzz * Vec3::random_in_unit_sphere(),
+ time: ray_in.time,
+ };
*attenuation = self.albedo.clone();
scattered.direction.dot(&hit_record.normal) > 0.0
}
diff --git a/src/material/mod.rs b/src/material/mod.rs
index c292ea1..cded09b 100644
--- a/src/material/mod.rs
+++ b/src/material/mod.rs
@@ -10,13 +10,23 @@ pub use diffuse_light::DiffuseLight;
mod isotropic;
pub use isotropic::Isotropic;
-use crate::{hittable::HitRecord, vec3::Point3};
-use crate::vec3::Color;
use crate::ray::Ray;
+use crate::vec3::Color;
+use crate::{hittable::HitRecord, vec3::Point3};
pub trait Material: Send + Sync {
fn emitted(&self, _: f64, _: f64, _: &Point3) -> Color {
- Color { x: 0.0, y: 0.0, z: 0.0 }
+ Color {
+ x: 0.0,
+ y: 0.0,
+ z: 0.0,
+ }
}
- fn scatter(&self, ray_in: &Ray, hit_record: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool;
+ fn scatter(
+ &self,
+ ray_in: &Ray,
+ hit_record: &HitRecord,
+ attenuation: &mut Color,
+ scattered: &mut Ray,
+ ) -> bool;
}