Wednesday, April 25, 2012

Ray Tracer Checkpoint 5 - Reflection

Before this checkpoint the ray tracer only calculated local illumination on the surface of an object; with the exception of shadows, the illumination of a point on a surface was completely independent of the rest of the scene. The purpose of this checkpoint is to change that a bit.

The Plan

The goal was to make surfaces reflective based on a coefficient of reflection that each surface will have.
The easiest way to do this was to have each surfaces' material (or texture, as of the last checkpoint) store the coefficient of reflection.

The actually reflection is then implemented using recursion. The basic algorithm is as follows:
  • Cast out a ray
  • Check for surface collision
    • If no collision, use background color (black)
    • If collision
      • Calculate normal illumination color
      • Determine reflection ray
      • Start over again, but with reflection ray
To prevent this from looping infinitely, we break out of the recursion whenever a surface isn't reflective (coefficient of 0) or after reaching a certain level of recursion depth.

Results

Basic - Reflective surface

 

The actual algorithm is extremely straightforward, simple to implement, and looks awesome. However, I still struggled for way too long with the blue sphere being in the reflection; turns out my sphere-ray collision detection was broken.

Also, it may be difficult to see, but please note that the specular highlight no longer shows up in the shadowed areas. This, of course, is a triumph. I have to make a note here; huge success. It's hard to overstate my satisfaction.

No comments:

Post a Comment