aboutsummaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/ray.rs8
-rw-r--r--src/core/spectrum.rs26
-rw-r--r--src/core/vector3.rs13
3 files changed, 44 insertions, 3 deletions
diff --git a/src/core/ray.rs b/src/core/ray.rs
index f5517ce..c944184 100644
--- a/src/core/ray.rs
+++ b/src/core/ray.rs
@@ -8,6 +8,14 @@ pub struct Ray {
}
impl Ray {
+ pub fn new_to(origin: Vector3f, target: Vector3f) -> Ray {
+ let dir = (target - origin).norm();
+ Ray {
+ origin,
+ direction: dir
+ }
+ }
+
pub fn at(&self, t: Float) -> Vector3f {
self.origin + self.direction * t
}
diff --git a/src/core/spectrum.rs b/src/core/spectrum.rs
index 41b3342..727a1b6 100644
--- a/src/core/spectrum.rs
+++ b/src/core/spectrum.rs
@@ -6,7 +6,7 @@ use std::ops;
// TODO implement SampledSpectrum instead for nicer images
-#[derive(Clone, Default)]
+#[derive(Clone, Copy, Default)]
pub struct Spectrum {
c: [Float; 3],
}
@@ -19,9 +19,17 @@ impl Spectrum {
pub fn to_rgb(&self, scale: Float) -> (Float, Float, Float) {
(self.c[0] * scale, self.c[1] * scale, self.c[2] * scale)
}
+
+ pub fn gamma_correct(&self) -> Self {
+ Self::new_rgb(
+ self.c[0].sqrt(),
+ self.c[1].sqrt(),
+ self.c[2].sqrt(),
+ )
+ }
}
-impl std::ops::Mul<Float> for &Spectrum {
+impl std::ops::Mul<Float> for Spectrum {
type Output = Spectrum;
fn mul(self, op: Float) -> Self::Output {
@@ -33,7 +41,7 @@ impl std::ops::Mul<Float> for &Spectrum {
}
}
-impl std::ops::Div<Float> for &Spectrum {
+impl std::ops::Div<Float> for Spectrum {
type Output = Spectrum;
fn div(self, op: Float) -> Self::Output {
@@ -45,6 +53,18 @@ impl std::ops::Div<Float> for &Spectrum {
}
}
+impl std::ops::Add for Spectrum {
+ type Output = Spectrum;
+
+ fn add(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::AddAssign<&Self> for Spectrum {
fn add_assign(&mut self, op: &Self) {
self.c[0] += op.c[0];
diff --git a/src/core/vector3.rs b/src/core/vector3.rs
index 10de647..ef067aa 100644
--- a/src/core/vector3.rs
+++ b/src/core/vector3.rs
@@ -50,6 +50,18 @@ impl<T: Number> Add for Vector3<T> {
}
}
+impl<T: Number> Add<T> for Vector3<T> {
+ type Output = Self;
+
+ fn add(self, op: T) -> Self::Output {
+ Self::new_xyz(
+ self.x + op,
+ self.y + op,
+ self.z + op,
+ )
+ }
+}
+
impl<T: Number> Mul<T> for Vector3<T> {
type Output = Self;
fn mul(self, op: T) -> Self::Output {
@@ -114,6 +126,7 @@ impl Vector3f {
/// assert!(v.x == 1.0);
/// ```
pub fn norm_in(&mut self) {
+ // TODO Experiment with checking for normality with len_squared
let len = self.len();
if len == 0.0 {
*self = Self::new(0.0);