diff options
author | Julian T <julian@jtle.dk> | 2021-02-07 14:25:06 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-02-07 14:26:20 +0100 |
commit | 33e747fa1c0957546c10e4d7b490ac7fbb0fd2d2 (patch) | |
tree | 515b2b2e017329cbcada60e898fb5795fbceb5cd /src/core | |
parent | a43ab40a75e9d4c6ee3fbdd583bc93574db19124 (diff) |
Abstracted lambertian into material
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/hittable.rs | 23 | ||||
-rw-r--r-- | src/core/mod.rs | 2 | ||||
-rw-r--r-- | src/core/spectrum.rs | 12 | ||||
-rw-r--r-- | src/core/vector3.rs | 11 |
4 files changed, 47 insertions, 1 deletions
diff --git a/src/core/hittable.rs b/src/core/hittable.rs new file mode 100644 index 0000000..20c82f7 --- /dev/null +++ b/src/core/hittable.rs @@ -0,0 +1,23 @@ +use crate::core::{Vector3f, Ray}; + +/// Returns the context of a intersection +pub struct Intersection { + /// Normal vector at intersection + pub n: Vector3f, + pub p: Vector3f, +} + +impl Intersection { + pub fn norm_against_ray(&self, r: &Ray) -> Vector3f { + if self.n.dot(&r.direction) < 0.0 { + self.n + } else { + -self.n + } + } +} + +/// Defines a common trait for objects in the scene +pub trait Hittable { + fn intersect(&self, ray: &Ray) -> Option<Intersection>; +} diff --git a/src/core/mod.rs b/src/core/mod.rs index 5723b2d..c2b3771 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -6,6 +6,7 @@ pub mod vector2; pub mod vector3; pub mod bound; pub mod spectrum; +pub mod hittable; mod ray; pub use vector2::{Vector2i, Vector2f}; @@ -13,3 +14,4 @@ pub use vector3::Vector3f; pub use bound::{Bound2i, Bound2f}; pub use spectrum::Spectrum; pub use ray::Ray; +pub use hittable::{Hittable, Intersection}; diff --git a/src/core/spectrum.rs b/src/core/spectrum.rs index 727a1b6..9cca7dc 100644 --- a/src/core/spectrum.rs +++ b/src/core/spectrum.rs @@ -41,6 +41,18 @@ impl std::ops::Mul<Float> for Spectrum { } } +impl std::ops::Mul for Spectrum { + type Output = Spectrum; + + fn mul(self, op: Self) -> Self::Output { + Self::Output::new_rgb( + self.c[0] * op.c[0], + self.c[1] * op.c[1], + self.c[2] * op.c[2], + ) + } +} + impl std::ops::Div<Float> for Spectrum { type Output = Spectrum; diff --git a/src/core/vector3.rs b/src/core/vector3.rs index ef067aa..ff5a678 100644 --- a/src/core/vector3.rs +++ b/src/core/vector3.rs @@ -1,7 +1,7 @@ //! Implements 3d vectors //! //! Also add more 3d math things needed for shading and 3d calculations. -use crate::{Float, Number}; +use crate::{Float, Number, NEAR_ZERO}; use std::ops::{Mul, Sub, Add, DivAssign, Neg}; use std::fmt; @@ -149,4 +149,13 @@ impl Vector3f { ) } + + /// Check if vector is close to [0, 0, 0] + /// + /// This is based on the NEAR_ZERO constant + pub fn near_zero(&self) -> bool { + (self.x.abs() < NEAR_ZERO) && + (self.y.abs() < NEAR_ZERO) && + (self.z.abs() < NEAR_ZERO) + } } |