0% found this document useful (0 votes)
57 views

University of Waterloo CS 489: Monte Carlo Methods for Advanced Applications - Path tracer

Uploaded by

dk4dv7vdgg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

University of Waterloo CS 489: Monte Carlo Methods for Advanced Applications - Path tracer

Uploaded by

dk4dv7vdgg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Path Tracer

Gaurav Talreja

Abstract
This project report focuses on the implementation and evaluation of the path tracing
rendering technique for realistic image synthesis in computer graphics. Path tracing is a
probabilistic approach that follows the most probable paths at each intersection based on
material properties to determine pixel intensity accurately. The report discusses the theoret-
ical foundation of path tracing and presents practical details of its implementation. Various
scenes with different lighting conditions and materials are rendered to assess the method’s
accuracy and performance.

I. Introduction
To accurately render a two-dimensional image of a three-dimensional scene, it is essential to con-
sider global illumination information that affects the intensity of each pixel in the image. Traditional
ray tracing approaches employ a tree-like structure of rays, extending from the viewer to the first sur-
face encountered, and then from surface to surface, and eventually reaching the light sources. This
ray traversal scheme allows for the calculation of direct and indirect lighting contributions, such as
reflection and refraction, at each intersection point. However, the sheer number of rays required for
accurate global illumination computation makes this process computationally demanding.
In contrast, path tracing offers an elegant solution to efficiently handle global illumination effects.
Path tracing algorithms focus on simulating the most probable paths of light rays at each surface inter-
section, as determined by the material properties of the surface. Rather than tracing a complex network
of rays, path tracing follows the most likely path for each interaction, considering both the primary
rays from the camera and the secondary rays resulting from reflection and refraction. This stochastic
approach leverages random sampling to simulate the path of light rays throughout the scene, produc-
ing physically plausible results without the need for an extensive ray tree.
Through this paper, we explore various aspects of path tracing and its benefits in achieving realistic
image rendering. We delve into the implementation details of path tracing algorithms, discuss the im-
portance of accurate material appearance models in capturing surface interactions, and investigate the
role of acceleration structures like bounding volume hierarchies (BVH) in speeding up the rendering
process. Furthermore, we analyze possible avenues for future research and optimization in this field.
The insights gained are valuable for practitioners in various domains, where realistic image rendering
is essential, such as gaming, animation, virtual reality, and architectural visualization.

II. Related Work


A. The Rendering Equation
Radiance represents the amount of radiant energy passing through a specific point in space in a given
direction [1]. Radiance (𝐿) at a point 𝑥 in the direction of the unit vector 𝜔 is defined as the radiant
power (Φ) passing through an infinitesimal area ( d𝐴) oriented in that direction, divided by the area
and the solid angle subtended by the area at the point of interest. Radiance is given by

𝐿(𝑥, 𝜔) = (1)
d𝐴 d𝜔
where dΦ is the radiant power passing through the infinitesimal area d𝐴 in the direction 𝜔.
Equation 2, as introduced by Kajiya [2], describes the total incoming light at a point on a surface as
the sum of emitted radiance and reflected radiance from other surfaces. Solving the rendering equation
involves tracing rays from the camera to the scene objects and computing the contributions of light
along their paths.

𝐿𝑜 (𝑥, 𝜔𝑜 ) = 𝐿𝑒 (𝑥, 𝜔𝑜 ) + ∫ 𝑓𝑟 (𝑥, 𝜔𝑖 , 𝜔𝑜 )𝐿𝑖 (𝑥, 𝜔𝑖 )(𝑛 · 𝜔𝑖 ) d𝜔𝑖 (2)


Ω
where 𝐿𝑜 is the outgoing radiance, 𝐿𝑒 is the emitted radiance and 𝑓𝑟 is the BRDF as described in Sec-
tion II.D.
B. Ray Tracing
The concept of ray tracing was first introduced by Whitted in 1980 [3]. In ray tracing, the process
starts by casting rays from the virtual camera through each pixel in the image plane and into the scene.
These rays, known as primary rays or eye rays, represent the paths that light would take from the cam-
era to the scene objects.
When a primary ray intersects an object in the scene, additional rays are generated to simulate the
interaction of light with the surface. These secondary rays can be classified into two main types: re-
flection rays and refraction rays. Reflection rays are used to simulate the phenomenon of reflection,
where light bounces off a surface in a mirror-like manner. Refraction rays simulate the phenomenon
of refraction, where light passes through transparent or translucent materials and changes direction
due to the change in the medium’s refractive index.
To compute the color of each pixel in the final image, ray tracing follows a recursive process. When
a secondary ray intersects another object, new secondary rays are generated again, leading to a poten-
tially infinite number of rays traced for each pixel. However, a limit on the number of bounces (known
as the recursion depth) is usually set to control computational complexity and prevent infinite loops.
The technique is computationally expensive.
C. Path Tracing
In contrast to ray tracing, path tracing consists of following only the most probable path at each in-
tersection. The path tracing process can be summarized as follows:
1) Ray Generation: For each pixel in the image, rays are generated from the camera’s viewpoint.
2) Ray Intersection: The ray is traced into the scene, and its intersection with the closest object is
computed.
3) Material Sampling: When the ray intersects a surface, a material appearance model is sampled
based on the surface’s properties. The material model determines how the surface interacts with light,
such as reflection, refraction, or emission.
4) Scattered Ray Sampling: After interacting with the surface, a scattered ray is sampled based on the
material properties. This ray represents the direction in which the light continues to travel.
5) Recursion: The process of intersection, material sampling, and scattered ray sampling is repeated
recursively for the scattered ray. Each interaction contributes to the overall radiance of the pixel by
accumulating the light that reaches the camera from different paths.
6) Termination: At each recursion step, the ray can be terminated based on various conditions, such
as reaching a light source, exceeding a maximum recursion depth, or hitting a light-emitting surface.
7) Radiance Accumulation: The process of tracing rays and accumulating radiance values is repeated
multiple times, and the results are averaged to obtain the final pixel color.
The number of rays generated per pixel is referred to as the number of samples.
D. Material Appearance
When light interacts with a material, it is reflected and/or transmitted in different spatial distribu-
tions depending on the material microstructure [1].
Reflectance refers to the ability of a surface to reflect light incident upon it. It is a general term used
to describe the ratio of reflected light to incident light. Reflectance is denoted by 𝜌. Reflectance is in-
dependent of the specific direction of the incoming or outgoing light; it provides a single value that
characterizes the surface’s overall reflective properties.
The Bidirectional Reflectance Distribution Function (BRDF) defines the relationship between the
incoming light direction (𝜔𝑖 ) and the outgoing light direction (𝜔𝑜 ) and is represented by
𝑓𝑟 (𝑥, 𝜔𝑖 , 𝜔𝑜 ) (3)
which represents the fraction of light energy reflected from the surface in the outgoing direction when
illuminated by a light from the incoming direction. Some examples of materials are:
1) Uniform Hemisphere (Ideal) Diffuse: This is a simple and intuitive model used to simulate perfectly
matte or non-glossy surfaces. This model assumes that light is scattered uniformly in all directions from
the surface point, regardless of the incident light direction. The BRDF of Uniform Hemisphere Diffuse
is constant across all outgoing light directions. The BRDF of Uniform Hemisphere Diffuse is given by
[4] as
𝜌
𝑓𝑟 (𝜔𝑖 , 𝜔𝑜 ) = (4)
𝜋
2) Lambertian Diffuse: Lambertian Cosine simulates real-world diffuse reflection. This model accounts
for the directionality of reflected light, making it more accurate for simulating rougher surfaces with
subtle directional effects. The BRDF of Lambertian Cosine is given by [4] as:
𝜌(𝑛 ⋅ 𝜔𝑖 )
𝑓𝑟 (𝜔𝑖 , 𝜔𝑜 ) = (5)
𝜋
where 𝑛 is the surface normal at the point of reflection.
3) Smooth Metal: The law of reflection is a fundamental principle in physics that describes how light
or electromagnetic waves interact with a surface. It states that when a ray of light strikes a surface, the
angle of incidence (𝜃𝑖 ) is equal to the angle of reflection (𝜃𝑟 ) with respect to the surface normal (𝑛) at
the point of incidence. A simple BRDF for smooth metallic reflection using the law of reflection is
𝑓𝑟 (𝜔𝑖 , 𝜔𝑜 ) = 𝜌(𝜔𝑜 ⋅ 𝑛)𝛿(𝜔𝑜 − 𝜌(𝜔𝑖 )) (6)
4) Dielectric: Snell’s law, also known as the law of refraction, is a fundamental principle in optics that
quantifies the relationship between the angles of incidence and refraction and the refractive indices of
the two media. When a light ray travels from one medium to another medium, the ray bends or changes
direction at the interface due to the difference in the speed of light in the two media. Snell’s law states
that the ratio of the sine of the angle of incidence (𝜃𝑖 ) to the sine of the angle of refraction (𝜃𝑟 ) is equal
to the ratio of the refractive indices (n) of the two media
𝑛1 sin(𝜃𝑖 ) = 𝑛2 sin(𝜃𝑟 ) (7)
where 𝑛1 is the refractive index of the incident medium, and 𝑛2 is the refractive index of the transmit-
ted medium. If the angle of incidence is such that the refracted ray would bend away from the normal
(𝜃𝑟 > 90 degrees), total internal reflection occurs, and the light ray is entirely reflected back into the
incident medium. The polynomial approximation provided by [5] allows us to simulate glass materials.
E. Bounding Volume Hierarchy (BVH)
A BVH organizes objects in a scene into a hierarchical tree-like structure of bounding volumes, such
as axis-aligned bounding boxes (AABBs), to accelerate ray-object intersection tests and improve ren-
dering performance [6].
The BVH construction starts by enclosing all objects in the scene within a root bounding volume,
typically an AABB that encompasses the entire scene. The objects are then recursively partitioned into
smaller groups, and each group is enclosed within a bounding volume. This process continues until
each group contains only a few objects, resulting in a tree structure with leaves containing individual
or small groups of objects and internal nodes representing larger bounding volumes.
To traverse the BVH efficiently during ray tracing, the nodes to visit and skip are based on the ray’s
intersection with the bounding volumes. By excluding unnecessary intersections, BVH significantly
reduces the number of ray-object tests, leading to faster rendering times, especially in scenes with com-
plex geometry.

III. Algorithm Implementation


A. Recursive Ray Color
The ray color function recursively computes the color of each pixel as RGB triples as per [7]. There is
a recursion depth limit, and the color of the pixel is determined by the average of all sample rays from
that pixel.
fn color(ray, depth) -> Color {
if depth == 0 {
return Color::ZERO;
}
match closest_hit(ray) {
Some(hit_result) => {
let material = hit_result.material;
match material.scatter(ray, hit_result) {
Some(Scatter { ray, attenuation }) => {
attenuation * color(&ray, depth - 1)
}
None => Color::ZERO,
}
}
None => background,
}
}
B. Ideal Diffuse Material
The ideal diffuse material scatters the ray in a random direction in the unit hemisphere [7].
impl Material for Diffuse {
fn scatter(
ray,
HitResult { normal, point, u, v, .. }
) -> Option<Scatter> {
let direction = rng.in_hemishphere(normal);
let ray = Ray::new(*point, direction, ray.time());
let attenuation = self.albedo.color(point, u, v);
Some(Scatter { ray, attenuation })
}
}
C. Lambertian Diffuse material
The lambertian material scatters the ray according the BRDF described in Section II.D.2 as per [7]
impl Material for Lambertian {
fn scatter(
ray,
HitResult { normal, point, u, v, .. }
) -> Option<Scatter> {
let direction = point + normal + rng.unit_vector();
let ray = Ray::new(*point, direction, ray.time());
let attenuation = self.albedo.color(point, u, v);
Some(Scatter { ray, attenuation })
}
}
D. Metallic material
The metallic material is a simple implementation of the law of reflection. We also add a fuzziness
parameter as per [7] which slightly randomizes the reflected ray to simulate fuzziness.
impl Material for Metal {
fn scatter(
ray,
HitResult { normal, point, .. },
) -> Option<Scatter> {
let reflected = reflect(ray.direction().normalize(), *normal);
let ray = Ray::new(
*point,
reflected + self.fuzz * rng.in_unit_sphere(),
ray.time(),
);
match ray.direction().dot(*normal) > 0. {
true => Some(Scatter {
ray,
attenuation: self.albedo,
}),
false => None,
}
}
}
E. Schlick Approximation
We implement the polynomial approximation to simulate the reflectance of a dielectric glass mate-
rial as per [7].
fn reflectance(cos_theta: f64, refraction_ratio: f64) -> f64 {
let r0 = (1. - refraction_ratio) / (1. + refraction_ratio);
let r0 = r0 * r0;
r0 + (1. - r0) * (1. - cos_theta).powi(5)
}
F. Intersection test with a bounding box
The intersection test for a ray with an AABB is implemented as per [8] to accelerate performance
using the bounding volume hierarchy.
pub fn hit(&self, ray: &Ray, time_min: f64, time_max: f64) -> bool {
let ray_origin = *ray.origin();
let inv_ray_direction = ray.direction().recip();
let t0 = (self.minimum - ray_origin) * inv_ray_direction;
let t1 = (self.maximum - ray_origin) * inv_ray_direction;
let t_min = t0.min(t1);
let t_max = t0.max(t1);
let time_min = time_min.max(t_min.max_element());
let time_max = time_max.max(t_max.min_element());
time_min < time_max
}
G. Light emissive material
We modify the ray color function to account for light emissive materials as per [9].

IV. Results and Discussion


For all images, the number of rays traced is equal to the resolution of the output image times the
number of sample rays per pixel. Hence, for an image with height ℎ, width 𝑤 and 𝑛 samples per pixel,
the total number of rays is ℎ × 𝑤 × 𝑛.
Figure 1 consists of 921,600 rays for a 1280 × 720 image and takes about three minutes to render on
an Apple M1 chip with an 8-core CPU.
Figure 2 consists of 921,600 rays for a 1280 × 720 image and takes about five minutes to render on
an Apple M1 chip with an 8-core CPU.
Figure 3 is a relatively simple scene also with 921,600 rays for a 1280 × 720 image and takes about
one and a half minute to render on an Apple M1 chip with an 8-core CPU.
Figure 5 takes significantly lesser time to render than Figure 4 due to the much fewer number of rays
(10) as compared to Figure 4 (200) and produces an image with less noise in comparison by sampling
the light directly. Taking the same number of samples alongside sampling the light directly in Figure 6
almost eliminates all of the noise.
Future extensions to this path tracer can involve using Multiple Importance Sampling [10] to com-
bine the PDF for the materials and lights and produce a more physically accurate scene.
Figure 1: Path traced scene with diffuse, metallic and glass spheres (100 sample rays per pixel)

Figure 2: Space-Time path traced scene with bouncing diffuse and staionary metallic and glass spheres
(100 sample rays per pixel)
Figure 3: Path traced scene with checker textured diffuse spheres and a spherical light source (100
sample rays per pixel)

Figure 4: Path traced scene Figure 5: Path traced scene Figure 6: Path traced scene
similar to Cornell Box, Lam- similar to Cornell Box, Impor- similar to Cornell Box, Impor-
bertian materials with Cosine tance sampling for the light tance sampling for the light
Distribution (200 sample rays source (10 sample rays per source (200 sample rays per
per pixel) pixel) pixel)

V. Conclusion
Path tracing, a probabilistic approach, efficiently handles global illumination effects by following
the most probable paths of light rays at each surface intersection. By considering material properties,
path tracing accurately determines pixel intensity, producing visually appealing and physically plausi-
ble renderings.
The theoretical foundation of path tracing, including the rendering equation and material appear-
ance models, has been discussed in detail. The report has provided practical insights into the
implementation of path tracing algorithms, considering ideal diffuse, Lambertian diffuse, and smooth
metallic reflection materials. Additionally, the role of bounding volume hierarchies (BVH) in acceler-
ating ray-object intersection tests has been explored.
Results and performance evaluation of the path tracer have been presented for various scenes with
different lighting conditions and materials. The path tracer demonstrates its ability to produce high-
quality images with relatively fewer rays compared to traditional ray tracing approaches. The results
showcase the importance of material models, sampling techniques, and BVH in achieving realistic and
efficient image rendering.
Future research and optimization avenues for path tracing have been identified, such as incorporat-
ing Multiple Importance Sampling (MIS) to enhance the accuracy of scene rendering. These extensions
hold the potential to further improve the efficiency and physical accuracy of path tracing techniques.
One prominent area of interest is real-time path tracing. Real-time path tracing aims to achieve inter-
active or even instantaneous rendering of complex scenes with global illumination effects, enabling
applications such as real-time video games, interactive simulations, and virtual reality experiences.

References
[1] A. S. Glassner, Principles of Digital Image Synthesis, San Francisco, CA, USA: Morgan Kaufmann Publishers Inc., 1994.
[2] J. T. Kajiya, “The rendering equation,” SIGGRAPH Comput. Graph., vol. 20, no. 4, p. 143, Aug. 1986, doi:
10.1145/15886.15902. [Online]. Available: https://doi.org/10.1145/15886.15902
[3] T. Whitted, “An improved illumination model for shaded display,” Commun. Acm, vol. 23, no. 6, p. 343, Jun. 1980, doi:
10.1145/358876.358882. [Online]. Available: https://doi.org/10.1145/358876.358882
[4] B. T. Phong, “Illumination for computer generated pictures,” in Seminal Graphics: Pioneering Efforts That Shaped
Field, New York, NY, USA: Association for Computing Machinery, p. 95. [Online]. Available: https://doi.org/
10.1145/280811.280980
[5] Z. Majercik, “The schlick fresnel approximation,” P.
a. W. I. Marrs Adam
and Shirley, Ed., Berkeley, CA: Apress, pp. 109–114. [Online]. Available: https://doi.org/10.1007/978-1-4842-7185-8_9
[6] T. L. Kay, and J. T. Kajiya, “Ray tracing complex scenes,” in Proc. 13th Annu. Conf. Comput. Graph. Interactive Techn.
in Siggraph '86, New York, NY, USA, 1986, p. 269, doi: 10.1145/15922.15916. [Online]. Available: https://doi.org/
10.1145/15922.15916
[7] P. Shirley, “Ray tracing in one weekend,” 2020. [Online]. Available: https://raytracing.github.io/books/
RayTracingInOneWeekend.html
[8] A. Majercik, C. Crassin, P. Shirley, and M. McGuire, “A ray-box intersection algorithm and efficient dynamic voxel
rendering,” 2018. [Online]. Available: https://api.semanticscholar.org/CorpusID:199371925
[9] P. Shirley, “Ray tracing: the next week,” 2020. [Online]. Available: https://raytracing.github.io/books/
RayTracingTheNextWeek.html
[10] V. Elvira, L. Martino, D. Luengo, and M. F. Bugallo, “Generalized Multiple Importance Sampling,” Statistical Sci., vol.
34, no. 1, pp. 129–155, 2019, doi: 10.1214/18-STS668.

You might also like