-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This required a fair bit of faffing, and I'm not completely sure I know what I'm really doing with the boxing. However! My first polymorphism and traits. I should probably read the book properly, but who's got time for that. Some fun also since rust modulo arithmetic seems to be non-intuitive, and the euclidean versions aren't stable yet. https://github.com/rust-lang/rfcs/blob/master/text/2169-euclidean-modulo.md rust-lang/rust#49048
- Loading branch information
1 parent
489fc4c
commit 32d66cb
Showing
5 changed files
with
89 additions
and
14 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 |
---|---|---|
@@ -1 +1,13 @@ | ||
pub mod sphere; | ||
pub mod plane; | ||
|
||
use crate::vector::Vec; | ||
use crate::ray::Ray; | ||
use crate::color::Color; | ||
|
||
pub trait Object { | ||
fn intersect(&self, ray: Ray) -> Option<f64>; | ||
fn surface_normal(&self, point: Vec) -> Vec; | ||
fn color_at(&self, point: Vec) -> Color; | ||
fn reflectance(&self) -> f64; | ||
} |
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,47 @@ | ||
use crate::object::Object; | ||
use crate::vector::Vec; | ||
use crate::ray::Ray; | ||
use crate::color::Color; | ||
|
||
#[derive(Debug)] | ||
pub struct Plane { | ||
point: Vec, | ||
normal: Vec, | ||
pub color: Color, | ||
pub reflectance: f64, | ||
} | ||
|
||
impl Plane { | ||
pub fn new(point: Vec, normal: Vec, color: Color, reflectance: f64) -> Self { | ||
Self { point, normal, color, reflectance } | ||
} | ||
} | ||
|
||
impl Object for Plane { | ||
fn intersect(&self, ray: Ray) -> Option<f64> { | ||
let ndotl = self.normal.dot(ray.direction); | ||
|
||
if ndotl.abs() < 1e-10 { | ||
None | ||
} else { | ||
let t = self.normal.dot(self.point.subtract(ray.origin)) / ndotl; | ||
if t < 0.0 { None } else { Some(t) } | ||
} | ||
} | ||
|
||
fn surface_normal(&self, _point: Vec) -> Vec { | ||
self.normal | ||
} | ||
|
||
fn color_at(&self, point: Vec) -> Color { | ||
if (point.x.round() + point.z.round()).abs() % 2.0 < 1e-10 { | ||
Color::new(10.0, 10.0, 10.0) | ||
} else { | ||
self.color | ||
} | ||
} | ||
|
||
fn reflectance(&self) -> f64 { | ||
self.reflectance | ||
} | ||
} |
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