Ray Tracer from Scratch

The defining technique for the current generation of computer graphics

Ray tracing is a computer graphics technique which simulates light's behavior in the real world. This leads to much more realistic shading, shadows, and reflections in a scene, which is extremely valuable for modern-day computer graphics in video games and movies. A brute force approach is sending millions of light rays from all of the light sources in the scene and having them react to the objects and materials appropriately. If the light ray hits a diffuse material, it should be scattered randomly. If it hits a glass object, it should either reflect or refract as light does with regards to glass in the real world. We then would keep track of all of the rays that enter the camera and use them to draw a picture of the scene.

Because a lot of light rays shot into the scene will never make it into the camera, a lot of the computations are wasted. One optimization that all ray tracers make is to shoot the rays from the camera, rather than the light sources. This essentially is tracing the path of the light backwards, which guarantees that all ray calculations will contribute to the final render.

I have coded a simple ray tracer which has global illumination, anti-aliasing, caustics, and subsurface scattering. For each render shown below, you'll see the complementary scene made in Blender. While the results aren't exact, it's very rewarding to see how closely the shadows, reflections, and shading resemble a more professional solution.

The code for this project can be found here. Feel free to check it out and either try it or give me any feedback. As a side note, a lot of my code and knowledge base about ray tracing is heavily derived and adapted from Peter Shirley's book series, so special thanks to him for writing it.

Python isn't the most ideal language for writing a ray tracer, but by using pypy, the render times are much more reasonable. For the future, I plan on implementing many more features, such as:

Stay tuned for these features to be released!

My ray tracer

Blender