Week 5 Entry - Ray Tracing

Ray Tracing - What is it?

The concept of ray tracing follows the representation of a scene rendered through the intersections of many rays of light that a source emits throughout the scene. This effect is related directly to the number of objects in the scene, pixels used to represent the scene at a given resolution, and the number of rays. The implementation of this concept that we've adopted utilizes a traditional method of ray tracing which begins at the point of perspective (behind the pixels representing the scene) rather than calculating the effects of tracing some of the infinite rays scattered from the light source(s) in the scene themselves.

In the above concept of traditional ray tracing, the only rays that we are interested in spending the resources to calculate the color effects of are those that reach the eye or camera. Knowing the position of the light source in our scene, we can also use these rays from the eye to see how significant of a brightness factor needs to be applied to our pixel relative to the intersected object's base color value. With multiple objects in our scene, we can also use this method to find whether there is an impedance between the intersected object and the light and therefore represent the pixel as a shadow.

Time Complexity is a Myth

This whole process is a little expensive on the old browser. Once you need to render up to four colors per pixel and check for multiple rays of reflection opportunity, the process slows down quite significantly between updates in color updates. Getting to the point where such features functioned at all, let alone quickly, was an entirely different story. The coding and math applications are both a lot to understand independently, so applying them together, even with only two geometry objects and a light source in the scene, was challenging.

At a point in the assignment, we needed to refactor our code completely so that the type of geometry being traced from was irrelevant. This reconstruction would allow for the addition of any number of objects to the scene without explicitly coding on an object-to-other-object basis. This change took the heaviest toll on the brain, and after having completed this conversion, I felt the highest level of understanding of how these rays allowed for the simulation of the perceived color. A similar change happened when the color-assignment process needed to become recursive to allow for reflection properties to be represented.

Optimizing the calculation and memory-management processes is the area which, given even more time, I would be willing to return to and improve for the sake of presenting a more responsive interaction with the render. By changing some of the calculation methods to avoid division, square roots, and the creation of new Ray and Vector3 objects, I was able to gain around a 25-30% improvement in the framerate. This change was relative to the environment in which the code was run, but also leaves a lot of room for improvement. Such improvement would likely involve changing the functionality of the geometry object methods to reduce their use of square root functions, and the antialiasing functionality likely doesn't need to be applied to all pixels on the screen at all given times. I wouldn't know how to start a dynamic solution for such an adaptive solution, but it would be massively beneficial to the rendering time of each frame ... I'd imagine.

A fun fact: the performance of the end-case shown below could be uplifted by as much as 200% if we only recursively calculate the color of objects that are actually reflective (reflectivity > 0), but that wouldn't be in the spirit of trying to optimize for any given case.

The Path Taken to Ray Tracing

Humble beginnings.


Let there be light! Just one light though, can't 
make it too complicated. Also, it's not really "light"
it's just a vector object in space, relax.


Shadows added. Not too much trouble, yet. Time
to refactor the code for more objects in the scene.


Now, THIS is interesting. Not what I wanted, don't
understand how we got here, and I'm not sure I
hate it either ... but let's break it down.


Looks like some layering cases are not being
handled correctly...


My shadows have mysteriously disappeared,
but this is an improvement.


Ah ... admittedly a little bland, but no issues are
screaming at me anymore.


Antialiasing: how to immediately make your code
run at 1/4 of the efficiency.


Reflections! I don't care about the speed now, this
is cool...but wait, now I can see there is a problem
with how the sphere is projecting the shadow on
itself ... enhance!


Turns out the solution for floating point calculations
isn't carrying forward as cleanly with more objects in
the scene. Time to become more robust with the
implementation of some ray offsets.


*leans back in squeaky chair*



Comments

Popular Posts