From e24fe740799a45097439834eeab8d73a9e311882 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Wed, 29 Nov 2023 23:35:01 +0200 Subject: [PATCH] add defocus blur --- src/camera.cpp | 31 ++++++++++++++++++++++--------- src/main.cu | 14 ++++++++++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index a7416ce..ffc456e 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -2,26 +2,39 @@ #include "ray.cpp" +__device__ vec3 random_in_unit_disk(curandState *local_rand_state) { + vec3 p; + do { + p = 2.0f*vec3(curand_uniform(local_rand_state), curand_uniform(local_rand_state), 0) - vec3(1,1,0); + } while(dot(p,p) >= 1.0f); + return p; +} + class camera { public: - __device__ camera(vec3 lookfrom, vec3 lookat, vec3 vup, float vfov, float aspect) { + __device__ camera(vec3 lookfrom, vec3 lookat, vec3 vup, float vfov, float aspect, float aperture, float focus_dist) { + lens_radius = aperture / 2.0f; float theta = vfov*M_PI/180; float half_height = tan(theta/2); float half_width = aspect*half_height; origin = lookfrom; - vec3 w = unit_vector(lookfrom - lookat); - vec3 u = unit_vector(cross(vup, w)); - vec3 v = cross(w, u); - lower_left_corner = origin - half_width*u - half_height*v - w; - horizontal = 2*half_width*u; - vertical = 2*half_height*v; + w = unit_vector(lookfrom - lookat); + u = unit_vector(cross(vup, w)); + v = cross(w, u); + lower_left_corner = origin - focus_dist*(half_width*u + half_height*v + w); + horizontal = 2*half_width*focus_dist*u; + vertical = 2*half_height*focus_dist*v; } - __device__ ray get_ray(float u, float v) { - return ray(origin, lower_left_corner + u*horizontal + v*vertical - origin); + __device__ ray get_ray(float s, float t, curandState *local_rand_state) { + vec3 rd = lens_radius*random_in_unit_disk(local_rand_state); + vec3 offset = u * rd.x() + v * rd.y(); + return ray(origin + offset, lower_left_corner + s*horizontal + t*vertical - origin - offset); } vec3 origin; vec3 lower_left_corner; vec3 horizontal; vec3 vertical; + vec3 u, v, w; + float lens_radius; }; diff --git a/src/main.cu b/src/main.cu index b77bc51..2fb4f75 100644 --- a/src/main.cu +++ b/src/main.cu @@ -81,7 +81,7 @@ __global__ void render(vec3 *fb, for (int s = 0; s < ns; s++) { float u = float(x + curand_uniform(&local_rand_state)) / max_x; float v = float(y + curand_uniform(&local_rand_state)) / max_y; - ray r = (*cam)->get_ray(u,v); + ray r = (*cam)->get_ray(u,v, &local_rand_state); col += color(r, world, &local_rand_state); } rand_state[pixel_idx] = local_rand_state; @@ -101,11 +101,17 @@ __global__ void create_world(hitable **d_list, int d_list_size, hitable **d_worl d_list[3] = new sphere(vec3(-1, 0 , -1), 0.5, new dielectric(1.5)); d_list[4] = new sphere(vec3(-1, 0 , -1), -0.45, new dielectric(1.5)); *d_world = new hitable_list(d_list, d_list_size); - *d_camera = new camera(vec3(-2,2,1), - vec3(0,0,-1), + + vec3 lookfrom(3,3,2); + vec3 lookat(0,0,-1); + float dist_to_focus = (lookfrom-lookat).length(); + float aperture = 2.0f; + *d_camera = new camera(lookfrom, lookat, vec3(0,1,0), 20.0, - float(nx)/float(ny)); + float(nx)/float(ny), + aperture, + dist_to_focus); } }