(* SRAY -- Sadol RAYtracer *) (* A simple raytracer ;p *) (* vector routines *) fun V:3 : _ ; (* vector *) op `+` : [L[0]+R[0], L[1]+R[1], L[2]+R[2]]; op `-` : [L[0]-R[0], L[1]-R[1], L[2]-R[2]]; op `*` : [L[0]*R[0], L[1]*R[1], L[2]*R[2]]; op `/` : [L[0]/R, L[1]/R, L[2]/R]; op `*.` : [L[0]*R, L[1]*R, L[2]*R]; op `.` : L[0]*R[0]+L[1]*R[1]+R[2]*R[2]; (* dot product *) fun M:1 : (_[0] `.` _[0])^(1/2); (* magnitude *) fun N:1 : {m=M(_[0]); [_[0][0]/m, _[0][1]/m, _[0][2]/m]}; (* normalize *) op `<<` : <%[LR%>; (* scene: *) (* header: [pixw,pixh, eye] *) (* eye: [x,y,z] *) (* objects: [object, object, ... ] *) (* object: [type, vec, rgb, ad] *) (* vec: [x,y,z] *) (* rgb: [r,g,b] *) (* ad: additional *) s = [ [640,480, [0,0,-5]], [ ["sph", [0,0,5], [1,0,0], 2], ["omni", [2,2,2], [1,1,1], 1] ] ]; (*raytrace*) fun R:1 : { s = _[0]; w = s[0][0]; h = s[0][1]; e = s[0][2]; (* pixels to projection plane (x,y) -> vec *) fun p:2 : [12/w*_[0]-6, 0-(9/h*_[1]-4.5), 0]; (*trace single ray (ray,depth) -> color *) fun t:2 : { (* intersection (ray, object) -> (num, num, num, ...) *) fun i:2 : { o = _[1]; r = _[0]; x = <%$0%>; if o[0] == "sph" : { a = M(r[0]); b = 2*(r[0] `.` (o[1] `-` r[1])); c = M(o[1] `-` r[1]) * o[3]^2; d = b^2 - 2*a*c; if (d < 0) : x `<<` -1 else { v = ((0-b)-d^(1/2))/a; if v >= 0 : x `<<` v; v = ((0-b)+d^(1/2))/a; if v >= 0 : x `<<` v } } else (* TODO: plane *) x `<<` -1 ; x }; out 0 }; out 0 }; R(s) (* COMPILED: ~V3_{endf V} ~M1^++*##_00##_00*##_01##_01*##_02##_02/12{endf M} ~N1(2:mM#_0 $3/##_00m/##_01m/##_02m{endlist}{)}{endf N} :s$2$3,3640,3480$300-05{endlist}{endlist}$2$4"3sph$3005{endlist}$3100{endlist}2{endlist}$4"4omni$3222{endlist}$3111{endlist}1{endlist}{endlist}{endlist} ~R1(7:s#_0 :w##s00 :h##s01 :e##s02 ~p2$3-*/,212w#_06-0-*/9h#_1+4.150{endlist}{endf p} ~t2(2~i2(5:o#_1 :r#_0 :x$0 ?=#o0"3sph(5:aM#r0 :b*2++*##r00#$3-##o10##r10-##o11##r11-##o12##r12{endlist}0*##r01#$3-##o10##r10-##o11##r11-##o12##r12{endlist}1*#$3-##o10##r10-##o11##r11-##o12##r12{endlist}2#$3-##o10##r10-##o11##r11-##o12##r12{endlist}2 :c*M$3-##o10##r10-##o11##r11-##o12##r12{endlist}^#o32 :d-^b2**2ac ?v0=v0[xv0{endif} :v/+-0b^d/12a ?|>v0=v0[xv0{endif}{)}{endif}{)}{els}[x-01{endif} x{)}{endf i} !0{)}{endf t} !0{)}{endf R} Rs *)