Raytracing in Python

I've always wanted to write my own ray tracer but it was really a hassle trying to do in the C++. With my foray into the Python world I finally set out to build a distributed raytracer. It's amazing how easy it is to code in Python a simple raytracer with the following features,

  • Sky box
  • Environment Mapping
  • Various objects - Spheres, Toruses, Metaballs ( a.k.a. zero-sets of potential fields )
  • Supersampling
  • Distributed Rendering
  • Multiple Light Sources
  • Object Materials
  • Scene Graph

In the below we show a rendering of a scene with two interlinked toruses with multiple light sources.

We also hook up the realtime raytraced image updates to display in the Ipython notebook.

The full source code for the raytracer can be downloaded here. The scene files ( *.json ) are also included in the source pack.

In [1]:
from   io import BytesIO
from   ipynb_utils import ImageWrapper
from   matplotlib.pyplot import imshow
import numpy as np
import raycast
from   raycast import SceneFile, Renderer, TracerOptions
from   IPython.display import HTML, display, clear_output, Image as IPImage
from   PIL import Image

%matplotlib inline
In [2]:
options = TracerOptions( 'scene2.json', fast=True )

raycast.options = options
pil_im          = Image.new('RGB', (320, 200), 'gray' )
image_wrapper   = ImageWrapper( pil_im )

scene_file = SceneFile( options.scene_file )
renderer   = Renderer( scene_file.camera, scene_file.scene_graph, image_wrapper )

renderer.render()