diff options
author | Julian T <julian@jtle.dk> | 2021-02-08 18:26:27 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-02-08 18:27:00 +0100 |
commit | 7ccc82fc0992fc23aee354d687ce009ae0523bea (patch) | |
tree | 635f7ffe386c638ef8d5ca0db81a3da8ef0f7cc0 /src/camera | |
parent | dbe5f54957ddc13549a6ce822da95170aa09a0c6 (diff) |
Finish depth of field
Diffstat (limited to 'src/camera')
-rw-r--r-- | src/camera/camera.rs | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/camera/camera.rs b/src/camera/camera.rs index e770efe..4d05e87 100644 --- a/src/camera/camera.rs +++ b/src/camera/camera.rs @@ -33,6 +33,7 @@ //! ``` use crate::Float; use crate::core::{Vector3f, Vector2f, Vector2i, Ray}; +use crate::sample::Sampler; /// A simple perspective camera pub struct Camera { @@ -43,6 +44,9 @@ pub struct Camera { /// Scaling vectors from screen_origin qx: Vector3f, qy: Vector3f, + + /// Value for depth of view + lens_radius: Option<Float>, } /// Settings for initializing camera @@ -59,7 +63,9 @@ pub struct CameraSettings { /// The film aspect ratio, height / width pub filmsize: Vector2i, /// The lens aperture - pub aperture: Float, + /// + /// Depth of view is disabled if None + pub aperture: Option<Float>, /// The distance to the focus plane /// /// if None it will be set to the distance between origin and target @@ -96,22 +102,31 @@ impl Camera { screen_origin, qx, qy, + lens_radius: set.aperture.map(|a| a / 2.0), } } /// Generates a ray a screen space point /// - /// The point coordinates should be between [0,1) with (0, 0) being the upper let corner + /// The point coordinates should be between [0,1) with (0, 0) being the upper left corner /// /// Will return a ray and a weight /// /// The direction of the returned way is normalized - pub fn generate_ray(&self, point: &Vector2f) -> (Ray, Float) { - let mut dir = self.screen_origin + (self.qx * point.x) - (self.qy * point.y); - dir.norm_in(); + pub fn generate_ray(&self, point: &Vector2f, sampler: &mut dyn Sampler) -> (Ray, Float) { + // Depth of view origin offset + let ooffset = match self.lens_radius { + Some(r) => { + let rand_dir = sampler.get_in_circle() * r; + self.qx * rand_dir.x + self.qy * rand_dir.y + }, + None => Vector3f::ZERO, + }; + + let dir = self.screen_origin + (self.qx * point.x) - (self.qy * point.y) - ooffset; ( - Ray { origin: self.origin, direction: dir }, + Ray { origin: self.origin + ooffset, direction: dir.norm() }, 1.0 ) } |