Monte Carlo Path Tracing using Progressive Rendering
Language: C++, Middleware: OpenGL
Overview of the project
Motivation
- How to render the scene under global illumination.
Problem which I had
- Raycasting is only able to render direct illumination without having radiocity. Monte Carlo raycasting, which computes possible reflected rays, can global illumination, but very slow.
Which features are included in this project
- Optimizing the computation of rendering equation using Path tracing.
- Used BRDF and Russian roulette to guarantee minimal errors in the rendered scene.
How to solve the problem
- Computed rendering equation using Monte Carlo Path tracing, which computed single path per pixel.
Limitation
- Path tracing can generate physically accurate scene and is faster than raytracing, but still it is not suitable to render scene in real time.
Detail of the project
1.Introduction
Path tracing, unlike ray tracing, generates realistic scenes which is lit by global illumination. The algorithm of Path tracing is to integrate all energy in unit area on the object (Radiance). Due to the fact that it considers perfect specular diffuse, this computation incredibly expensive. Even though single ray started from the eye(camera), it will be ended up a host of rays because in order to decide colour of the single point on the object, thousands of rays arriving to the point have to be traced in every reflection step.
Rather than doing this, the path tracing algorithm shoots several rays from the camera and each ray will be reflected different direction and the colour of the point will be decided based on reflectance function (BRDF). Namely, if we have enough number of ray, we can get realistic scene.
2.Rendering Equation
Rendering equation can be written as following,
where,
This equation tells us light transports which the radiance on the surface to another is the sum of emitted radiance and reflected radiance. In the scene, large amount of rays travels around and the point on the surface is lit by a number of incoming rays, which are reflected from other surface or come from light source.
This equation tells us light transports which the radiance on the surface to another is the sum of emitted radiance and reflected radiance. In the scene, large amount of rays travels around and the point on the surface is lit by a number of incoming rays, which are reflected from other surface or come from light source.
In order to trace ray (incoming light) , we need to integrate all incoming lights. This can be written as following equation.
'f', which is the first term on the right side of equation, is BRDF (Bidirectional Reflectance Distribution Function). BRDF denotes how lights are reflected and can be different depending on the surface material.
3.Path Tracing vs Ray Tracing
In order to render the scene lit by global illumination, the system has to calculated all lights travelling the space in the scene.
Ray tracing integrates all incoming rays arriving at the point on the surface to compute the reflected ray. The more rays are integrated, the more accurate result we can get. For example, let assume that reflected ray is sum of 20 incoming rays, which are sampled in the area on the hemisphere in the image below.
In order to trace the ray reflected 5 times, the number of ray which we need to consider will be 20^5 (320000). In fact, 20 samples for all incoming rays are not enough to render accurate scene. For such a reason, rendering global illumination using ray tracing can be very expensive and take ages.
In order to render the scene lit by global illumination, the system has to calculated all lights travelling the space in the scene.
Ray tracing integrates all incoming rays arriving at the point on the surface to compute the reflected ray. The more rays are integrated, the more accurate result we can get. For example, let assume that reflected ray is sum of 20 incoming rays, which are sampled in the area on the hemisphere in the image below.
In order to trace the ray reflected 5 times, the number of ray which we need to consider will be 20^5 (320000). In fact, 20 samples for all incoming rays are not enough to render accurate scene. For such a reason, rendering global illumination using ray tracing can be very expensive and take ages.
When it comes to Path tracing, it traces only single path per ray, but the ray will be shot several time from light source. In this case, the path can be different whenever the ray hits the surface based on BRDF, which is probability function of reflected direction of ray. So the radiance of the point on the surface will be the sum of radiance of each path.
Since it traces single ray, it is much faster than ray tracing.
Since it traces single ray, it is much faster than ray tracing.
4.Implementation of Path Tracing
In order to render the scene, we need to solve following equation for reflected ray.
In order to render the scene, we need to solve following equation for reflected ray.
Following code is BRDF given direction of incoming light.
In order to integrate the radiance, I used Monte Carlo integration. If we sample graph uniformly for discrete integration, there will be high error where the graph changes rapidly.
In order to integrate the radiance, I used Monte Carlo integration. If we sample graph uniformly for discrete integration, there will be high error where the graph changes rapidly.
So sampling is based on its probability function. In other words, sample more where high probability is in the graph. Doing so, we can get more accurate result.
So we can update rendering equation using Monte Carlo integration.
This can be written as code like below.
Since the ray travels the space as reflecting on the surface, the algorithm traces all reflected rays and computes final colour using weighted sum (because certain amount of energy will be lost in each reflection).
5.Result
Since only single path per ray is considered rather than integration through the space, the algorithm shoots a number of rays to reduce error in rendering result.
Since only single path per ray is considered rather than integration through the space, the algorithm shoots a number of rays to reduce error in rendering result.