<p>Exactly the same happened to me as it did to you: I was reading the book, and quite following everything that I was reading, until that bit with the defocus blur. My issue was very specifically with the following bits:</p>

<pre><code>lower_left_corner: look_from - half_width*focus_dist*u - half_height*focus_dist*v - focus_dist*w,
horizontal: 2.0*half_width*focus_dist*u,
vertical: 2.0*half_height*focus_dist*v
</code></pre>

<p>It was very clear to me that we should multiply <code>-w</code> by <code>focus_dist</code>, <code>-w</code> being the vector pointing towards the subject. But I couldn't figure out why we should multiply the <code>u</code> and <code>v</code> vectors by <code>focus_dist</code> too. That made no sense to me. Was it about the dimensions of the focus plane or something? I couldn't make sense of it. But removing it made the generated image wrong, so it was really needed.</p>

<p>The insight I had was 'where do we get <code>half_width</code> from?' And the answer is...</p>

<pre><code>let half_height = tan(theta/2.0);
let half_width = aspect * half_height;
</code></pre>

<p>And NOW everything makes sense! That is obviously wrong, as the code computing <code>half_height</code> assumes a distance of 1.0 to the target! Therefore <code>half_height</code> was miscalculated in the book, and the author compensated a little later by multiplying by <code>focus_dist</code> when using <code>half_width</code>...</p>

<p>Therefore the correct code (despite the fact that the original code worked 100%) would be:</p>

<pre><code>let half_height = tan(theta/2.0) * focus_dist;
let half_width = aspect * half_height;
// ...
lower_left_corner: look_from - half_width*u - half_height*v - focus_dist*w,
horizontal: 2.0*half_width*u,
vertical: 2.0*half_height*v,
</code></pre>

CC BY-SA 4.0
2019-07-18T17:39:23.577Z
2019-07-18T17:57:19.937Z
2019-07-18T17:57:19.937Z
Emmanuel Touzery
<p>You don't need that equation.</p>

<p>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.</p>

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

<pre><code>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, &amp;lens);

// scale points in [-1, 1] domain to actual aperture radius
lens *= apertureRadius;

// 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;
</code></pre>

CC BY-SA 4.0
2018-10-13T20:58:34.520Z
2018-10-13T21:18:01.693Z
2018-10-13T21:18:01.693Z
zeno
<p>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. </p>

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

<p>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?</p>

<p>Here's the link to the code I talked about. 
<a href="https://github.com/petershirley/raytracinginoneweekend/blob/master/camera.h" rel="nofollow noreferrer">https://github.com/petershirley/raytracinginoneweekend/blob/master/camera.h</a></p>

<raytracing>
Defocus blur: Computing the pixel plane distance
Manh Nguyen
# Defocus blur: Computing the pixel plane distance

(Asked by: username_0 on 2018-10-09)

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?

Here's the link to the code I talked about.
<https://github.com/petershirley/raytracinginoneweekend/blob/master/camera.h>

**Tags:** raytracing


---

## 2 answers

### Answer by username_2 on 2018-10-13. Score: 4 | Accepted answer

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
lens *= apertureRadius;

// 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;

```


---

### Answer by username_1 on 2019-07-18. Score: 1

Exactly the same happened to me as it did to you: I was reading the book, and quite following everything that I was reading, until that bit with the defocus blur. My issue was very specifically with the following bits:

```
lower_left_corner: look_from - half_width*focus_dist*u - half_height*focus_dist*v - focus_dist*w,
horizontal: 2.0*half_width*focus_dist*u,
vertical: 2.0*half_height*focus_dist*v

```

It was very clear to me that we should multiply `-w` by `focus_dist`, `-w` being the vector pointing towards the subject. But I couldn't figure out why we should multiply the `u` and `v` vectors by `focus_dist` too. That made no sense to me. Was it about the dimensions of the focus plane or something? I couldn't make sense of it. But removing it made the generated image wrong, so it was really needed.

The insight I had was 'where do we get `half_width` from?' And the answer is...

```
let half_height = tan(theta/2.0);
let half_width = aspect * half_height;

```

And NOW everything makes sense! That is obviously wrong, as the code computing `half_height` assumes a distance of 1.0 to the target! Therefore `half_height` was miscalculated in the book, and the author compensated a little later by multiplying by `focus_dist` when using `half_width`...

Therefore the correct code (despite the fact that the original code worked 100%) would be:

```
let half_height = tan(theta/2.0) * focus_dist;
let half_width = aspect * half_height;
// ...
lower_left_corner: look_from - half_width*u - half_height*v - focus_dist*w,
horizontal: 2.0*half_width*u,
vertical: 2.0*half_height*v,

```


---

