Monday, December 6, 2010

GPU Ray Tracing With GLSL

Tracing light rays from a virtual camera into a 3D scene
Ray tracing, at its core, is the process of tracing beams of light through some physical 3D space.  Light beams (or rays) typically travel in straight lines until they bump into some object.  At that point, the rays can either reflect off the surface, refract through it, or be absorbed by it.  Though the ray tracing technique can serve other purposes, it is generally used as a means of rendering photorealistic images.

One of the graphical features that are easy to obtain using ray tracing is physically accurate and pixel-perfect shadows.  As is apparent in the figure to the right, rendering a shadow is as simple as casting a ray from the surface of an object toward the center of a light source.  If that ray intersects some object before it reaches the light source, the originating surface point is in shadow.

Simple!

Ever since I was introduced to GLSL, I wondered if it would be feasible to implement ray tracing on the GPU.  The GPU, after all, is well-suited to geometrically-oriented computations.  I decided to take the opportunity to find out by attempting to implement it as a final project for Chuck Hansen's course on Interactive Computer Graphics at the University of Utah.  As a first pass, I decided to use ray tracing simply as a means of computing the shadows of an interactive OpenGL scene.

Read on for how I did it.