Plane-ray intersection test

This commit is contained in:
bijan2005 2020-11-30 09:00:35 -05:00
parent 7eb6f48fef
commit 6a7a3c7774
3 changed files with 11 additions and 3 deletions

View file

@ -15,8 +15,8 @@ This list may be changed or extended in the future.
- [x] Sphere normal generation - [x] Sphere normal generation
- [x] Color mapping on spheres - [x] Color mapping on spheres
- [ ] Plane objects - [ ] Plane objects
- [ ] Plane struct - [x] Plane struct
- [ ] Plane intersection test - [x] Plane intersection test
- [ ] Color mapping on planes - [ ] Color mapping on planes
- [ ] Triangle objects - [ ] Triangle objects
- [ ] Triangle struct - [ ] Triangle struct

View file

@ -1,5 +1,6 @@
mod sphere; pub use sphere::*; mod sphere; pub use sphere::*;
mod plane; pub use plane::*;
mod triangle; pub use triangle::*; mod triangle; pub use triangle::*;
use na::*; use na::*;

View file

@ -27,7 +27,14 @@ impl Plane {
{ Plane::new(center, normal, move |_, _| color) } { Plane::new(center, normal, move |_, _| color) }
pub fn intersect(&self, ray: Ray) -> Option<f32> { pub fn intersect(&self, ray: Ray) -> Option<f32> {
unimplemented!()
let d = self.normal.dot(&ray.direction);
if d < 1e-6 { return None; }
let t = (self.center - ray.origin).dot(&*self.normal) / d;
if t >= 0.0 { Some(t) }
else { None }
} }
pub fn getcolor(&self, point: Point3<f32>) -> Color { pub fn getcolor(&self, point: Point3<f32>) -> Color {