|
| GOLD MEDAL awarded for best final project in class. |
|
Ray tracing is a method of producing realistic images given a user-defined 3D scene.
In its simplest form, rays are traced from the eyepoint, through each pixel in a virtual screen, and into the scene.
The colour of each pixel in the final image is determined based on the colour of the closest object that the corresponding ray intersects, the colour and occlusion of the light sources illuminating the point of intersection, and the fate of the reflected and refracted rays.
This project involved the implementation of 10 objectives of my own choosing, which are described below.
The ray tracer was programmed in C++, and the scenes were defined hierarchically using Lua scripts.
|
|
|
Antialiasing refers to the reduction of visual artefacts that appear when high-frequency visual data is sampled too coarsely.
Supersampling addresses this issue by tracing multiple rays into the scene for each pixel, thereby capturing more high-frequency visual content.
The adaptive supersampling technique that was implemented involves two rendering passes.
On the first pass, the results of which are shown in Figure 1, only one sample per pixel is used.
|
|
 |
| Figure 1: Results of first rendering pass. |
|
A Sobel edge detection filter is then used to identify the high-frequency visual content, which are the areas that will benefit most from supersampling.
The detected edge information is then smeared to ensure that all edges are supersampled, as shown in Figure 2.
|
|
 |
| Figure 2: Edges detected using a Sobel filter. |
|
On the second rendering pass, only those high-frequency regions identified by the Sobel filter are supersampled, so the number of samples used can be relatively high without dramatically increasing the rendering time.
Notice how much smoother the edges are in the final image, shown in Figure 3, compared to the results from the first rendering pass.
|
|
 |
| Figure 3: Results of second rendering pass. |
|
The solutions of the general quadric equation define either points, lines, or surfaces in 3D space, depending on the coefficients used in the equation.
Solutions that define points or lines are referred to as degenerate solutions, and can be described using simpler equations.
There are 9 nondegenerate forms of the general quadric equation.
An ellipsoid, an elliptic paraboloid, a hyperbolic paraboloid, hyperboloids of one and two sheets, a cone, and an elliptic cylinder are shown in Figure 4.
|
|
 |
| Figure 4: Variety of quadric surfaces. |
|
The basic shapes used for modelling objects are referred to as primitives, and typically include spheres, cubes, and polygonal meshes.
Constructive solid geometry (CSG) involves treating all primitives as solids, and combining them with union, intersection, and difference operations.
For each primitive, intervals of intersection along each ray are computed rather than simply the points of intersection between the ray and the surface of each object.
These intervals of intersection are then combined using a specific set of rules for each type of CSG node.
CSG was implemented for spheres and cubes, as illustrated in Figures 5 and 6, as well as for general quadric surfaces since they are, by definition, infinite in extent.
|
|
 |
| Figure 5: Constructive solid geometry with two spheres. |
|
 |
| Figure 6: Constructive solid geometry with a sphere and a cube. |
|
Rather than assigning a solid colour to each object in a scene, texture mapping can be used to paint a 2D image, often a digital photograph of a real surface, onto the surface of an object as it is rendered.
This process involves first calculating the texture coordinates associated with the point of intersection between a ray and the surface of an object, and then assigning a colour value to this point based on the colour of the corresponding pixel in the texture.
Spherical coordinates are used for texture mapping spheres, and barycentric coordinates are used for cubes and meshes, as shown in Figure 7.
|
|
 |
| Figure 7: Texture mapping of spheres, cubes, and meshes. |
|
Textures can also be mipmapped and tiled.
Mipmapping is a method of avoiding the appearance of moiré patterns caused by incorrectly downsampling high-frequency textures.
Rather than always determining the colour of a pixel based on the full resolution texture, pixel colours are determined by interpolating between the two mipmaps closest to the size of the object being rendered.
In Figure 8, the entire left half of the floor has been tiled using the full texture, whereas the right half of the floor has been tiled using mipmapping, and is less noisy as a result.
|
|
 |
| Figure 8: Comparison between straight texture mapping and mipmapping of a mesh. |
|
Perlin noise is a method of generating volumetric textures procedurally, and can be used to model realistic-looking wood and marble textures, as shown in Figure 9.
|
|
 |
| Figure 9: Wood and marble textures created using Perlin noise. |
|
Bump mapping is used to create more realistic-looking textures by perturbing the direction of the surface normal vector before performing the lighting calculation.
In Figure 10, the two planets on the left have been bump mapped and appear to have rough surfaces.
[Note: the sign error resulting in recessed mountains and protruding valleys has since been fixed.]
|
|
 |
| Figure 10: Bump mapping applied to spheres. |
|
Normal mapping is similar to bump mapping, except that the entire normal vector is replaced, which can create dramatic effects.
In Figure 11, the divots in the solid white cube have been created using CSG; the appearance of divots in the near cube has been created using normal mapping.
The tiled surface in the background has been normal mapped as well.
|
|
 |
| Figure 11: Comparison between normal mapping and CSG. |
|
When a ray intersects an object, it can be reflected, refracted, absorbed, or some combination of the three.
The amount of light that is reflected and refracted can be calculated using the Fresnel equations; the direction of the refracted ray can be determined using Snell's Law.
Reflection and refraction are illustrated in Figure 12.
|
|
 |
| Figure 12: Reflection and refraction. |
|
Dielectrics such as glass do not necessarily transmit all wavelengths of light with equal intensity, so can appear to be tinged with various colours.
When compared to the glass sphere shown in Figure 12, it is evident that the sphere in Figure 13 tends to absorb blue wavelengths of light more than red or green wavelengths.
|
|
 |
| Figure 13: Absorption of light through glass. |
|
Caustics are created when light is focused onto a surface as it reflects off or refracts through a material, such as water focusing light from the sun on the bottom of a swimming pool.
The simulation of caustics involves first building a photon map by casting photons from the light sources into the scene.
A photon is reflected off or refracted through materials until it hits a diffuse surface, at which point it is stored in the photon map.
Once the photon map has been populated, the scene is rendered in the usual way.
When a ray intersects the surface of an object, however, the data in the photon map is included in the pixel colour calculation.
|
|
In order to obtain results containing low amounts of noise, a large number of photons must be used.
To reduce the amount of time required to build the photon map, records are maintained of the number of photons cast into the octants surrounding the light source.
If a sufficient number of photons are cast into a given octant without intersecting any reflective or refractive objects, then the octant is no longer considered.
Reflective and refractive caustics as well as the corresponding photon maps are shown in Figures 14 to 17.
|
|
 |
| Figure 14: Reflective caustics generated using photon mapping. |
|
 |
| Figure 15: A direct visualisation of the photon map used in Figure 14. |
|
 |
| Figure 16: Refractive caustics generated using photon mapping. |
|
 |
| Figure 17: A direct visualisation of the photon map used in Figure 16. |
|
Hierarchical bounding volumes (HBVs) are used to reduce the number of costly intersection calculations that must be performed.
For hierarchically-defined scenes, HBVs can be implemented fairly readily.
In particular, if a ray is known to miss one child of an intersection node, then the remaining children of the intersection node need not be tested.
If the simplest children are tested first, a significant amount of time can be saved.
Similarly, if a ray does not intersect the eldest child of a difference node, then there is no need to perform the intersection calculations for any of the other children.
The image shown in Figure 18 was used as a test case, since it contains a number of infinite quadric surfaces combined with CSG nodes.
Without the use of HBVs, the image required 9 minutes, 15 seconds to render.
By using HBVs to avoid unnecessary intersection calculations, the image required only 7 minutes, 24 seconds to render, a 20% reduction in rendering time.
|
|
 |
| Figure 18: Test case for hierarchical bounding volumes. |
|
In general, a Monte Carlo technique involves the use of random samples to form an estimate of some phenomenon.
In the context of ray tracing, Monte Carlo sampling refers to the tracing of many randomly-directed rays from a given point in order to obtain an approximate solution for the bidirectional reflectance distribution function at that point.
Stratified sampling, a variation of Monte Carlo sampling, involves first dividing the space from which the random rays are selected into n subregions of equal dimensions, where n is the number of samples desired; one random sample is then obtained for each subregion.
Stratified sampling ensures that the random samples are distributed evenly; pure Monte Carlo sampling does not.
Consequently, the results obtained using stratified sampling tend to be less noisy than those obtained using the pure Monte Carlo sampling technique.
|
|
Depth of field involves modelling the aperture of a virtual camera so that objects not in the focal plane are out of focus.
This effect can greatly improve the realism of the final image.
A ray directed from the eyepoint toward the center of a pixel is used as the reference ray.
The focal point, the point at which all rays emanating from the aperture converge, is determined by travelling along the reference ray a distance equal to the focal length.
One randomly-positioned ray per subregion of the stratified sampling grid is then directed at the focal point and traced into the scene as before.
Each pixel in the final image will be determined based on the points of intersection of the sample rays associated with that pixel.
As an object moves further from the focal plane, the area of its surface being sampled by the sample rays increases, hence the increasingly blurry appearance of objects in the extreme foreground and background of an image.
In Figure 19, the focal plane is far away and, as such, the distant spheres and Rubik's cube are in focus.
The same scene is shown in Figure 20, but has been rendered using a focal plane much closer to the eyepoint.
Note that the Rubik's cube in the bottom-left corner is near and small; that in the upper-right corner is far away but large.
|
|
 |
| Figure 19: Depth of field with far focal plane. |
|
 |
| Figure 20: Depth of field with near focal plane. |
|
Soft shadows can be obtained by replacing point light sources with area light sources.
To determine whether a given point on the surface of an object is in shadow, shadow rays are cast to random positions on the light source.
Once again, stratified sampling is used rather than pure Monte Carlo random sampling.
Furthermore, reducing the amount of noise in the final image requires the use of many shadow rays, so an optimisation technique has been implemented.
A small set of shadow rays is cast before applying the full stratified sampling algorithm.
If all the test rays are occluded, then it is assumed that the point of interest is in shadow.
If all the test rays are visible to the light source, then it is assumed that the entire light source is visible, and the lighting for that point is calculated as if the area light source were a point light source concentrated at its center.
Otherwise, the point of interest is in the penumbra region, and the full stratified sampling algorithm is applied.
The scenes shown in Figures 1 and 16 have been re-rendered using area light sources, as shown in Figures 21 and 22, respectively.
|
|
 |
| Figure 21: Soft shadows created using area light sources. |
|
 |
| Figure 22: Soft shadows with refractive caustics. |
|
The features described above have been used to generate the images shown below.
Unfortunately, due to the intense amounts of computation required, it was not possible to render the scene on the computer graphics lab machines with antialiasing, photon mapping, depth of field, and soft shadows enabled simultaneously.
As such, two images of reasonable quality have been generated.
Caustics, depth of field, and some antialiasing have been used to generate the image shown in Figure 23; the corresponding photon map is shown in Figure 24.
Figure 25 is a rendering of the same scene, but with caustics and soft shadows enabled.
|
|
 |
| Figure 23: Final scene with caustics, depth of field, and some antialiasing. |
|
 |
| Figure 24: A direct visualisation of the photon map used in Figure 23. |
|
 |
| Figure 25: Final scene with caustics and soft shadows. |
|
|