Wednesday, November 24, 2010

Real-time ray tracing in Silverlight

I’ve always been very interested in computer generated graphics. A technology that I find particularly interesting is ray tracing. This technique is based on the physical interaction between light and materials, and it can be used to generate very realistic images.

Because of the physical accuracy, this technique is very suited to predict how something will look before it’s even built. When we were in the process of building our home, I created several ray-traced images that we used to choose the color of the bricks, the windows, the roof, … This way it was possible to create virtual versions of our future home and select the one that most matched our taste. This is such a ray traced image that I created:


Developing a ray tracer is not very difficult and through the years I’ve written some simple ray tracers. Unfortunately the calculations are very CPU-intensive. That is the reason why 3D games use different techniques which are also nice to look at but they are not physically accurate. But computers are getting faster and faster, so real-time ray tracing will likely become feasible in the near future.

As a test-case, I’ve optimized my old ray-tracing code and I’ve managed to get it to run at real-time speed. Check out this Silverlight version:

[It might take some time until this Silverlight application is loaded so please be patient. If you’re viewing this post in an RSS reader (such as Google Reader), you might have to open it in your browser.]

Note that you need a fairly fast CPU to get it to run real-time (on my quad-core it runs at 20 frames per second). You don’t need a fancy graphics card because this ray tracer only uses the CPU. The implementation is fairly straightforward, no real tricks have been used to improve the performance (other than common sense). I believe that, given enough effort, it would be possible to improve the performance considerably. That would allow it to render a more complex scene real-time.

1 comment:

Aniket Bera said...

Hi Kristof,
Great work. Is it possible to get hold of the source code of this ?
My ID is