Sunday, April 20, 2014

Writing a simple raytracer in Common Lisp

[Note: This is a repost after I accidentally deleted the original one].

Well, it's been a long time since I've last worked with Lisp and created the following stuff (~2008). Nevertheless I find it a waste to let all that stuff linger on some old URL that apparently neither I nor any other people have been using for ages.

So, if you've seen and read about the following tutorial on writing a simple raytracer in Common Lisp already, please forgive me for reposting it. If not, I hope you'll enjoy it and share with me the fun that I had during the creation of the material. Ok then, here goes what I have:

After stumbling upon Marco Baringer's and Rainer Joswig's Lisp related screencasts I realized how interesting, helpful and informative their contributions have been to me (thanks a lot!) and hence considered creating a comprehensive screencast/tutorial myself.

As I think that screencasts are a great idea to give someone a first insight into a particular topic and because (recently) on reddit there's been lots of discussions about Lisp being only for quantum physicists and the like, I eventually decided to create a tutorial on writing a simple raytracer in Common Lisp.

In my humble opinion, writing a raytracer shouldn't be too sophisticated for novices but allows for unveiling a reasonable amount of the programming language. And as far a the relatively easy mathematics and in particular the background of raytracing are concerned, I have included explanatory slides which will be thoroughly discussed during the tutorial.

Unfortunately I have only very limited time and hence couldn't finish each and everything that I would have liked to see in the tutorial. At the end of the provided screencasts, however, the raytracer is at a stage where basic raytracing and lighting do work.

As the creation of the screencasts and slides really was a lot of work and as I do not claim to be a Lisp uber-professional, I'd of course like to hear from you yet also ask you in advance to forgive me my (lispy) mistakes. The ones that I know about have been mentioned in the subsequent movie respectively. Also, please note that the first part is a neccessary prerequisite to its successors, yet the more interesting stuff starts from Part 2.

Topic Duration Size Related slides
Part 1 - Linear Algebra 1:10h 142 MB PDF
Part 2.1.0 - Basic Raytracing 1:28h 169 MB PDF
Part 2.1.1 - Basic Raytracing 1:09h 150 MB PDF
Part 2.1.2 - Basic Raytracing 1:54h 289 MB PDF

These two images show examples of what was rendered at the end of the above screencasts (click to enlarge).

The next images demonstrate more capabilities of the final raytracer. Unfortunately, whereas the code has obivously been written to the corresponding extent, I have never found the time to finish the screencasts and slides explaining the respective features as well as their implementation details.

Source Code

The source code is available at GitHub.


  1. Hi Alexander,

    Watching your videos on youtube in 2012 was a profound enlightenment for me and made me a better software engineer. Thank for all !

  2. Great videos series Alexander. As a fellow CL+Slime+Emacs user, I would say I enjoyed them a lot. I am trying to implement something similar -- find all cells intersected by a ray in a 3d grid. Hopefully, I will get good ideas. Thanks again.

    1. Thanks a lot for your comment. I have to admit though that the series is quite old now and that I'd do things quite differently nowadays. However, regarding your problem with cell-ray-intersection I'd recommend you have a look at raycasting. The 2d case is easily transferable to 3d. Similarly, have a look at bounding box / ray intersections. HTH!

  3. Thanks for the recommendation. You are versed in this area but I am not expert in graphics but I did get a lot of good ideas from the series.

    I will send you a private email with some figures to better clarify what I am trying to. I think the problem has been solved in different areas but I am not sure the actual name is: ray tracing, ray intersection, etc.
    I saw a matlab code and will include the link. My tool is in CL and would like to do it in CL although using CFFI with some external C or C++ graphics is not out of the question.

    Once again, thanks.