Let there be (HDR) light
Part 5: Let There Be (HDR) Light
Hi again, as we are improving our ray tracers, the time has come to enhance our ray tracers ability to simulate light. In this blog post I will be explaining a few different type of light sources and more importantly High Dynamic Range imaging and lighting.
Directional Lights
First, Let me start with the more basic and boring stuff and then I will move to cooler and relatively complex stuff. Up to now we only had two types of light sources now we are going to add three more, first one of them is directional lights. Their distance can assumed to be infinity to in real life some light sources emit all their light rays in the same direction. The best example of this is the sun. The sun is usually modelled by a directional light in computer graphics applications.
Directional lights are actually the easiest light source to implement. They are defined by a direction and their radiance. when we are computing the rendering equation we don't really need to do much. First we need to check if there is an object that is shadowing the point that is being shaded in the direction directional light. To do that we will simply send a ray from our object if that ray does not intersect with anything at all then we illuminate that point. and since these light sources are defined by radiance we don't even have to attenuate the incoming light. It's the simplest. You can see the difference between point lights and directional light sources. Notice the attenuation and the shape of the shadow.
| Figure 1: A cube illuminated by point light |
| Figure 2: A cube illuminate by directional light |
Spot Lights
As an introverted person I can say I am not the biggest fan of spot lights but when it comes to computer graphics I think they are pretty cool. Spot lights work just like point light but they only illuminate a cone shaped area. Aside from point lights intensity and position, spot lights also have a direction and two angles to define it's effective area. One of these angles define the full illumination area in this area spot light acts the same as a point light. The other angle defines the fall-of area in this area spot lights intensity goes down rapidly as we move to the edges. In the below slide you can see the formula for computing a spot lights contribution to a object surfaces recieved irradiance.
| Figure 3: Spot lights [1] |
|
And we can have even better, cooler scenes with multiple lights.
| Figure 5: Reflecting dragon on a reflecting plane with RGB spot lights |
We have another type of light source that is cooler but before explaining that I will explaing what HDR is and why we need to do our computations in HDR domain.
High Dynamic Range
Up to this point while rendering images I was clamping any light value that was above 255 to 255 so that I would get a value that I could write to a png file. This is called low dynamic range for lighting because it compresses the light into an interval between 0 and 255 and these values only but when a greater or a lower value comes it can not handle the change. This is not how things work in real life. The human eye can can adapt to different luminosities depending on the environment. For instance while at night a car's headlights can appear very bright for the human eye but the same headlights might not even be noticable while the sun is up. In both cases humans can see the world around quite well. This is because human eye is adaptive. When we clamp the values into a range while doing computations we are losing this wide range of luminance values. In order to prevent this from now on I will be preserving the HDR values as floating point numbers and output images in exr format. Most of the monitors today are not hdr, even though they getting popular, they wouldn"t be able to display these images. Because of this I will now explain a type of algorithm that is called tonemapping.
Tone Mapping
Now our problem is that we have a an HDR image and we want to display this image in an ldr this play. We want to create an ldr image from this hdr image such that this ldr image looks resembles the actual hdr output as much as possible in many cases this also means as realistic as possible but in some cases for artistic purposes we might want to deviate from realism.
To solve this problem I am going to use a very popular tone mapping operator that is called the photographic tone mapping operator. This is operator is called photographic because it uses the logic photographers used to use. This comes from the fact that photographers used to select a key to map the photograph. Depending on the scene a light or dark key would be desirable.
![]() |
| Figure 6: Different keys for a scene [2] |
Now I will directly proceed with the algorithm and it's results.
First of we start with finding the average luminance(Y) of the image. To find the luminance in an sRGB pixel, you can use the formula below.
Y = 0.2126R + 0.7152G + 0.0722B
But instead of computing the direct average writers of the paper suggested we take the log-average luminance an that is computed like
In step two we apply the aforementioned photographers method to map the luminances to a spesific value by linearly scaling them.
In this formula a represents the key value which is typically 0.18 but can change depending on the scene we are processing.
After linear scaling now we are going to do the crucial part human eye does not work linearly it's response to illumination is something like an s shaped curve because of this we are going to apply one more step that is the compression step.
Notice that now that all the values are guarenteed to be between [0, 1] after this operation. This operation is nice but for some scenes we may want to intentionaly burnout some pixels for artistic reasons or to make other pixels more visible. And to achieve that we can replace the formula above (3) with the followingWhere Lwhite is a value we decide typically we can choose Lmax, the maximum luminance of the scene but to burn out some values we can sort the luminances after applying formula 2 and find the 99th percintile for instance to burn the 1% of the pixels to value of 255. That is how I did in this homework.
After finishing the luminance mapping we can find the new RGB colors with the formula below
where Yo is the new Luminance we computed, Yi the input luminance and the s is a given saturation parameter. it is supposed to be between 0 and 1 it is typically 1 but we can use smaller values for saturated, grayish images.
And finally we clamp the values we found to [0, 1] then We compute the final RGB values of our output image by applying gamma correction and scaling them by 255.
| Figure 7: hdr texture on a sphere |
You can see the difference between and LDR rendered image and an image that is rendered HDR then tonemapped below
| Figure 8: LDR rendered scene |
| Figure 9: HDR rendered and tonemapped scene |
We had a cool dragon above now I have increased the intesity of light sources to 10 times higher and added a glossy ground and now we have an HDR rendered tone mapped dragon with beautiful colors. Look at the difference between HDR and LDR They are not exactly the same scene rendered with LDR and HDR domain because in LDR we would get a fully burnt image because in the scene below but in HDR we have no such limits
| LDR dragon with dimmer lights |
![]() |
Environment Lights
I have told that one type of light was remaining that type of light source is spherical environment map. Typically these are light sources that are assumed to infinitely far away from the scene and in a spherical form so the light is coming from every direction. These maps are usually created by 360 degree HDR cameras scanning an entire environment. These are fairly simple and we just actually use texture maps to use these. The only thing we need to do is define a mapping between a sphere and a rectangle(the actual image file) so that we can use the usual textures. It turns out that the following formulae are all we need.
Aaand we now have environment lights.And since we are doing ray tracing we can benefit from cool reflections and refractions.
I also want to share another mesh I have recently imported from stl. It is the medallion from withcer games you can see the same mesh with conductor material with illuminated by a point light and different environment lights. Environment lights are definitely much cooler(one of them is literally a glacier xD).
Cool Bugs and broken scenes I have made
I can't really give an output for these (because it prevented me to get
outputs) but while doing tonemapping I have also encountered a bug which
was occurence of the NaN values while computing the log-average-sum. It
turns out that this was caused by gcc's -Ofast option and disabling it and using -O3 solved the problem. man gcc says the following about -Ofast-Ofast
Disregard strict standards compliance. -Ofast enables all
-O3 optimizations. It also enables optimizations that are not
valid for all standard-compliant programs.
It turns on -ffast-math, -fallow-store-data-races
and the Fortran-specific -fstack-arrays, unless
-fmax-stack-var-size is specified, and -fno-protect-parens.
It turns off -fsemantic-interposition.
so using -ffast-math can cause problems if you don't know what you are doing.
See https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html for more information about optimizers.
unfortunatley I still couldn't render this scene without bugs because It is supposedly takin 12 hours and the computers I have used have been shutdown unexpectedly.
| Merged spheres |
| Broken spot light |
Tone mapping errors.
| Tone mapping bug |
And at one point I forgto to clamp the values to 0,1 before applying gamma correction and I have got the following results. These black and white waves happened because when the value gets bigger than 255 it overflows and 256 becomes 1 and 276 becomes 21 and 513 becomes 255 again and then we turn back to 0 and so on. Some of them are psychadelic
| recursing bugs |
Thanks for reading.
References
1 - A. O. Akyüz, Advanced Ray Tracing Lecture Slides
2 - Reinhard, E., Stark, M., Shirley, P., & Ferwerda, J. (2002, July). Photographic tone reproduction for digital images. In Proceedings of the 29th annual conference on Computer graphics and interactive techniques(pp. 267-276)
3 - https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html



Comments
Post a Comment