Thinking until the 2147483648'th second

About This

James Long
I think all of the files I linked to in some of my older posts are gone now. I am working on fixing them.
View my complete profile

Saturday, October 6, 2007

Scheme Raytracer written in a couple of days

5/26/2009 Update: I have moved over to http://jlongster.com. You can find Schemeray there.

So there's a Haskell raytracer, a Perl raytracer, and even a Javascript raytracer. Where is Scheme in all this? The Raytracer language comparison includes Scheme, but I can't find any source for it. I decided to write a raytracer in Scheme to give it some credit.

Scheme Raytracer v0.1 supports Phong illumination, sphere/plane/triangle intersection, .obj file loading, and reflections as its basic features.



Example of loading a .obj ("TIX" are the last three letters of the company I work for, COPTIX, but the COP in the mesh need reconstruction so I took them out):





Source code (Gambit Scheme, includes .obj file): schemeray-0.1.tar.gz
Or just view the main file: schemeray.scm

I've fairly new to Scheme, meaning I've been studying it (along with functional languages in general) for a while but haven't coded anything of substance in it. I'm interested in working in Scheme for 3d graphics, and I figured a raytracer is a great place to start feeling out Scheme for that kind of work.

I was right - I learned a lot about what I love about Scheme and what I miss from other languages. This article could go in all sorts of directions, but I'm going to keep it simple, post some screenshots and list some pros and cons about Scheme. Serious profiling is out of the question because the raytracer hasn't been optimized yet. It is currently written using Gambit Scheme.

The second and third image took ~415s to render. The mesh consists of 84 triangles which, without any spatial partitioning implemented, slows down the rendering a bit. A few spheres with full effects renders in ~30s, but none of these numbers mean much because I haven't optimized anything. I tried running it on Chicken just to see speed differences but I couldn't get the syntax-case egg working. At some point I'll profile different schemes with this which may merit a write-up.

What I Liked

These are a few things I liked about Scheme. I suppose I should say that I'm coming from a C++ background.
  1. Code and data equivalence. My scene was simply a list of property lists, something in the form of
    (define scene `((sphere ,(make-vec3d .2 .5 .5)
    ,(make-vec3d -10 0 50)
    10)
    (light ,(make-vec3d 1.0 .8 1.0)
    ,(make-vec3d 0 30 50)))
    and I can map over it. This is just a simple example, but especially in testing it's very flexible to be able to construct arbitrary lists as well as inspect functions.

  2. 'write' and 'read' are beautiful functions. It's an elegant abstraction of parsing, something which I usually dreaded in C++.

  3. Naming conventions. Because of Scheme's lack of constraint on variables names, I can do things like
    (let ...
    (n.l (saturate (vec3d-dot n l)))
    (r.v (saturate (vec3d-dot r v)))
    (o-a.n (vec3d-dot (vec3d-sub origVec a) n)))
  4. General readability of code. Once you get used to s-expressions, reading mathematical expressions becomes very natural.

Things I Miss

Just a few things that I caught myself reminiscing about.
  1. Types. I'm a big fan of types not so much for compile-time error checking, but mainly for determining the execution path. I think it leads to elegant code. We all know that repetitive code (or patterns) indicate that something's wrong. And I find this pattern in Scheme:
    (define (generic-operation a)
    (if (pair? a)
    (operation (cdr a))
    (operation a)))
    I don't think Scheme should be a statically typed language. I'm just saying that I need refactor that kind of pattern out somehow. Possibly a simple macro would do, or maybe I need an object system like Meroon. At least generic methods are needed; if you take a look at the raytracer code, I had to manually dispatch actions to the sphere, lights, planes, etc. because there isn't any automated dispatching.

  2. A solid module system. This applies to Scheme in general, not to an specific implementation (I know that Scheme48 has a nice module system). I miss a standard module system because, for example, if I wanted to use Meroon I would have to figure out how to integrate it with whatever module system I choose. If a standard module system existed, properly written libraries would already be integrated. I know that R6RS includes a module system, but from what I hear it's broken. Here's hoping a standard module system is implemented across implementations soon.
Half-way through writing the raytracer I realized how much I miss types, and I looked at Haskell. It's a very elegant language, but I just couldn't pull myself away from the expressiveness of Scheme. So I'm sticking with Scheme, and it's just a matter of tailoring the syntax for my needs.

(forgive formatting inconsistency, I'm still fighting a little with blogspot)

13 comments:

Jeremy said...

FWIW: On my machine:

Gambit
real 4m22.555s
user 4m12.809s
sys 0m2.762s

Chicken
real 4m16.025s
user 4m2.641s
sys 0m9.172s

It took some work to make it work with Chicken though. The syntax-case egg was easy enough and I replaced define-structure w/ define-record. On the other hand, obj-file-parser.scm needed quite a bit of massaging but this shouldn't have affected the timing much.

Jon Harrop BA MA MSci PhD (Cantab) said...

You might like my ray tracers in OCaml and F# as well.

James Long, code expander said...

Thanks for the results jeremy. I've heard that Chicken can be quite fast. At some point I will optimize the code for both systems and profile it then. Would you mind putting your chicken code somewhere and posting a link to it?

jon - very interesting stuff!

Jeremy said...
This post has been removed by the author.
Jeremy said...

It's a pretty rough translation but I posted it and wrote a little about the experience on my blog

offby1 said...

You should try PLT scheme -- it has a couple of built-in object systems, at least one of which I strongly suspect is quite solid; and it has a very nice module system.

dghnfgj said...

This momentousdecree warcraft leveling came as a great beacon light wow lvl of hope to millions of negroslaves wow power level who had been seared power leveling in the flames of power leveling withering wrath of the lich king power leveling injustice.wrath of the lich king power leveling it came as a WOTLK Power Leveling joyous daybreak to end the long WOTLK Power Leveling night ofcaptivity.WOTLK Power Leveling but one hundred years wlk power leveling later, we must face aoc gold the tragic fact thatthe age of conan power leveling negro is still not free. aoc power leveling one hundred years later,age of conan power leveling the lifeof the negro ffxi gil is still sadly crippled by the final fantasy xi gil manacles ofsegregation guild wars gold and the chains of discrimination. one hundred yearslater, maplestory mesos the negro lives on a lonely island of poverty in themidst of a vast ocean of material prosperity.dog clothes one hundred yearslater, the negro is still languishing in the corners of americansociety and finds himself an exile in his own land.

world of warcraft gold said...

Weaknesses of World Of Warcraft Gold the client-server model used by World of Warcraft have been wow power levelingexploited in order to crash the cluster of servers that aoc goldmake up a realm. Exploits also include characters being able to instantly Cheapest Wow Goldchange location or teleport. The situationbecame worse cheap wow goldwhen trying to coordinate activities across a number of playersor guilds on the same realm.World of Warcraft Lead Producer, stated that new realms would be introduced to warhammer goldrelieve the burden on existing ones. Existing realms would be upgraded.

Although the game wow gold follows a similar model to others in the genreand was noted for having wow gold cheapmany familiar concepts from roleplaying games, the new approaches gold4powerto reduce pauses between game encounters was well liked. At various times, World of Warcraft players have experienced problems with connecting to and logging in to wow gold for sale the game. Sudden server crashes that would force realms offline also occurred.

belrion46 said...

buy wow goldbuy wow goldbuy wow accountbuy wow gold

dfous said...

Bon marche de Dofus Kamas.achat de dofus.le prix moins cher.nous vendons dofus,or de Wakfu. 24/7 appui-en-ligne et livraison rapide.Wakfu Kamas
china: chinaserving

dreaz said...

I am grateful to you for this great content.aöf thanks radyo dinle cool hikaye very nice sskonlycinsellik very nice ehliyet turhoq home free kadın last go korku jomax med olsaoy hikaye lesto go müzik dinle free only film izle love aşk 09sas mp3 indir

Anonymous said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服酒店

酒店兼差

酒店上班

酒店PT

酒店

酒店喝酒

酒店消費

喝花酒

粉味

喝酒

Anonymous said...

Great work! If you miss generic methods, try TinyCLOS.

Random Notes

I work at Coptix

Ideas / To-do

  • Research .Mac style photo gallery for screenshots