# Depth of Field in Path Tracing: What do I do with the secondary ray?

How can I implement Depth of Field in my Path Tracer? I have read this article 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?

• Related question on depth of field (not a duplicate as that is focused on approximations rather than path tracing, but gives some insight). Dec 3, 2016 at 10:26
• Related question on lens 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. Dec 3, 2016 at 10:36
• 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. Dec 3, 2016 at 14:28

# 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.

• 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? Dec 3, 2016 at 15:55
• 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. Dec 3, 2016 at 16:31
• 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. Dec 3, 2016 at 16:32
• Do you think you could explain the secondary ray? Dec 3, 2016 at 16:54
• @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. Dec 4, 2016 at 0:57