<h1>The missing step</h1>

<p>If you already understand how to generate a secondary ray, then you have already grasped the difficult part. All you need to do now is find the colour that this secondary ray results in. This is exactly the same process as using the primary ray to find a colour, in basic ray tracing.</p>

<p>After repeating this for a large number of secondary rays, the average of all those colours is the colour you assign to the pixel in question (you don't use the primary ray - its only purpose is generating the secondary rays). This is the only difference from basic ray tracing.</p>

<h1>Why it works</h1>

<p>My personal difficulty when trying to understand this was that I was trying to understand how the light gets from the different points on the lens back to the eye/camera. The thing to understand is that this is only an approximation of a lens, and the rays don't need to consider the eye/camera at all.</p>

<p>The lens is being approximated by a disc.</p>

<p>All these different secondary rays show you what you would see from different points on that disc, if you looked directly towards a certain point (the focal point).</p>

<ul>
<li>For an object at the focal point, all these different viewpoints will see the same point on the object, and the object will look sharply defined.</li>
<li>For an object behind the focal point, all these different viewpoints will see slightly different positions on the object, and some of them will miss the object and see what is to the side of it. The edges of the object will be blended with the colour of whatever is to the side of it.</li>
<li>For an object in front of the focal point, again the different viewpoints will see slightly different positions on the object, giving blurred edges.</li>
</ul>

<p>If a primary ray hits an object at the focal distance, but there is a closer object which is not quite in the way, but almost, then some of the secondary rays will hit the closer object before they reach the focal point. This will give a blurred edge to the closer object, partially obscuring the object at the focal point, even though that object itself is in sharp focus.</p>

<p>All of this is as desired. To this level of explanation, this mimics the effect of a real lens, without having to consider refraction at all.</p>

CC BY-SA 3.0
2016-12-03T15:55:17.323Z
I have heard about a term 'jitter matrix' in a few DOF tutorials. I have no clue what it's for and how it comes to play in Depth of Field. Could you explain what a jitter matrix is and if I need it here?
Arjan Singh
CC BY-SA 3.0
2016-12-03T16:31:45.933Z
For DOF, you just need to be able to randomly choose points on the disc. If you just choose these uniformly randomly then the method will work. If instead you use a more advanced method of choosing random points then you may be able to get the same quality image with fewer points (speeding up the process of rendering an image). "Jitter matrix" is a way of choosing points with a different random distribution, and you don't need it in order to make depth of field work - it's just a refinement you could add later. It's also applicable to any other situation where you need random points.
trichoplax is on Codidact now
CC BY-SA 3.0
2016-12-03T16:32:54.447Z
I've deliberately kept to just what is essential to make depth of field work, to avoid cluttering the answer with surplus information. You could ask a separate question if you run into any difficulties with different random distributions.
trichoplax is on Codidact now
CC BY-SA 3.0
2016-12-03T16:54:58.993Z
Do you think you could explain the secondary ray?
Alan Wolfe
CC BY-SA 3.0
2016-12-04T00:11:31.627Z
@AlanWolfe since the question author mentions already understanding secondary rays, I've tried to just focus on the specific aspect required. Perhaps secondary rays would make a separate question?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-12-04T00:25:54.193Z
Yeah your question answers the body of the question but not the title. As someone asking the question as listed in the title, your answer doesn't help me. I wonder if we ought to edit the question title or something...
Alan Wolfe
CC BY-SA 3.0
2016-12-04T00:57:42.320Z
@AlanWolfe That's a good point. The reason the question was asked is because general descriptions of depth of field are too much information, and just that one narrow topic needed to be addressed alone. I've edited the title to try and better describe the question. The title is always going to be a short approximation, but search engines consider the body too, so it should still be found.
trichoplax is on Codidact now
CC BY-SA 3.0
2016-12-03T15:23:44.857Z
2016-12-03T15:36:38.837Z
2016-12-03T15:36:38.837Z
trichoplax is on Codidact now
<p><strong>How can I implement Depth of Field in my Path Tracer?</strong> I have read this <a href="https://courses.cs.washington.edu/courses/csep557/99au/projects/trace/depthoffield.doc" rel="nofollow noreferrer"><strong>article</strong></a> which explains the theory behind everything &amp; I already have a good understanding of what this effect needs to achieve based on previous experience with a DSLR but <strong>I am a little confused on how to code everything although I understand a most of the calculations involved</strong> <em>(after reading the article linked above)</em>. </p>

<p>I understand how to calculate the focal point, focal distance and the secondary ray. But I'm confused on how to put this all together to create the 'blur' that is needed. I haven't found much online so I was hoping that someone here could explain that with some code.</p>

<p>Could someone show me their implementation? </p>

<p><a href="https://i.sstatic.net/nVWIf.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nVWIf.jpg" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-12-03T10:26:58.263Z
[Related question on depth of field](http://computergraphics.stackexchange.com/questions/66/how-is-depth-of-field-implemented) (not a duplicate as that is focused on approximations rather than path tracing, but gives some insight).
trichoplax is on Codidact now
CC BY-SA 3.0
2016-12-03T10:36:30.130Z
[Related question on lens model for path tracing](http://computergraphics.stackexchange.com/questions/246/how-to-build-a-decent-lens-camera-objective-model-for-path-tracing). Near to being a duplicate, so I recommend you make the question more specific by explaining what you already understand, so we can see where any confusion is being introduced.
trichoplax is on Codidact now
CC BY-SA 3.0
2016-12-03T14:28:30.350Z
I understand how to calculate the focal point, focal distance and the secondary ray. But I'm confused on how to put this all together to create the 'blur' that is needed. I haven't found much online so I was hoping that someone here could explain that with some code.
Arjan Singh
<rendering><physically-based><image><depth-of-field><camera>
Depth of Field in Path Tracing: What do I do with the secondary ray?
Arjan Singh
# Depth of Field in Path Tracing: What do I do with the secondary ray?

(Asked by: username_0 on 2016-12-03)

**How can I implement Depth of Field in my Path Tracer?** I have read this [**article**](https://courses.cs.washington.edu/courses/csep557/99au/projects/trace/depthoffield.doc) which explains the theory behind everything & I already have a good understanding of what this effect needs to achieve based on previous experience with a DSLR but **I am a little confused on how to code everything although I understand a most of the calculations involved** *(after reading the article linked above)*.

I understand how to calculate the focal point, focal distance and the secondary ray. But I'm confused on how to put this all together to create the 'blur' that is needed. I haven't found much online so I was hoping that someone here could explain that with some code.

Could someone show me their implementation?

[![enter image description here](https://i.sstatic.net/nVWIf.jpg)](https://i.sstatic.net/nVWIf.jpg)

**Tags:** rendering, physically-based, image, depth-of-field, camera

> **Comments:**
> **username_1**: [Related question on depth of field](http://computergraphics.stackexchange.com/questions/66/how-is-depth-of-field-implemented) (not a duplicate as that is focused on approximations rather than path tracing, but gives some insight).  
> **username_1**: [Related question on lens model for path tracing](http://computergraphics.stackexchange.com/questions/246/how-to-build-a-decent-lens-camera-objective-model-for-path-tracing). Near to being a duplicate, so I recommend you make the question more specific by explaining what you already understand, so we can see where any confusion is being introduced.  
> **username_0**: I understand how to calculate the focal point, focal distance and the secondary ray. But I'm confused on how to put this all together to create the 'blur' that is needed. I haven't found much online so I was hoping that someone here could explain that with some code.  

---

## 1 answers

### Answer by username_1 on 2016-12-03. Score: 5 | Accepted answer

# The missing step

If you already understand how to generate a secondary ray, then you have already grasped the difficult part. All you need to do now is find the colour that this secondary ray results in. This is exactly the same process as using the primary ray to find a colour, in basic ray tracing.

After repeating this for a large number of secondary rays, the average of all those colours is the colour you assign to the pixel in question (you don't use the primary ray - its only purpose is generating the secondary rays). This is the only difference from basic ray tracing.

# Why it works

My personal difficulty when trying to understand this was that I was trying to understand how the light gets from the different points on the lens back to the eye/camera. The thing to understand is that this is only an approximation of a lens, and the rays don't need to consider the eye/camera at all.

The lens is being approximated by a disc.

All these different secondary rays show you what you would see from different points on that disc, if you looked directly towards a certain point (the focal point).

* For an object at the focal point, all these different viewpoints will see the same point on the object, and the object will look sharply defined.
* For an object behind the focal point, all these different viewpoints will see slightly different positions on the object, and some of them will miss the object and see what is to the side of it. The edges of the object will be blended with the colour of whatever is to the side of it.
* For an object in front of the focal point, again the different viewpoints will see slightly different positions on the object, giving blurred edges.

If a primary ray hits an object at the focal distance, but there is a closer object which is not quite in the way, but almost, then some of the secondary rays will hit the closer object before they reach the focal point. This will give a blurred edge to the closer object, partially obscuring the object at the focal point, even though that object itself is in sharp focus.

All of this is as desired. To this level of explanation, this mimics the effect of a real lens, without having to consider refraction at all.

> **Comments:**
> **username_0**: I have heard about a term 'jitter matrix' in a few DOF tutorials. I have no clue what it's for and how it comes to play in Depth of Field. Could you explain what a jitter matrix is and if I need it here?  
> **username_1**: For DOF, you just need to be able to randomly choose points on the disc. If you just choose these uniformly randomly then the method will work. If instead you use a more advanced method of choosing random points then you may be able to get the same quality image with fewer points (speeding up the process of rendering an image). "Jitter matrix" is a way of choosing points with a different random distribution, and you don't need it in order to make depth of field work - it's just a refinement you could add later. It's also applicable to any other situation where you need random points.  
> **username_1**: I've deliberately kept to just what is essential to make depth of field work, to avoid cluttering the answer with surplus information. You could ask a separate question if you run into any difficulties with different random distributions.  
> **username_2**: Do you think you could explain the secondary ray?  
> **username_1**: @username_2 since the question author mentions already understanding secondary rays, I've tried to just focus on the specific aspect required. Perhaps secondary rays would make a separate question?  
> **username_2**: Yeah your question answers the body of the question but not the title. As someone asking the question as listed in the title, your answer doesn't help me. I wonder if we ought to edit the question title or something...  
> **username_1**: @username_2 That's a good point. The reason the question was asked is because general descriptions of depth of field are too much information, and just that one narrow topic needed to be addressed alone. I've edited the title to try and better describe the question. The title is always going to be a short approximation, but search engines consider the body too, so it should still be found.  

---

