3 ms·
Alright, it's been a few years since I did anything with ray tracing so I'm a bit rusty, the following probably contains a few errors but it should give you a g
by crote 4y ago
Alright, it's been a few years since I did anything with ray tracing so I'm a bit rusty, the following probably contains a few errors but it should give you a general idea.
Let's start with a ray. It begins at the camera, travels through some point in the viewport, and goes on its merry way through the scene. It does so in a straight line, and Interesting Stuff happens when the ray hits some object. In practice by far the hardest part is figuring out what you are going to hit - and doing so quickly. But we're going to completely ignore that here for simplicity.
The most primitive way of ray tracing is to draw a pixel colored by the object you hit. The ray hit a red ball? Pixel is red. The ray hit a green cube? Pixel is green. The ray hit nothing? Pixel is black. But this creates solidly colored objects, so it is pretty useless. One important thing to note is that you are done after a single ray.
Let's add some basic physics to it. Perfect mirrors are easy: the angle of incidence is equal to the angle of reflection, so we just alter the direction of the ray and continue tracing it. Transparent objects are easy too: Snell's Law will trivially tell you how the ray should be adjusted to account for different refractice indices - sometimes it gets bent, and sometimes it gets reflected. And we just got things like lenses implemented! Adjusting for things like colored mirrors or tinted water is left to the reader. Opaque objects are slightly more complicated. They get their light from, well, light sources. To do this, we cast secondary "shadow rays": one ray from each point light source to the point on the opaque object we hit. If nothing is in the way we use a shading algorithm like a Phong shader to determine the influence of that light source at that angle on that object. The important takeaway here is that the ray tracing is now recursive. We have now arrived at what we call "Whitted-style ray tracing".
There are two important things to keep in mind for Whitted-style ray tracing. First, you have to shoot a shadow ray to every light source. This gets expensive really fast. Second, while you can create objects which do multiple things at once (somewhat opaque, somewhat glassy), this results in even more rays. In practice, hitting a surface point spawns 1) a shadow ray for each light source 2) a reflection ray 3) a ray transmitted through the material. And the reflected and transmitted rays will go on to hit even more surfaces - leading to an exponential growth in the number of rays. And the final result isn't even physics-accurate!
Finally, path tracing. We start off the same, but when we hit an object we choose a single random path to continue on, until we finally hit a light source to terminate on. The key point here is that the continuation vector is a weighted random value of a probability function: if enough rays hit this object, the average result will eventually approximate the integral of this function. Similarly, if an object does multiple things, like both reflection and transmission, we randomly choose one of them to continue with. This means we can simulate complicated physics without generating an exponential amount of secondary rays. The only downside is that you need to trace a lot of paths to get a good result: a single path can end up with a heavily biased result due to the randomness rolling dice in a silly way, which ends up generating noise in the output.
----------------------
In summary, ray tracing recursively generates secondary rays to exactly calculate some laws of physics, leading to an explosion of the amount of secondary rays you need to handle, and poor results because you simply cannot implement all physics correctly. Path tracing generates a predetermined and fixed amount of rays randomly going through the scene in an effort to eventually approximate pretty much all laws of physics, but you need a lot more primary rays to get a good approximation and thus a noise-less result.
I followed the Advanced Graphics course at Utrecht University a few years ago, taught by the amazing Jacco Bikker. It seems last year's recordings are available (in English!), so if you want to know more check them out at http://www.cs.uu.nl/docs/vakken/magr/2021-2022/index.html http://www.cs.uu.nl/docs/vakken/magr/2021-2022/index.html