Archive for February, 2007

Ray Tracing

Ok, I’ve got most of what I want done on the Ray Tracing project — enough at least to publish some code and do some blogging. This first article describes the overall coding structure, and how to use the project. I’ll get into some of the more difficult aspects of ray tracing in future posts.

Before going any further, here’s the source code.

Running the Application

After extracting the zip file, you’ll find two directories: Executable and Source Code. Pre-compiled binaries are in the Executable directory, and the source code is in the, errr… you get the picture.

Again, this project was built against XNA, so to run the executable you’ll need to install the following software:

To compile the project, you’ll need:


You can use the keyboard and/or an Xbox 360 Game Pad to control the application. The controls are as follows:

Control Device Description
W, A, S, D Keyboard Move the camera Forward, Left, Back, and Right respectively.
Print Screen Keyboard Take a screen shot.
R Keyboard Reload the scene from an Xml file (if the scene was initially loaded with an Xml file). Note: this version of the project auto loads changes to the Xml file. As such, you don’t really need ‘R’, but it can be used as a ‘Reset’ feature.
Left Thumbstick Game Pad Move the position of the camera along the X and Y axes (target stays fixed).
Triggers Game Pad Move the position of the camera along the Z axis.
Right Thumbstick Game Pad Move the camera’s target along the X and Y axes.
X Button Game Pad Toggle the ray tracing coloring methods. Theses methods are WithoutLighting, WithLighting, WithHardShadows, WithReflection, and WithRefraction.
Code Organization

The are three assemblies that make up the application. These are:

Assembly Name Description
Bespoke.Games.Framework This is a set of basic classes I use when working in XNA.
Bespoke.Games.Framework.RayTracing Class libraries specific to the ray tracing project. This is where the bulk of code resides for this project.
BasicRayTracing The executable and host of the Game-derived class. There’s very little actual code here — just initialization and the main update and rendering loops.
Significant Classes Description
RayCamera The camera within a ray tracing scene. The Initialize() method generates and caches a set of direction vectors for casting rays into the scene.


Maintains the list of shapes and actors within the scene and implements the Ray Tracing algorithm within its Draw method.
SceneLoading\*.* All of these classes support the Xml scene loading. The entry point to Xml scene loading is the class RaySceneLoader.

IShape / Sphere / Plane / Triangle / ShapeBase

Procedurally created “simple” shapes.


“Complex” shapes made from 3D models. Actors use the XNA content pipeline to extract the index and vertex buffers to produce a set of Triangle objects.

So now to the code itself. Of the above significant classes, the RaySceneManager is where the ray tracing is actually happening — starting with the Draw() method. Essentially, with each pass through the Draw() method we build up a Texture2D object by casting a set of rays into the scene, collecting a color at the closest object intersection (or the background color if there is no intersection). We then draw the resulting texture to the screen. Here’s that code (with some timing code removed):

The ray direction vectors are precalculated in the RayCamera.Initialize() method. Most of the work here is handed off to the TraceRay() method, and it’s this method that returns a color to be stored in the texture to be rendered. The TraceRay() method casts a ray into the scene and determines a resulting color using a variety of methods. I’ve used a delegate to allow for different color retrieval methods within the same coding architecture (for effects such as shadows and refraction). Here’s the general TraceRay() algorithm:

  1. Find the closest “simple” shape object that the ray intersects.
  2. Find any closer shapes contained within “complex” objects (Actor instances) that the ray intersects.
  3. With the closest intersected shape in hand, get a color at that intersection point and return it to the calling method.

This third step looks something like:

Where sCurrentColorMethodHandler is a delegate pointing to the currently selected color method.

I’ve been typing on this for awhile now, so I think I’ll call it quits for the moment and let you experiment with the code. I’ve included a set of scenes you can get started with, and you’re welcome to use this code in any way that you see fit.

Please note, there are a number of known deficiencies with this implementation. These include: no refraction for solid objects (e.g. spheres), slow uniform grid initialization for the uniform spatial subdivision algorithm, ray initialization could be improved, and in-code documentation. And I’m certain there are dozens upon dozens of other problems that I don’t know about. Please go easy on me if you find these. This project has been about learning ray tracing, and I’m very happy with what I’ve produced thus far. But as with everything, this is a work-in-progress and is far from a ‘finished’ state.

Here’s a screenshot from a fairly complex scene (17,000 shapes) using uniform spatial subdivision:

Uniform Spatial Subdivision

If you have any questions please let me know. Here’s a link to the code again.

RayTracing Source Code


Graphics Projects

I’m taking a graphics class at UCF, and we’ve been working on a set of projects for hardware rendering and ray tracing. The professor was very flexible about the language and graphics libraries we employed, so I chose C# and XNA. I’m actually writing a set of posts on this and will publish them under the Ray Tracing page. But for starters, here are some screenshots from the first few iterations.

(Click on the images for a larger version.)

Project 1 (Hardware rendering)

 Project 1 (image 1) Project 1 (image 2)

This project used the hardware renderer to draw the car, a reference grid, and the surrounding skybox (just a set of quads with a garage-looking texture on it).

Project 2 (First iteration of the ray tracer)

 Project 2 (image 1) Project 2 (image 2) Project 2 (image 3)

The first image is a simple triangle and sphere set against a crimson plane with diffuse lighting and a single light in the scene. The second image is a white plane with three colored lights set directly in front of the it. The third image is a box made of six colored planes and a model of a wooden fork (with a single light in the scene). All images are at 640×480.

Project 3 (Ray tracer with shadows, reflection, and refraction)

 Project 3 (image 1) Project 3 (image 2) Project 3 (image 3)

This version demonstrates hard shadows, reflection, refraction, and Phong shading.

The first image is a set of 4 spheres and a refractive face (2 triangles with a refractive index of 1.3) within a box of 6 planes and 2 lights in the scene. The second image kinda represents a mini-solar system (with a very small sun) on an olive drab plane with 3 lights in the scene. The third image traces 2 spheres and a refractive rectangular face (refractive index of 1.1) near the intersection of 2 planes with single light in the scene.

Comparing a scene with different color methods

I thought it’d be interesting to flip through a single scene and compare the output of each color method. The methods are WithoutLighting, WithLighting, WithHardShadows, WithReflection, and WithRefraction (where each subsequent method incorporates the previous methods). Here goes:

Without Lighting With Lighting With Shadows

With Relection With Refraction

I’m going to publish a set of write ups about all of this, so keep an eye out.


Back to School

It’s been awhile since my last post, and quite a lot has changed for me. I left EA around the end of October to pursue my Masters and Ph.D. at the University of Central Florida. This has been something I’ve been considering for quite a long time now, and the Universe seemed to be pointing me in this direction.

This move goes directly to my love of learning and teaching, and I’m aiming for a full-time career doing both.

Anyhow, I’m about five weeks into my first semester in the Modeling & Simulation program and I’m really enjoying it. I’m working on a robotics project for one class, and a series of graphics projects in another. I’m going to publish some of that work here for anyone interested.

Thanks for reading.