aboutsummaryrefslogtreecommitdiffstats
path: root/src/camera.cpp
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2020-12-22 23:19:59 +0100
committerladevieq <ladevie.quentin@gmail.com>2020-12-22 23:19:59 +0100
commit3b567beadb6275adf4b60e1eca003e44014c3786 (patch)
tree7e5c65c19ddee300c01ad2ef698b66ebf9a4802e /src/camera.cpp
parent6b6ef77808585713ab1d04f03e970865b7e7ddb8 (diff)
Defocus blur & randow scene generation
Diffstat (limited to 'src/camera.cpp')
-rw-r--r--src/camera.cpp30
1 files changed, 20 insertions, 10 deletions
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
};
}