Monday, April 27, 2015

Tray Racer ( aka Ray Tracer )

No, the title and the article isn't aimed to whiskey drinking competition with crystal glasses full of ice cubes, slashed from ice trays. It's a silly try for a wordplay, meant to make a reference to ray tracing.
I was asked to write a thesis in computer graphics, so I had to refresh my basic knowledge not only in interactive graphics and rasterization, but also and other methods of non-real time graphics like ray tracing.
I find it quite useful to investigate a completely different approach to computer graphics. And to be honest, I like it very much. It is much more elegant and natural way to render things, not to mention it often needs just a small fraction of hacks needed to render proper graphics on screen, compared to rasterization methods. You just have your eye properties, like position, direction and field of view and for every pixel on screen you shoot rays through the scene to test what objects ( their properties- color, etc.) and where those rays hit them ( lighting interaction calculation). In a very simple test case, you can use spheres to test against your eye rays. Spheres can be represented parametrically by a position in space and a scalar value for the radius. 
You can find the intersection point completely algebraically by substituting the sphere equation in the line equation and solving a resulting quadratic equation. The idea is : if you have your ray equation and sphere equation, you can find solutions ( points ) that satisfy both equations at the same time. That's where ray pierce the sphere. For a ray tracer you are most likely interested in the closest point to the origin of the ray.
I'm playing with a simple ray tracer, that supports reflection, shadows, texture mapping ( sphere coordinates turned into UV coordinates ) and even global illumination by calculating several bounces of light. It uses recursion as it provides more elegant way for solving such problems. It's also single threaded.