Simple Python Raytracer

It’s been a while since I’ve posted anything up here, but here’s a little thing I whipped a couple weeks ago.  It’s a really basic raytracer, written in Python.

A good portion of the logic came from this tutorial, but I’ve converted it to Python and made a few changes.  I’ve not included all the things from that tutorial in this raytracer, since I’m not sure if I want to continue this project.  Python isn’t really the language of choice for writing something as computationally intensive as a raytracer.  But at least it was a fun project to brush up on my Python skills.

It takes in a scene file (a sample one is included in the zip file) and outputs a TGA file (which, from what I have heard from my wife, is pretty normal as far as rendering software goes).  To run it, use the following command:

python raytrace.py scene.txt output.tga

Using the basic scene included in the zip, you should get something that looks like the following.

Raytracer Output

The code still needs quite a bit of work.  I’m not certain if I’m going to continue refining this, or if I’m going to try to start on a C++ one.  Run time on my home machine (2.4 GHz quad-core, 3 GB of RAM) with Psyco installed is about 17 seconds to render a 640×480 image.  On a dual-core machine without Psyco, it ends up being ~55 seconds.  To compare, the C++ version from the tutorial ends up being just a couple seconds, and my wife tells me that’s still on the slow side for rendering such a simple scene.

The code can be downloaded here.

Comments are closed.