A Case of a Missing Square Root

by Eugene Lee

Recently I started working on my ray tracer again. I took a lot of new things that I learned in software design and applied it to my ray tracer which in my mind produced a happy marriage. Over the years my ray tracer has grown in code size. It has also gone through quite a lot of testing. Therefore, I try to make good use of refactoring tools when I make code design changes. But in one instance, I decided to move some code by hand. Mind you, the code has a lot of dense math operations so it wasn’t exactly easy code to move. And as I later found out, I made a mistake.

I was going through rendered images when I discovered that on a particular image, the edge of the shadows were a lot shaper than I expected. Given that I use rather large area light sources in all my images this was surprising.

Please take a look at the following comparison. These are actually poor sample images because you can barely notice the difference and the light intensity and number of samples are different, but if you look at the full size image carefully, the soft shadow on the left image is angled wrong. It almost looks as if there are two different light sources casting two different shadows. You might also notice that the lighting looks off.

Because the problem is with shadows, I knew I could probably reproduce it using a direct illumination shader (which is much simpler and is a subset of a photon mapping shader) to narrow the problem. And, as expected, the problem persisted.

I then suspected the problem might lie in sampling because sampling an area light source is a straight forward closed formed evaluation of the rendering equation and that is where the problem likely was. I looked at the code where I randomly select a point within a triangle with a uniform distribution and sure enough, there was a missing square root. And this was one code which I foolishly decided to manually refactor.

Advertisement