title: Bidirectional path tracing, algorithm explanation
body: <p>I'm trying to understand path tracing. So far, I have only dealt with the very basis - when a ray is launched from each intersection point in a random direction within the hemisphere, then again, and so on recursively, until the ray hits the light source. As a result, this approach leads to the fact that in the case of small light sources, the image is extremely noisy.</p>
  <p>The following images show the noise level depending on the number of samples (rays) per pixel.
  <a href="https://i.stack.imgur.com/SKrlM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SKrlM.jpg" alt="Noise level" /></a></p>
  <p><em>I am also not sure that i am doing everything correctly, because the &quot;Monte Carlo&quot; method, as far as I understand, implies that several rays are launched from each intersection point, and then their result is summed and averaged. But this approach leads to the fact that the number of rays increases exponentially, and after 6 bounces reaches inadequate values, so i decided that it is better to just run several rays per pixel initially (slightly shifted from the center of the pixel in a random direction), but only 1 ray is generated at each intersection. I do not know whether this approach corresponds to &quot;Monte Carlo&quot; or not, but at least this way the rendering does not last forever..</em></p>
  <p><strong>Bidirectional path tracing</strong></p>
  <p>I started looking for ways to reduce the amount of noise, and came across bidirectional path tracing. But unfortunately, i couldn't find a detailed explanation of this algorithm in simple words. All I understood is that the rays are generated from both the camera and the light sources, and then there is a check on the possibility of connecting the endpoints of these paths.</p>
  <p><a href="https://i.stack.imgur.com/2eN5y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2eN5y.png" alt="enter image description here" /></a></p>
  <p>As you can see, if the intersection points of the blue ray from the camera and the white ray from the light source can be freely connected (there are no obstacles in the connection path), then we can assume that the ray from the camera can pass through the points y1, y0 directly to the light source.</p>
  <p>But there are a lot of questions:</p>
  <ol>
  <li>If the light source is not a point, but has some shape, then the point from which the ray is launched must be randomly selected on the surface of this shape? If you take only the center - then there will be no difference from a point light source, right?</li>
  <li>Do i need to build a path from the light source for each path from the camera, or should there be only one path from the light source, while several paths (samples) are built from the camera for one pixel at once?</li>
  <li>The number of bounces/re-reflections/refractions should be the same for the path from the camera and the light source? Or not?</li>
  </ol>
  <p>But the questions don't end there. I have heard that the bidirectional trace method allows you to model caustics well (in comparison with regular path tracing). But I completely did not understand how the method of bidirectional path tracing can somehow help for this.</p>
  <p><strong>Example 1</strong></p>
  <p><a href="https://i.stack.imgur.com/Bxbi6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bxbi6.png" alt="enter image description here" /></a></p>
  <p>Here the path will eventually be built, but the number of bounces will be extremely large, so no caustics will work here, despite the fact that the ray from the camera is directed almost to the same point where the path of the ray from the light source ends.</p>
  <p><strong>Example 2</strong></p>
  <p><a href="https://i.stack.imgur.com/mykpN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mykpN.png" alt="enter image description here" /></a></p>
  <p>Here the path will not be built, because there is an obstacle between the endpoints of the paths, although it could be built if point x3 was connected to point y1, but according to the algorithm (if I understand everything correctly), only the last points of the paths are connected.</p>
  <p><strong>Question:</strong></p>
  <p>What is the use of such an algorithm, if in a significant number of cases the paths either cannot be built, or are unnecessarily long? Maybe I misunderstand something? I came across many articles and documents where this algorithm was somehow described, but mostly it was described mathematically (using all sorts of magical terms like biased-unbiased, PDF, BSDF, and others), and not.. algorithmically. I am not that strong in mathematics and all sorts of mathematical notation and wording, I would just like to understand WHAT TO DO, how to implement it correctly in the code, how these paths are connected, in what order, and so on. This can be explained in simple words, pseudocode, right? I would be extremely grateful if someone would finally shed some light on all this.</p>