Distribution Ray Tracing

 Part 3: Distributed Ray Tracing

Hello everyone, in this post I will be explaining how I extended my ray tracer to a distributed ray tracer. What is distributed ray tracing? It is a method first introduced by a paper in 1984[1]. Distributed ray tracing is a method of ray tracing that solves the some of the problems of classic ray tracing. Some of these problems being unable to render depth of field effect, motion blur, soft shadows and rough reflective surfaces. These problems occur due to nature of classic ray tracing. As you know in ray tracing we trace the light by geometry calculations and as a result we get precise results like sharp objects and sharp shadows. In order to solve this we are going to use multisampling and as bonus we will get a distributed ray tracing with effects such as motion blur, soft shadows, rough reflective surfaces (like brushed metal) and depth of field.

What is Multisamping and Why?

In order to understand multisampling and the problem of aliasing we need to go back to signals and systems. So actually in ray tracing and rendering what we are doing is sampling the lights and object geometry to fill the pixels and generate an image. Unfortunately when sampling we have serious problem to deal with. That problem is aliasing. Aliasing occurs when our samples does not represent the original signal and as a result we can get imperfectly reconstructed signals such as below.

Advanced Ray Tracing lecture slides [2]


One way to solve this is increasing sampling frequency and actually that what we are going to do for multisampling. Unfortunately in rendering we don't know the frequency of the original signal or shape of the original signal so only increasing the sample size(which is also called uniform sampling) would be equivalent to increasing our images resolution. While it solves some problems it is not the best solution.

Advanced Ray Tracing lecture slides [2]


To solve this we will sample our pixels in a random fashion.

Advanced Ray Tracing lecture slides [2]



But randomness also creates a problem which is clustered samples in a pixel.
 
Advanced Ray Tracing lecture slides [2]



Fortunately we have solution for that too we can divide the pixel into equal areas and the we can generate samples in these areas. This is called jittered sampling or stratified random sampling. I have implemented jittered sampling in my ray tracer.

Advanced Ray Tracing lecture slides [2]


As a result I have got following anti-aliased image(left single sample right multisampled).




multisampled(x64) on the right versus single sampled on the left

Now let us move on to cooler benefits of multisampling

Area Lights and Soft Shadows

Up to now my ray tracer was only supporting point lights and as a result I was getting objects with very sharp shadows.
 
Now I will add area lights to my ray tracer. They will be defined by their center position, area and a surface normal to define their orientation. When we are computing their contribution to the objects we will randomly sample point, namely p in below, from it's area and compute the angle between the lights normal and vector from light source to object.

Advanced Ray Tracing lecture slides [2]


In the formula above I is the radiance of the light source and L(x) is the received irradiance for a point x. You may think sampling once may not give very good results but since we are sending hundreds of ray per pixel it gives fine results. Below you can see two scenes one with point light and one with area light and soft shadows.
Cornell box with point light source



Cornell box with area light source


Rough Reflective and Refractive Surfaces

In my old ray tracer I was implementing reflective and refractive objects as they are perfect mirrors or perfectly glossy dielectrics. Now with distribution ray tracing I will do something different when a reflective/refractive object interacts with light I will slightly deviate the light around the perfect reflection point because in real life most objects doesn't reflect light. We have imperfect lights.
Advanced Ray Tracing lecture slides [2]

In each sample our ray will go to a slightly different direction and we will get rough surfaces like below.

Now can have rough cast glasses in our scenes.

Depth of Field

Up to now my ray tracer was rendering all objects very sharp. Again in reality human eyes and cameras have focus distances when they focus on an object at a certain distance they see the other objects slightly blurred. In order to mimic this behavior we could put an dielectric object in front of our camera but actually without doing that we can achieve good results with a simpler trick. What we are going to do is we are going to act like there is an lens in front of camera while there actually isn't.











Advanced Ray Tracing lecture slides [2]

Finally we get defocused objects with depth of field effect.


Motion Blur

Another effect we can have with distributed ray tracing is the effect of motion blur. The effect we get when one object starts moving while we are taking our photograph. Again this method is very simple. All we need to is assigning a time component to our rays and at each sample we will uniformly sample a time value between 0 and 1. If our rays have 0 as time it means the object is in the beginning of it's motion and if it is 1 it means it has finished it's motion. Every other value between these will mean our object is still in motion and we will linearly interpolate our objects position. This way some rays will intersect with the object and some won't.  

Advanced Ray Tracing lecture slides [2]

Advanced Ray Tracing lecture slides [2]


Advanced Ray Tracing lecture slides [2]


Aaaaand voila, we've got the motion blur effect.

A sphere moving away from the camera

Here you go now we have a distributed ray tracer with more realistic effects. Let us combine soft shadows motion blur and rough surfaces with one final scene.





References

2. Ahmet Oğuz Akyüz. Advanced Ray Tracing lecture slides term 2022-2023 fall



Comments

Popular posts from this blog

Putting it all together: BRDFs, Object Lights and Path Tracing

Trace the Ray

Textures