CS 101 Final Project: Raytracing in GLSL

Introduction

Raytracing is a well-studied technique for accurately rendering certain types of scenes. Historically it has been slow in comparsion to rasterization methods, but with the advent of fast, parallel GPUs, real-time raytracing is increasingly becoming a reality. To throw my hat in the ring, so to speak, I created a small simple sphere raytracer using GLSL.

Running the Program

You can download the source here. Compiling the program requires OpenGL and GLUT. To run the program at a decent framerate, you will need a relatively modern graphics card. A Makefile is included, so building the program should be as simple as `make`.

You can download a Linux binary here.

Press 'l' to toggle between a single white light and three rotating colored lights.

Implementation

Scene information (spheres and lights) is packed into the GL_LIGHT strutures. Light position, diffuse, and specular components are given in the obvious. Sphere position is packed into GL_SPOT_DIRECTION. The sphere color and radius is packed into the GL_AMBIENT component of the light. The camera is determined by OpenGL's model-view matrix, so any camera system compatible with OpenGL is similarly compatible with the raytracing application.

Since the scene information is supplied through GL_LIGHTs, the scene can be updated inexpensively in real-time, as demonstrated in the example scenes.

The vertex shader just renders a fullscreen quad. The fragment shader does most of the work. The camera is computed using the MV matrix. Then the ray and its reflections are traced through the scene. Tracing stops when the maximum ray depth is reached or the contribution from the reflection is sufficiently small. (I intented to make reflective power a per-sphere attribute, but I didn't have time, so the conditions are somewhat redundant at this point).

Results

On my laptop, with a nVidia 9600M GT graphics card, the above scene runs at ~60 FPS.

Limitations

Since the scene information is packed into GL_LIGHTS, there can only be at most 8 spheres and at most 8 lights in the scene. One could use textures to have an unlimited number of entities, but I decided not to go through the trouble.