 Let's take a look at how the exact algorithm looks like. I have a recursive function where what I'm looking for first is if I have reached the maximum depth that I would like to render. This means that if I say that I will write together 5 bounces, then this is going to be 5. Did I reach this number of bounces? Yes, okay, then just top and return the back part of it. Then what I'm looking for is the nearest intersection. You remember from the previous lecture that this means parametric equations which I solved for p. So what I'm interested in is that I am intersecting a lot of objects and I'm only interested in the very first intersection. And if I didn't hit anything, then I will return a black color because there's no energy going in this through this way. Now, if I have the intersection of the object, I will be interested in the emission and the material of this object. The emission means that if this is a light source, then it's going to have emission and the material can be not retrieved by the idea of diffuse, glossy or some complicated multi-linear material. This I'm going to store on that. What's up next? Well, I would like to construct a new ray because I would face the next ray. I, especially to move right. This will start wherever I hit this object. So if I hit the table, I will create a new ray that starts from the table and I will set the outgoing direction according to some circumstances to find out. Let's say that what they have here, if it says that random unit vector, if you have a sphere ball where the object was hit, and this sounds like a diffuse case for me. So I generate a random unit vector on this and this is going to be the outgoing direction. Now let's add together the elements of the random equation. I have the cosine theta which is the light attenuation. I have the BRDF term and in the BRDF term it seems that here they have also included this cosine theta or which is the light attenuation. This is the albedo of the material. How much light is absorbed and how much is reflected. And then what I would like to do is I would call the very same function that you see in line of code number one. So this is a recursive function. I will start the same process again with a new ray, a new particle form and a new direction. And in the end if I have phrased a sufficient number of rays, then I will exit this recursion and I collect the result of this in this variable that's called reflected. And in the end this is the elegant representation of the random equation, the emission, the lb, plus the integrated function which is the BRDF times this reflected which is all the recursive terms. So this means that I shoot out this ray in the hemisphere and there is going to be many sub-city contences and I add up all this energy into the reflected incoming light. So this is the pseudo-fold. This is not something that you should try to compile or anything like that, but this is what we will code during the next lecture. This is just a schematic overview on what is happening exactly. It's actually very clear. We shoot the ray, we bounce it into the scene and then we hopefully hit the light source at some point. And even if we hit the light source we continue, but hitting the light source is important because this is where the emission term comes from. Let me show you what's going on if we don't hit light sources. So this lb is the emission term of the left side here. So we add this to the end result at every recursion step. And the fundamental question is that if we have a long light path that doesn't hit the light source, we are using completely random sampling. So maybe some smart important sampling. Well, that we never have this emission term. What does this mean? That the variance that we give, we get from the program is going to be zero. So the corollary of this is that only you will get variance, you will get an output from only light ads that hit the light source. If you don't hit the light source, you don't know where the light is coming from so you will return a black pixel. And this is obviously really bad thing because you're computing, turning out samples and samples and samples perhaps on your GPU, but it doesn't return you anything. So it's a very peculiar fact about simple life operating is that if we have a small light source, the convergence of your final result to this lower. Why? Small light sources, more variance, slower convergence. Because we need a random raised hit the light source. It's small, then we want to clear this off. Exactly. So the relative probability of hitting the light source is going to be less for a small light source. And up to the extreme where we have a point light source. And if we have a point light source that we will see that we will be in trouble, because what I would expect from my heart crisis to return something like this, what we imagine a point light source in here. But this is not what we will end up with. So I would expect it to return the correct results. Many people have reported many forums on the internet that, hey, I implemented it, but this is what I got. This doesn't work at all. All these pronounce law, smells law, totally turn over reflection, Monte Carlo intervention, for a black image. I mean, I could generate this with five lines of C++. Why do we even part? We will get nothing. Why is that? Point light source, black image. Why? Yes. Exactly. Exactly. So the point represents a location in mathematics. It does not have error. So technically, it is the same. Getting a point by source is impossible, because this is the same as what you would study in statistics and how we can deal with that. If you have one number in a continuous scale, what is the probability of hitting this number? 0, because that's the point. It has low surface error. It's infinitely small. We cannot hit it. So this is the reason of your black image. You read the coordinates on the internet, you don't find plenty of this. Now, we could also sum up our findings in the internet meme style, if you will. So if you would like to compute our face into the point by source, without the technique that is called next event estimation, then you usually will expect a wonderful image. But this is what you're not getting. Now, the first question is obviously, how will your work out this? What we can do is that every time we get something some object in the scene, we get the views for anything that in not light sources, we compute the direct effect of the light source on this point in the scene. So this is a schematic to show what is going on. So I start from the viewer, I hit this sphere, and I don't just start tracing the new gray outwards, but I will connect this point to the light source, and I will compute the direct illumination. This is the schematic for path tracing without the next event estimation, and this is with next event estimation. So at every station, I connect to the light source. In this case, this is actually included. In this case, this is the data ball, and in the third bounce, you will get some contributions from the light source. The question is, how do we do this exactly? Well, this was the topic of assignment 0. So the formula that you see in assignment 0 is exactly the very same thing as what you should be using. What was in there? Well, what we were interested in is that there was a term with the 4 pi, because if you have a light source that's a sphere, then what we were interested in, how much variance is emitted in one direction. So then you will need to divide by the area of the surface, which is a division by 4 pi, and there's going to be the attenuation curve, which is all squared. Same as in the gravitational law, or in the law of electric fields. It means that the further away I am from the light source, the less light is going to be. This is a really good technique because of multiple reasons. One of the reasons is that you will get contributions from every bounce during the computing to light. Before I proceed, I would like to tell you that here is L, we are talking about this LB, the emission curve. We are adding these parts of this emission curve in every bounce. So if I hit P1, I add this something. If I hit P2, I add this something. If I hit P3, then I also add this something. But when I hit the light source, I don't add the emission curve anymore because I will be adding it again. So this 1LB that you will add, when you hit the light source by chance, this is distributed into individual complexes. Why is this great? One, you can rather point light sources because the direct effect you can actually measure, but you cannot hit the light source itself. So let's get to 2, you will have less variance because it's not like I either hit the light source or I don't. I statistically always hit the light source unless there are recruiters. So I'm adding many samples with small variances, not one sample and lottery because you either win or you don't get anything. So I can lower the variance which means that my images will converge faster. And the other thing is that because there are contributions of every bounce, I can separate directly and redirect the illumination. So a lot of people do this in the industry because the movie industry is nowadays using power tracing. I cannot say that like as a whole and composing something statement, but for instance, this movie is now using global illumination. What do you think that most of the power tracing? Why? Because it looks insanely good and it is very simple. And it took them more than 20 years for them to replace their old systems which they really liked. And now they are using global illumination. And it has taken a long time but the benefits of global illumination are now 2D to S. And what they are doing is that they get a physically based result but this is not always what the artist is looking for. And if you have work together with artists, then they will say, okay, you have computed a beautiful image but I would like the shadows to get a bit brighter. The engineers say that, well, this is not possible. I can put it what will happen in physical reality and that's it. But the artists are interested in physical reality. They are interested in their own thoughts and their own artistic vision. And they would like to change the shadows. So you could technically make one of the light sources brighter and then the shadows would get brighter. But then the artist says, hey, but don't change anything else in the scene. Just change the shadows. And then you could pull out your knowledge of the rendering equation and look. The radius coming out from this point, you can't only surround this. So you cannot just make something brighter and the nearby things will also get brighter. You cannot circumvent that. What you can do with the next event destination is that you take, you generate an image from the first box. So you will get one image which is which you deposit the radiance that you measured in P1. That's an image. And then you create another image which will only contain the second file, P2 and upwards. So you would have multiple images and you could technically just end up all these images with simple addition and you would get physical reality. But if the artist says that I want stronger interactive illumination, then you would grab this buffer, this image that holds up the second and the higher or the advances. And you could do some Photoshop or you could do whatever you want without touching any of these. So you have a nice separation for directing the director illumination, moving the street. But moving the street, they love it. They are playing it all the time. And later you will see some algorithms that behave differently on the interactive illumination and differently on the interactive illumination. You can only do that if you separate these terms. So let's see path tracing now with next event estimation. I have the very first bounce. And before I continue my array, I will send the classical super, super classical shadow array to the light source. I'm going to choose the point of the light source. And I will add this direct contribution of the light source to this point. And then I continue. Let's go back to the terms. Sorry, we use many terms for the very same thing. This is why I write in all of these terms. Because if you do the forms, if you do the papers, we will see these terms and they all mean the same. So explicit light something, next event estimation, the very same thing. So I continue my array. And I also hit the light source with the shadow array. And then I continue on and on and on. And imagine that this third one is an outgoing rate that actually hits the light source. And if I do, I don't add the dark part in there because I did in the previous one. It's very important. Now you have seen the results for point light source, nothing versus something that's pretty happy. But even if you have a reasonably big light source, like the light, light side light source, I told you that you can't have variance suppression effect as well. So this is some amount of sample prefix. I think it's two maybe three samples to pixel. So this means that I grab one pixel and I send three ways to it. So it's three. Now, this you can do in two different ways because if you start to use renders, then you will see how this exactly happens. Some renders are rendering tiles. So what they do is that they start with some pixels. And if you say I want 1000 samples for pixel, then it will start take one or four or whatever number of threads you have on your machine. It would take four or four pixels and it will shoot more and more samples through. And after it got to 1000 samples, it will start and show you a really good and conversion pixel. And what we call progressive rendering is the opposite. You pick one pixel, you shoot or rate it, but only one. And then you go to the next. And then you go to the next. And then you will see an image that has some amount of noise and progressively you will get less and less noise. So this is what you see here is progressive render. Now, no next event estimation. So we only get contributions if we hit this light source in there. If we don't, you will get a black sample. Now, look closely. This is with next event estimation. So there's a huge difference. Such a simple technique can speed up the rendering of medicines with orders of brain. You can also play with this program by the way. This is implemented on shader toy. So when you read this at home, just click on the link and play with it. It's amazing.