# Defocus blur: Computing the pixel plane distance

I'm following Peter Shirley book, Ray tracing in one weekend. In the last chapter, he talks about how to make defocus blur by using thin len approximation. His camera class accepts a parameter called focus_dist, which he then uses to compute the distance to the image plane.

lower_left_corner = origin  - half_width*focus_dist*u -half_height*focus_dist*v - focus_dist*w; // lower_left_corner of image plane


This subject was new to me, so I went searching around online. From what i understand, the distance to the image plane is governed by the equation 1/f = 1/d + 1/d prime with f being focal length and d being the focus distance. d prime will be the answer. The lack of this equation in Peter Shirley's code and the fact that he seems to be putting the image plane at the focus distance makes me completely lost. Can anyone explain it to me?

You don't need that equation.

You know your focus point (point on y-axis with focus_distance as z component) and the ray origin. All that needs to be done then is to compute the ray direction from the origin through that focus point.

For what it's worth, I think a camera space formulation of a thin lens makes it easier to understand the concept:

float fov = 2.0f * atan((sensorWidth / (2.0f * focal_length))); // in radians
float tan_fov = tanf(fov / 2.0f);

// create point on lens, screen space samples are in [-1, 1] domain
vec3 p(screen_space_sample_x * tan_fov, screen_space_sample_y * tan_fov, 1.0);

// calculate direction vector from origin to point on lens
dir = normalize(p - origin);

// get uniformly distributed points on the unit disk
vec2 lens(0.0, 0.0);
concentricDiskSample(random1, random2, &lens);

// scale points in [-1, 1] domain to actual aperture radius

// new origin is these points on the lens
origin.x = lens.x;
origin.y = lens.y;
origin.z = 0.0;

// Compute point on plane of focus, intersection on z axis
float intersection = std::abs(focus_distance / dir.z);
vec3 focusPoint = dir * intersection;
dir = normalize(focusPoint - origin);

// now looking down -Z (optional depending on your coordinate system)
dir.z *= -1.0;