
In the last couple of days I've been experimenting with ray-tracing a little bit.
I started from a simple wikipedia introduction from here and because this was supposed to be a fun programming exercise, I went into coding as soon as possible.
I chose to use c# just to speed up the development and XNA just to avoid to write another math library (XNA comes with a nice Vector library). I know that .NET/c# is not probably the best choice in term of performance, but at this stage I just wanted to focus on the algorithm.
Anyway, the above picture is the first interesting result I got. 5 spheres and two directional light sources, with ambient, diffuse and specular effects. I also implemented the tracing recursion (just 1 level in the picture, but the code is generic) and shadow rays.
The code is pretty straightforward at this stage: for every pixel on the screen, throw a ray from the view point through the image plane and get the intersection with the closest object. The ray cast information is used to:
- compute a shadow ray against all the light sources in the scene (easy for directional lights, the only supported type atm)
- if the point is not in shadow, compute the diffuse and specular contribution
- if the requested recursion level is not reached, compute a reflection ray and trace it against the scene.