summaryrefslogtreecommitdiff
path: root/src/calculus.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/calculus.rs')
-rw-r--r--src/calculus.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/calculus.rs b/src/calculus.rs
index 568eb60..2e8a1d5 100644
--- a/src/calculus.rs
+++ b/src/calculus.rs
@@ -119,6 +119,19 @@ pub mod calculus {
)
)
}
+
+ pub fn refract(&self, normal: &Self, etai_over_etat: f32) -> Self {
+ let cos_theta = f32::min(
+ 1.0,
+ self.scalar_mul(-1.0).dot_prod(&normal)
+ );
+ let r_out_perp = self
+ .add(&normal.scalar_mul(cos_theta))
+ .scalar_mul(etai_over_etat);
+ let f = (1.0 - r_out_perp.mag_sqr()).abs().sqrt();
+ let r_out_parallel = normal.scalar_mul(-1.0 * f);
+ r_out_perp.add(&r_out_parallel)
+ }
}
pub struct Ray {
@@ -133,7 +146,13 @@ pub mod calculus {
}
pub fn reflect(&self, normal: &Vec3) -> Vec3 {
- self.direction.reflect(&normal)
+ // return unit direction vector
+ self.direction.reflect(&normal).unit()
+ }
+
+ pub fn refract(&self, normal: &Vec3, etai_over_etat: f32) -> Vec3 {
+ // return unit direction vector
+ self.direction.unit().refract(&normal, etai_over_etat)
}
}