From 3b567beadb6275adf4b60e1eca003e44014c3786 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Tue, 22 Dec 2020 23:19:59 +0100 Subject: Defocus blur & randow scene generation --- src/camera.cpp | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'src/camera.cpp') diff --git a/src/camera.cpp b/src/camera.cpp index 520cc1e..c7866d6 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -2,27 +2,37 @@ #include "rt.hpp" -camera::camera(point3 position, point3 target, double v_fov, double aspect_ratio) - : position(position) { +camera::camera( + point3 position, + point3 target, + double v_fov, + double aspect_ratio, + double aperture, + double focus_dist + ) : position(position), lens_radius(aperture / 2.0) { - auto forward = (position - target).unit(); - auto right = vec3{ 0.0, 1.0, 0.0 }.cross(forward).unit(); - auto up = forward.cross(right).unit(); + forward = (position - target).unit(); + right = vec3{ 0.0, 1.0, 0.0 }.cross(forward).unit(); + up = forward.cross(right).unit(); auto h = tan(deg_to_rad(v_fov) / 2.0); auto viewport_height = 2.0 * h; auto viewport_width = viewport_height * aspect_ratio; - horizontal = viewport_width * right; - vertical = viewport_height * up; + horizontal = focus_dist * viewport_width * right; + vertical = focus_dist * viewport_height * up; - bottom_left_pixel = position - horizontal / 2.0 - vertical / 2.0 - forward; + bottom_left_pixel = position - horizontal / 2.0 - vertical / 2.0 - forward * focus_dist; } ray camera::get_ray(double u, double v) const { + vec3 rd = lens_radius * random_in_unit_disk(); + vec3 offset = rd.x * right + rd.y * up; + point3 viewport_pixel = bottom_left_pixel + u * horizontal + v * vertical; + return ray { - position, - viewport_pixel - position + position + offset, + viewport_pixel - position - offset }; } -- cgit v1.3