aboutsummaryrefslogtreecommitdiff
path: root/src/material/dielectric.rs
diff options
context:
space:
mode:
authorlamp2024-01-21 17:03:07 +0000
committerlamp2024-01-21 17:03:07 +0000
commit45fafbcd4b41a5388ece377c4e051b5846407288 (patch)
tree2192c471ca3b47671f0906e27f6f6088f40b3d8a /src/material/dielectric.rs
parent89a5c9a8a0cdf627cda0e31da454f83ca21315ce (diff)
fmt and tidy
Diffstat (limited to 'src/material/dielectric.rs')
-rw-r--r--src/material/dielectric.rs33
1 files changed, 26 insertions, 7 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
}
}