ZX Spectrum Raytracer

https://gabrielgambetta.com/zx-raytracer.html

I love raytracers; in fact I’ve written half a book about them. Probably less known is my love for the ZX Spectrum, the 1982 home computer I grew up with, and which started my interest in graphics and programming. This machine is so ridiculously underpowered for today’s standards (and even for 1980s standards), the inevitable question is, to what extent could I port the Computer Graphics from Scratch raytracer to a ZX Spectrum?

The ZX Spectrum has a 3.5 MHz Z80 processor (1,000 times slower than current computers) that can’t multiply numbers (!!!), 48 KB of RAM (1,000,000 times smaller), and a 256x176 graphics mode (~200 times lower resolution) capable of displaying 15 colors (1,000,000 times fewer – and with some unusual quirks). This is an interesting setup for a CPU-intensive graphics application!

My plan was to implement this in Sinclair BASIC, the built-in programming language of the Spectrum. This is not just BASIC, but an ancient, very limited dialect of BASIC. For example, the only control structures are FOR and IF (and IF has no ELSE or even ENDIF); all variables are global; there are no function calls, only GO TO and GO SUB; etc. It’s also interpreted, so super slow. But at least it implements multiplications in software! I could always rewrite the raytracer in assembler if I cared about performance.

I set up a minimal environment: I write BASIC code using VS Code, compile it using BAS2TAP, and run it on the FUSE emulator. This gave me a pretty decent iteration speed.

As an aside, I hadn’t written BASIC in something like 30 years, and I was surprised at how quickly it all came back. I was between 4 and 10 when I was doing this, so I guess it sticks in the brain like anything you learn at that age, like languages and accents. Now let’s get coding like it’s 1984!

8 Likes

The way the Spectrum display works, you can only have 15 colours at a resolution of 32 x 44 (or maybe 64 x 22) since you’re limited to two colours per attribute square. And you can’t choose any 15 colours - you’re stuck with a fixed palette of fairly saturated hues. Not much use for ray tracing, I would think.

It’s a nice progression the author shows - he ends up with some dithered results. It’s really rather good - especially for a “weekend coding project”!

2 Likes

I agree. Very good for a Spectrum display.

Well, I disagree, it’s definitely not pointless. I also caught the ZX80 bug in the 80’s and that eventually led me to create a new fantasy retro computer inspired by it (and other platforms).
I totally love what you did and I just added a task in a checklist (for later) to adapt your project to Continuum 93’s assembly language and see what it looks like (if you don’t mind, that is). :smiley:
Thank you for this, very impressive!

1 Like

I was into raytracers like POVray (DKBTrace) and spent many weekends running scripts to get 320x240 pictures. I even wrote a front end scene editor for POVray (see https://mse.sourceforge.net/) and it nearly turned commercial but that didn’t quite work out.

I was working with Acorn Archimedes by then, and started moving to PC’s when I created my first scene editor.

2 Likes

You can do a bit better than that although you’d need to compute it before you could display it really because you burn CPU doing fancy graphics on a Spectrum. Not sure 64x48 is much of an improvement though