Dave Cope's Homepage
   Main | Personal | Photo Gallery | Projects | Linux | Employment Info. | Links | Misc. | About e-mail  

C64 Programming

Perhaps this also falls under "Raytracing / Image Synthesis", but I decided it deserved its own section. :-) I have also started work on a raytracer that can run on a C64. If you don't know a lot about raytracing, it may surprise you that it requires very little memory to calculate a very interesting image. In fact, it is possible to use procedural objects and textures to avoid large data sets. Since I haven't seen a "real" raytracer on the C64, I decided to write one.

The C64 has an 8-bit 6510 processor (same instruction set as the 6502). It runs at just under 1Mhz!! I thought that I might be able to write the raytracer in C64 Basic, but I scrapped that idea because the raytracer would be extremely slow, and C64 Basic doesn't even have functions! It would be very difficult to write a raytracer in the language even if speed wasn't an issue. So, I decided to write the raytracer using cc65 (an almost C compiler).

My next problem was figuring out how to perform floating point math operations. The cc65 compiler doesn't support any floating point number formats. So, I had to perform these operations without the compiler. Also, the 6502 doesn't handle floating point numbers like most current processors. In fact, the processor doesn't even have a multiplication instruction. But, the Basic language does support floating point numbers. So, I wrote functions in C/Assembly that "stuff" the areas in memory that Basic uses as floating point registers. Then I call the Basic ROM routines to calculate the result for me.

Rendering of a sphere

Initially my program just rasterized a sphere (i.e. no rays were cast). As you can see, I must dither the image so I can display it on the C64. It takes almost 30min for the sphere to render on a C64! Luckily I can run the program much faster on my PC using a C64 emulator. I am also able to run the raytracer natively on my PC. I placed all the machine specific code in different modules, so I am able to compile the raytracer for the C64 or under Linux.

Raytraced image showing reflections

Here is a much newer image I generated. This time I am actually ray tracing instead of rasterizing the image. The raytracer now supports two types of primitives; spheres and planes. The raytracer also supports different material types. At the moment it supports a base material that contains diffuse colour, specular colour, and reflectivity attributes. I also have a checkerboard material that is almost the same as the base material, but it has two different diffuse colours.

Raytraced image showing reflections (in full colour)

Here you can see a true-colour version of the scene show above. I have modified the raytracer so it writes images out to disk as true colour PPM files. The image here was actally rendered on the c64.
I didn't precisely time how long this image took to render, but I think it would take about five hours to render on a c64. My 800Mhz Athlon renders the same image in about two seconds!

Raytraced image showing reflections and shadows (in full colour)

Here is another image showing the latest features of my raytracer. Now, the raytracer supports shadows properly. I also changed the scene I was raytracing a bit (so that the FOV looks better).




This site was designed and coded by Dave Cope - © 1998-2003