Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
SML port of the 128-byte raytracer wolf128. See http://finalpatch.blogspot.dk/2014/06/dissecting-128-byte-raycaster.html. Uses the InstagraML library: https://github.com/DIKU-IP/InstagraML
(* ex: set ts=2 sw=2 et ai: *)
use "InstagraML.sml";
local
fun rayMarch (x,y,z) =
let
val xdist0 = (((x-160) * z + 4096) div 256) mod 256
val ydist = (((y-100) * z + 4096) div 256) mod 256
val xdist = case z div 64 of
0 => xdist0 - 10
| 1 => xdist0 - 40
| 2 => xdist0 - 20
| _ => xdist0 - 30
in
if (xdist >= 32 andalso ((z div 32) mod 2 <> 0)) orelse ydist >= 32
orelse z > 255 then
(xdist,ydist,z)
else
rayMarch (x, y, z+1)
end
in
fun shadeFragment (x,y) =
let
val (xdist,ydist,z) = rayMarch(x,y,0)
val texel0 = Word8.xorb(
Word8.xorb(Word8.fromInt(xdist),Word8.fromInt(ydist)),Word8.fromInt(z))
val texel = Word8.toInt(texel0) mod 16
in
(texel*16, texel*16, texel*16)
end
end;
val img = InstagraML.fromFunction (320, 200, shadeFragment);
InstagraML.writeBMP ("wolf128.bmp", img);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment