From 2b1f81ba329b17a18cdd75604625076ee614f5ef Mon Sep 17 00:00:00 2001 From: ladevieq Date: Wed, 20 Jan 2021 22:08:03 +0100 Subject: Update fov on window resize --- src/camera.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/camera.cpp') diff --git a/src/camera.cpp b/src/camera.cpp index 027ad1e..54c847d 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -10,18 +10,29 @@ camera::camera( float aspect_ratio, float aperture, float focus_dist - ) : position(position), lens_radius(aperture / 2.0) { + ) : position(position), lens_radius(aperture / 2.0), fov(v_fov), focus_distance(focus_dist) { - auto forward = (target - position).unit(); + forward = (target - position).unit(); right = forward.cross(vec3{ 0.0, 1.0, 0.0 }).unit(); up = right.cross(forward).unit(); - auto h = tan(deg_to_rad(v_fov) / 2.0); + auto h = tan(deg_to_rad(fov) / 2.0); auto viewport_height = 2.0 * h; auto viewport_width = viewport_height * aspect_ratio; horizontal = focus_dist * viewport_width * right; vertical = focus_dist * viewport_height * up; - first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_dist * forward; + first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_distance * forward; +} + +void camera::set_aspect_ratio(float aspect_ratio) { + auto h = tan(deg_to_rad(fov) / 2.0); + auto viewport_height = 2.0 * h; + auto viewport_width = viewport_height * aspect_ratio; + + horizontal = focus_distance * viewport_width * right; + vertical = focus_distance * viewport_height * up; + + first_pixel = position - horizontal / 2.0 - vertical / 2.0 + focus_distance * forward; } -- cgit v1.3