diff options
| -rw-r--r-- | include/camera.hpp | 7 | ||||
| -rw-r--r-- | shaders/compute.comp | 4 | ||||
| -rw-r--r-- | src/camera.cpp | 19 | ||||
| -rw-r--r-- | src/main.cpp | 1 | ||||
| -rw-r--r-- | src/vk-renderer.cpp | 6 | ||||
| -rw-r--r-- | src/window.cpp | 2 |
6 files changed, 31 insertions, 8 deletions
diff --git a/include/camera.hpp b/include/camera.hpp index 43ed755..fd17947 100644 --- a/include/camera.hpp +++ b/include/camera.hpp @@ -14,8 +14,11 @@ class camera { float focus_dist ); + void set_aspect_ratio(float aspect_ratio); + point3 position; + vec3 forward; vec3 right; vec3 up; @@ -25,7 +28,9 @@ class camera { vec3 first_pixel; float lens_radius; - float padding[3]; + float fov; + float focus_distance; + float padding; }; #endif // !__CAMERA_HPP_ diff --git a/shaders/compute.comp b/shaders/compute.comp index e236f3e..1e903b0 100644 --- a/shaders/compute.comp +++ b/shaders/compute.comp @@ -35,6 +35,7 @@ struct sphere { struct camera { vec4 position; + vec4 forward; vec4 up; vec4 right; @@ -218,8 +219,9 @@ void main() { vec2 lens_disk = random_disk[sample_index] * cam.lens_radius; vec3 offset = lens_disk.x * cam.right.xyz + lens_disk.y * cam.up.xyz; vec4 proj_plane_pos = cam.first_pixel + jittered_uvs.x * cam.horizontal + jittered_uvs.y * cam.vertical; + ray r = ray(cam.position.xyz + offset, proj_plane_pos.xyz - cam.position.xyz - offset); - out_color += ray_color(ray(cam.position.xyz + offset, proj_plane_pos.xyz - cam.position.xyz - offset), sample_index); + out_color += ray_color(r, sample_index); } float spp_scale = (1.0 / samples_per_pixel); 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; } diff --git a/src/main.cpp b/src/main.cpp index b449c4c..0de69f7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,6 +67,7 @@ int main() { } else { canRender = true; renderer.recreate_swapchain(); + renderer.mapped_data->cam.set_aspect_ratio((float)event.width / (float)event.height); } break; } diff --git a/src/vk-renderer.cpp b/src/vk-renderer.cpp index cfb437e..e3eca75 100644 --- a/src/vk-renderer.cpp +++ b/src/vk-renderer.cpp @@ -309,10 +309,12 @@ void vkrenderer::create_swapchain() { if (wantedExtent.width < caps.minImageExtent.width) { wantedExtent.width = caps.minImageExtent.width; - } else if (wantedExtent.height < caps.minImageExtent.height) { - wantedExtent.height = caps.minImageExtent.height; } else if (wantedExtent.width > caps.maxImageExtent.width) { wantedExtent.width = caps.maxImageExtent.width; + } + + if (wantedExtent.height < caps.minImageExtent.height) { + wantedExtent.height = caps.minImageExtent.height; } else if (wantedExtent.height > caps.maxImageExtent.height) { wantedExtent.height = caps.maxImageExtent.height; } diff --git a/src/window.cpp b/src/window.cpp index c273f43..77e8f1c 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -81,6 +81,7 @@ void window::poll_events() { auto client_message_event = (xcb_client_message_event_t*)event; if (client_message_event->data.data32[0] == win_delete_atom) { events.push_back({ .type = EVENT_TYPES::QUIT }); + isOpen = false; } } default: { @@ -159,6 +160,7 @@ LRESULT window::message_handler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lpar case WM_CLOSE: case WM_DESTROY: case WM_QUIT: { + events.push_back({ .type = EVENT_TYPES::QUIT }); isOpen = false; return 0; } |
