How to build a ray tracing engine in Silverlight

I'll explain briefly how to make a raytracer in Silverlight, and what were some of the challenges I faced. This article is Silverlight-specific and will not go into discussing the actual core raytracing technique.
To learn more about raytracing, search for "raytracing tutorial" on live.com, or if lazy just click this: http://search.live.com/results.aspx?q=raytracing+tutorial&form=QBRE

Challenges

The 3 challenges I faced were:
1. Finding a raytracing algorithm in C#: this was easy, just simple search returned a suitable one
2. Modifying Silvelright image dynamically - again, just searched
3. Making the raytracing work in parallel with the Silverlight UI and update appopriately

No 3. was the most challenging. In Silverlght, any code that's running on different than the UI thread cannot access any UI elements directly.
Silverlight provides a very useful class the Dispatcher, that allows calling a function on a UI thread from within a background thread.
Another challenge is that the background thread has the same priority in the UI thread and I didn't find how to change this. The background thread that runs the raytracing core algorithm is very computation heavy. Thus the UI thread does not have time to render the UI.

In our case refreshing the image. To make it work, I pause for a moment every 2 seconds in the background thread to allow the UI thread to "kick in" and repaint the screen.

Ok, I'll talk a bit about Raytracing

Raytracing is very simple in theory. Imagine looking through a window (e.g. imagine your monitor border as the window border).
What you see through this window is light rays reflected from objects on the other side of the window.
Where does the light come from? Yes, if you're outside it will come from the Sun. If you're at home, it will probably be a lamp or something. The lamp and the Sun are both light sources.
They emit rays of light all around. Rays travel through space (or the atmosphere) and reach something (object). Then, part of the ray bounces off the object and part of the ray is "eaten" by the object.
If the object "eats" some red part of the ray, less red will bounce off it, and we'll see it as not-so-red object (generally speaking). The ray bounces from objects until it:
1. EITHER goes away in space OR
2. passes through our window and ends up in our eyes!

As you can imagine, very small amount of the rays end up in our eyes, and most of the rays fly away, never to be seen again. What a pity!.

The raytracing algorithm pretends to "shoot" a ray from our eyes to somewhere on the "window" (or screen) we're looking at.
After the ray is shoot, it will hit something behind the window (or not hit anything). The raytracing algorithm determines the object hit by the ray, and bounces the ray off the object.
Do you see where this ends? The algorithm "traces back" the ray to its lightsource! Amazing isn't it.
Once the ray reaches the lightsource we know all objects it hit and can calculate its color.
If we trace one ray backwards for each pixel of the screen we'll have color for each pixel!
That's what we call a scene! Amazing, isn't it?

Download source code and start coding already!