Git Product home page Git Product logo

rtzx's Introduction

RTZX


Simple raytracer that works on ZX Spectrum 48K and above in under 45 seconds.

A contribution to 1KiB procedural gfx compo at Chaos Constructions 2024 by RCL of Virtual Vision Group.

Source code in Z80 assembly.


Algo

A 85 x 64 grid is traced, and in each grid point we get a [0..16] brightness value from the raytracer. To stretch the result to the whole 256 x 192 screen, we proceed to render as many dots around each grid point as its brightness, using a very crude Gaussian approximation to disperse them, and also set the attributes accordingly. So a grid point with a value of 0 will remain black, while a grid point with a value of 9 will have 9 points rendered around it, with them being concentrated primarily in its center.

Here's the youtube video of it in action: https://www.youtube.com/watch?v=-syTIinQFQQ

Background

This is an honest raytracer, at least for a certain definition of honesty - the only inputs are the camera position and 4 geometric primitives (3 spheres and a plane). Some assumptions and tweaks have been made to make the calculation numerically easier on the poor Z80.

My inspiration was this year's (2024) article by Gabriel Gambetta (https://gabrielgambetta.com/zx-raytracer.html), which I used as a starting point. However its floating point calculations would not allow me to get anywhere close to 1 minute calculation time limit imposed by the rules of the compo, so calculations are done in a signed 9.7 fixed point. You can see the jagged edges and other artifacts due to that.

Also, I found out that none of the signed 16 bit multiplication and division routines (Z80 does not have mul/div ops in case you weren't aware) that I could find on the web worked, so I had to go back to the basics and implement them myself (perhaps not in an optimal way). If you want to improve upon then, please go ahead.

I have also written a C program on PC to debug the algorithm and tweak it numerically. I am not including that (yet?) as it is a part of my larger experimentation framework - maybe one day. You may need to calculate the values in Spheres table by hand if you want to change the positions of the spheres (they all are essentially floats multiplied by 128 and converted to shorts).

To draw the screen,the code does about 11k divisions and 148.5k multiplications, of which a lot are multiplications with 0 (13k) or 8x8 bit (85k), so fast paths exist for those. Most loops are unrolled both for speed and better compression.

Speaking of which, you may be wondering how the hell this fit 1024 bytes. Well, just like most other size-limited entries, the code is compressed, and a small kickstart of 70 something bytes is prepended to decompress it on start. When decompressed, the main code is 3604 bytes. To achieve good compression I had to use the same registers the same way as much as possible and duplicate blocks of code even when more elegant solutions could exist, so take that into account when judging the source.

Credits

This code is by RCL/VVG and hereby I am placing it into the public domain.

Extra credits

rtzx's People

Contributors

rcl avatar

Stargazers

Douglas Mencken avatar Maxim Muchkaev avatar Eugene avatar Alexey Pichugin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.