TU Wien Rendering #7 - Ray-Sphere Intersection

Lecture segment explaining parametric representation of rays (R(t)=O + tD with D usually unit-length) and deriving ray-sphere intersection by substituting R(t) into the sphere implicit equation (||P-C||^2 = r^2). The substitution produces a quadratic in t; the discriminant (b^2-4ac) yields two, one (tangent), or no real intersections. Negative t solutions are behind the eye and discarded.
Chapters
Okay, so why don't we start. After being immersed into the beauty of Fresnel's equation and Snell's law, we are going to continue with kind of putting together a new ray tracing problem. And we know all about hourglass interactions and things like that, but we, for instance, don't know what a representation of a ray of light could be. So let's go with this. So array is basically starting somewhere and it is going somewhere. That's basically it. This is what I have written here mathematically. So this is a parametric equation. We'll talk about this in a second. So O is the origin. This is where we start from. D is a direction vector. This is where the ray is. This is where the ray is going. And T is the distance that it had gone. That's basically it. So if T is a large number, then the ray had traveled a lot. And If T is 1, then that's unit distance. Now, we are always going to talk about vectors of unit length, if we are talking about direction vector. And most vectors are normed in global illumination anyway. But I would like to state this because now this T is meaningful. If D is of unit length, then T is a scalar. It's a number. It tells you the distance that is traveling. And this notation is a bit weird for many people because this R depends on T. And if you come from the regular math courses, most of what you encounter is implicit equations. So this could be an equation of the surface F of X and Y equals zero. For instance, this is an example. This would be the implicit equation of a sphere. And this is an equation. So basically you can say that whatever X and Y that satisfy this equation is going to be the point of this sphere. And this is going to be this collection of points that gives you a sphere. And parametric equations don't look like that. So with these parametric equations, you can see that the X coordinate I can dig out from a function that depends on T. The Y coordinate I can dig out from a different, perhaps a different function, but it also depends on p. And I can write up this whole thing as a vector form. So I'm not talking about xy, but probably vectors. So let's see an example. The equation of array is such an example that you have seen above. But we're going to play a bit more with this. And the first question is, why are we doing this? Why parametric equations instead of implicit functions? Well, you will see soon enough when we encounter a problem, and this is going to be easy to solve. With parametric equations. So this is a secret. And now let's try to compute the intersection of array and the sphere. So I cast array and I would like to know which is the first object that I hit in the scene. And if I have a scene of spheres, then this is the kind of calculation that I need to do. So the expectations are the I have a sphere and the ray. And it is possible that the ray hits the sphere in two different points. Well, what else is possible? If two hit points are possible, then one hit point is also possible. This is essentially the tangent of a sphere. It is just here at the very side. While this is a rare sight, but this still exists. And obviously it is possible that the ray does not hit the sphere at all. So we have again listed our expectations before doing any kind of calculation. And you will see that this will make things much more beautiful. So the solution for this whole problem should be some kind of mathematical something that can give me two solutions, one solution, or maybe no solutions. If I do the intersection routine and I get whatever else, then this should be incorrect. So this is what I expect to see. There's possibility of two, one or zero solutions. Well, this is the equation of a sphere. The P's are going to be the points on the surface of the sphere, and the C is the center of the sphere and R is obviously the radius. This is the equation of the ray. We have to mix these two together in some way in order to get an intersection. What I'm going to do is I'm going to substitute this R of T in the place of P. So what it will give Me is O +TD -C times O +TD -C equals R squared. So this is a big multiplication between the two parentheses. And if I do this actual multiplication, then I will see that there's going to be a term which gives me the TD times td. So there's going to be something like T squared D squared here. And there's going to be another term where the O minus C is multiplied with the TD on the other side. And this happens twice because both sides. And the rest is going to be a scalar term because O minus C, I'm going to multiply with O minus C. So this is going to be a scalar. I don't see T in there. And this is already quite interesting because if we smell this equation, what does this equation smell like? Raise your hand if you have some idea of what this smells like. Yes, that's going to be correct. Yes, it's a polynomial Equation of what is it called? Degree two. Exactly. Exactly. So this is. But I have to smell it first. So yes, indeed, that's a quadratic equation. So I have T squared T and the scalar term equals zero. What are the coefficients? Well, simple T is going to be B squared. What about the B? I mean not the T but the A. We apologize. The B is going to be the 2D O minus C, because this is what I multiply T with and the scalar term is going to be all the rest. Okay, so this should be very simple to solve. I have as a solution a possible T1 and a T2 that satisfies this equation. And now the question is, is it possible that this equation has two solutions? Someone help me out. I missed some courses at kindergarten, so I don't know anything about this. Is it possible to get two solutions for this? I can't hear anything. Okay, excellent, excellent. This is the interesting part during the lecture because that the teacher asks something and no one answers. And this can mean two things. One thing is that no one knows the answer. And the second is that, yeah, anyone knows the answer. And it's so trivial that no one wants to look like an idiot, so no one says anything. And I would imagine that maybe this is the second case. So is it possible to get two solutions for this? Yes, yes. Excellent. Okay, cool. Well, it's simple. If this b squared minus 4ac is larger than zero, then the second term under the square root is going to be a number, some real number. And therefore this is going to be T1 is minus B plus this number T2 is A minus B minus this number. And therefore this is going to be two different solutions. Maybe I shouldn't go so much during the course. Can you hear this? Can you take them off? Well, not always. I could take them off. Yes, and look like an academic. Okay, well, is one solution possible? I still cannot hear anything. Yes. Okay, cool. When someone tell me in the same term equals zero. Excellent. Okay, so if this term is zero, then I'm adding zero and subtracting zero. This is the same thing. So P1 equals P2. Very simple. And it is also possible that we have no real solution. If this square root term, I mean the term under the square root is less than zero. Excellent. So this is quite beautiful because we listed our expectations and it indeed needs to look like something that can give me two one or zero solutions. And if I do the math, this is exactly what happens. So this is the beauty of the whole thing. And let's imagine that I've solved this equation and I got the result that T1 is 2 and T2 is minus 2. And now what if I told you that these T's mean distances, so I'm solving the parametric equations in a way that this T means a distance, so it means that the first intersection is two times the unit distance, therefore two in the front. There could be a solution, which is a classical case for a quadratic equation where I get a second solution that is minus something. What does it mean? Yes, I think we can dismiss that because it's behind our eyes. So we don't really care about that, do we? Precisely. Precisely. So it is possible that the race starts in the middle of the sphere, and then this is indeed a perfectly normal thing to happen, that there's one intersection in front of us and there's one intersection to our backs, and obviously we don't care about it too much. And if we find a solution like this, we discard it. Indeed. So we're studying computer science and we are not studying politics, because if we would be studying politics, we would be interested in what happens behind our backs. But this is computer science, so we can discard all this information.










