More Lights, HDR Rendering and BRDFs
Welcome to the fifth part of my raytracer development blog posts. In this part, I have added directional lights and spotlights, the ability to render HDR images, tonemap them so that they can be saved as a png and also many BRDFs(Bidirectional reflectance distribution function) that give us a more realistic lighting on the object itself.
Directional lights
Directional lights, as the name suggests illuminate the scene the same amount (same radiance) from a specified direction. They are used to simulate a distant but powerful light source (such as sun). It is fairly easy to implement the directional light, only thing to note would be that shadow rays are the exact opposite of the light's direction.
 |
cube_directional.png saved in 0,210s |
Spot lights
Spot lights are similar to a point lights. Difference is that there is a falloff factor so that the light is focused at a particular location. The falloff factor is a smooth function therefore the transition from lit area to an unlit area is soft. Spot lights require beta and alpha angles which determine the falloff factor to be specified.
 |
dragon_spot_light_msaa.png saved in 20,306s |
HDR Rendering
In ray tracing, shading calculations give us fractional values that is not limited to 0 - 255 range. In real life this is the case as well. Thanks to ray tracing, we were already generating color values for an HDR image. I needed to rewrite the image interface a bit. Before, I was storing the raster array for the image as unsigned chars, I had to change it to rgb (basically 3 float values) and then I was able to generate the HDR image in memory.
EXR is an HDR image file format, we will be generating exr images.
tinyexr is a header only library with a public domain license. Those two are probably my favorite attributes when it comes to adding a third-pary library to my project. It also has a clean API and easy to use. Although I did not look into other options, I know that
OpenCV also has read/write operations for exr images. I noticed that the exr images I generated using tinyexr are larger in terms of storage compared to reference ones. Maybe OpenCV would generate images smaller in size.
After these additions, I was able to get exr outputs. Also, by reading exr files as textures, we can map HDR textures onto our scene objects.
 |
cube_point.png saved in 0,260s |
This image is created without any tonemapping applied.
 |
sphere_point_hdr_texture.exr saved in 0,413s |
For HDR images, I only put the png ones. This image shows the ability of the raytracer to also texture map HDR images. (tonemapping applied)
Photographic Tonemapping
The problem now is to somehow obtain png images from HDR images. We obviously can not directly use the values of the HDR image as they are not limited to 0 - 255 range in the first place. A mapping of HDR to LDR is needed. A very simple and effective operator for this task is the photographic tonemapping operation. The details can be found in the
original paper. Note that for the first equation 1/N should be inside the exp().
 |
cube_point_hdr.exr saved in 0,392s |
BRDFs
BRDFs are used to compute the diffuse and specular shading at an intersection point. These functions characterize how the light is reflected from a surface.
I have implemented Phong, Modified Phong, Normalized Modified Phong, Blinn-Phong, Modified Blinn-Phong, Normalized Modified Blinn-Phong and Torrance-Sparrow BRDFs. Modified ones remove cos(theta) term from the denominator so that the function can be normalized. The importance of normalization will come up when doing path tracing as the BRDFs should be energy conserving. Torrance-Sparrow is a little more involved than the other BRDFs. It treats the surface as a collection of micro-facets and models shadowing, masking and interreflections.
 |
brdf_phong_original.png saved in 0,178s |
 |
brdf_phong_original.png saved in 0,178s |
 |
brdf_phong_modified_normalized.png saved in 0,169s |
 |
brdf_blinnphong_original.png saved in 0,181s |
 |
brdf_blinnphong_modified.png saved in 0,184s |
 |
brdf_blinnphong_modified_normalized.png saved in 0,172s |
 |
brdf_torrancesparrow.png saved in 0,180s |
 |
killeroo_blinnphong.exr saved in 9,262s |
 |
killeroo_blinnphong_closeup.exr saved in 15,913s |
 |
killeroo_torrancesparrow.exr saved in 9,390s |
 |
killeroo_torrancesparrow_closeup.exr saved in 16,920s |
Comments
Post a Comment