# TU Wien Rendering #7 - Ray-Sphere Intersection

## Метаданные

- **Канал:** Two Minute Papers
- **YouTube:** https://www.youtube.com/watch?v=bQKy3N4TshU
- **Дата:** 29.03.2015
- **Длительность:** 10:42
- **Просмотры:** 12,414

## Описание

We learn how to compute where a ray of light intersects a sphere. The advantages of parametric equations over the classical implicit formulations (in this context) are also discussed. Turns out there is some beauty to be seen and appreciated during the process!

About the course:
This course aims to give an overview of basic and state-of-the-art methods of rendering. Offline methods such as ray and path tracing, photon mapping and many other algorithms are introduced and various refinement are explained. 

The basics of the involved physics, such as geometric optics, surface and media interaction with light and camera models are outlined. 

The apparatus of Monte Carlo methods is introduced which is heavily used in several algorithms and its refinement in the form of stratified sampling and the Metropolis-Hastings method is explained. 

At the end of the course students should be familiar with common techniques in rendering and find their way around the current state-of-the-art of the field. Furthermore the exercises should deepen the attendees' understanding of the basic principles of light transport and enable them to write a simple rendering program themselves.

These videos are the recordings of the lectures of 2015 at the Teschnische Universität Wien by Károly Zsolnai and Thomas Auzinger

Course website and slides → http://www.cg.tuwien.ac.at/courses/Rendering/
Subscribe → http://www.youtube.com/subscription_center?add_user=keeroyz
Web → https://cg.tuwien.ac.at/~zsolnai/
Twitter → https://twitter.com/karoly_zsolnai

## Содержание

### [0:00](https://www.youtube.com/watch?v=bQKy3N4TshU) <Untitled Chapter 1>

okay so why don't we start uh after being immersed into the beauty of Fel law Fel equation and sn's law we are going to continue with uh kind of putting together a new rate tracing program and we know all about airglass 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 always the origin this is where we start from D is a direction Vector this is where the ray uh is 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 one then that's unit distance now uh 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 scaler it's a number it tells you the distance that is travel 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

### [1:40](https://www.youtube.com/watch?v=bQKy3N4TshU&t=100s) Implicit Equations

equations so this could be an equation of a surface f of x and y equal Z for instance this is an example this would be the implicit equation of a

### [1:51](https://www.youtube.com/watch?v=bQKy3N4TshU&t=111s) Implicit Equation of the Sphere

sphere and this is an equation so basically you can say that uh whatever X and Y does 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 X Y but probably vectors so uh 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 in 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

### [3:02](https://www.youtube.com/watch?v=bQKy3N4TshU&t=182s) Compute the Intersection of the Ray and the Sphere

compute the intersection of AR Ray and a 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 following I have a sphere and the Ray and it is possible that the ray hits the sphere in two different points well what 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 hitting it at the very side while this is a rare site 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 get 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 p is the P's are going to be the point points on the surface of the sphere and the C is the center R is obviously the radius this is the equation of the ray uh 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 plus TD minus C * = r 2 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 * TD so there's going to be something like t^2 d^2 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 scaler 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 on what this smells like yes that's that's going to be correct yes it's a polinomial equation of uh what is it called degree to exactly so this is but I have to smell it first so yes indeed that's a quadratic

### [6:06](https://www.youtube.com/watch?v=bQKy3N4TshU&t=366s) Quadratic Equation

equation so uh I have t^ s t and the scalar term equals zero what are the coefficients well simple uh the t is going to be d^ squ uh what about the B I mean not the T but the a we apologize uh the B is going to be the 2 the 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 it possible that this equation has two solutions someone helped me out I missed some courses at kindergart so I don't know anything about this is it possible to get two solutions for this I can't hear anything okay excellent this is the interesting part during a lecture because 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 excellent okay cool uh well it's simple if this B ^2 - 4 a c is larger than zero then this 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 walk so much during the course can you hear this can 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 equal Z excellent okay so if this term is zero then I'm uh adding zero and subtracting zero this is the same thing so t1al T2 very simple and it is also possible that we have no real solution uh 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 happen 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 min-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 uh 2 * 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 the 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 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 uh 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 this is computer science so we can discard all this information

---
*Источник: https://ekstraktznaniy.ru/video/15005*