Sunday 14 December 2008

Playing around with Ray Tracing


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.
And that's it! It was quite simple so far: the ray-sphere and ray-plane analythical intersections don't require very complex math and the shading algorithms are quite basics. I used the good lighthouse3d glsl tutorial as a reference, here. Of course, ray tracing is an incredibly complex topic and this is just a sort of "hello world"! The matter is really fascinating me, so I hope I can find some time to start implementing some more complex aspect of the topic. I would also like to release some code soon!

Coding experiments

Here I am,
I finally started a blog, yet another programming blog. I work in the games industry, usually on complex and long term projects, and I don't have much spare time to learn tools/languages and technologies that I don't usually work on in the office. For this reason, I will jump around different topics, and probably I won't manage to publish anything very interesting and complete, but I'll try to post something from time time!