| 1 |
;;; RAY -- Ray-trace a simple scene with spheres, generating a ".pgm" file. |
| 2 |
;;; Translated to Scheme from Paul Graham's book ANSI Common Lisp, Example 9.8 |
| 3 |
|
| 4 |
(library (rnrs-benchmarks ray) |
| 5 |
(export main) |
| 6 |
(import (rnrs) (rnrs r5rs) (rnrs arithmetic flonums) (rnrs-benchmarks)) |
| 7 |
|
| 8 |
(define (make-point x y z) |
| 9 |
(vector x y z)) |
| 10 |
|
| 11 |
(define (point-x p) (vector-ref p 0)) |
| 12 |
(define (point-y p) (vector-ref p 1)) |
| 13 |
(define (point-z p) (vector-ref p 2)) |
| 14 |
|
| 15 |
(define (sq x) (fl* x x)) |
| 16 |
|
| 17 |
(define (mag x y z) |
| 18 |
(flsqrt (fl+ (sq x) (sq y) (sq z)))) |
| 19 |
|
| 20 |
(define (unit-vector x y z) |
| 21 |
(let ((d (mag x y z))) |
| 22 |
(make-point (fl/ x d) (fl/ y d) (fl/ z d)))) |
| 23 |
|
| 24 |
(define (distance p1 p2) |
| 25 |
(mag (fl- (point-x p1) (point-x p2)) |
| 26 |
(fl- (point-y p1) (point-y p2)) |
| 27 |
(fl- (point-z p1) (point-z p2)))) |
| 28 |
|
| 29 |
(define (minroot a b c) |
| 30 |
(if (flzero? a) |
| 31 |
(fl/ (fl- c) b) |
| 32 |
(let ((disc (fl- (sq b) (fl* 4.0 a c)))) |
| 33 |
(if (flnegative? disc) |
| 34 |
#f |
| 35 |
(let ((discrt (flsqrt disc)) |
| 36 |
(minus-b (fl- b)) |
| 37 |
(two-a (fl* 2.0 a))) |
| 38 |
(flmin (fl/ (fl+ minus-b discrt) two-a) |
| 39 |
(fl/ (fl- minus-b discrt) two-a))))))) |
| 40 |
|
| 41 |
(define *world* '()) |
| 42 |
|
| 43 |
(define eye (make-point 0.0 0.0 200.0)) |
| 44 |
|
| 45 |
(define (tracer pathname res) |
| 46 |
(call-with-output-file/truncate |
| 47 |
pathname |
| 48 |
(lambda (p) |
| 49 |
(let ((extent (* res 100))) |
| 50 |
(display "P2 " p) |
| 51 |
(write extent p) |
| 52 |
(display " " p) |
| 53 |
(write extent p) |
| 54 |
(display " 255" p) |
| 55 |
(newline p) |
| 56 |
(do ((y 0 (+ y 1))) |
| 57 |
((= y extent)) |
| 58 |
(do ((x 0 (+ x 1))) |
| 59 |
((= x extent)) |
| 60 |
(write (color-at |
| 61 |
(fl+ -50.0 |
| 62 |
(fl/ (exact->inexact x) (exact->inexact res))) |
| 63 |
(fl+ -50.0 |
| 64 |
(fl/ (exact->inexact y) (exact->inexact res)))) |
| 65 |
p) |
| 66 |
(newline p))))))) |
| 67 |
|
| 68 |
(define (color-at x y) |
| 69 |
(let ((ray (unit-vector (fl- x (point-x eye)) |
| 70 |
(fl- y (point-y eye)) |
| 71 |
(fl- (point-z eye))))) |
| 72 |
(inexact->exact (flround (fl* (sendray eye ray) 255.0))))) |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
(define (sendray pt ray) |
| 77 |
(let* ((x (first-hit pt ray)) |
| 78 |
(s (vector-ref x 0)) |
| 79 |
(int (vector-ref x 1))) |
| 80 |
(if s |
| 81 |
(fl* (lambert s int ray) |
| 82 |
(surface-color s)) |
| 83 |
0.0))) |
| 84 |
|
| 85 |
(define (first-hit pt ray) |
| 86 |
(let loop ((lst *world*) (surface #f) (hit #f) (dist 1e308)) |
| 87 |
(if (null? lst) |
| 88 |
(vector surface hit) |
| 89 |
(let ((s (car lst))) |
| 90 |
(let ((h (intersect s pt ray))) |
| 91 |
(if h |
| 92 |
(let ((d (distance h pt))) |
| 93 |
(if (fl<? d dist) |
| 94 |
(loop (cdr lst) s h d) |
| 95 |
(loop (cdr lst) surface hit dist))) |
| 96 |
(loop (cdr lst) surface hit dist))))))) |
| 97 |
|
| 98 |
(define (lambert s int ray) |
| 99 |
(let ((n (normal s int))) |
| 100 |
(flmax 0.0 |
| 101 |
(fl+ (fl* (point-x ray) (point-x n)) |
| 102 |
(fl* (point-y ray) (point-y n)) |
| 103 |
(fl* (point-z ray) (point-z n)))))) |
| 104 |
|
| 105 |
(define (make-sphere color radius center) |
| 106 |
(vector color radius center)) |
| 107 |
|
| 108 |
(define (sphere-color s) (vector-ref s 0)) |
| 109 |
(define (sphere-radius s) (vector-ref s 1)) |
| 110 |
(define (sphere-center s) (vector-ref s 2)) |
| 111 |
|
| 112 |
(define (defsphere x y z r c) |
| 113 |
(let ((s (make-sphere c r (make-point x y z)))) |
| 114 |
(set! *world* (cons s *world*)) |
| 115 |
s)) |
| 116 |
|
| 117 |
(define (surface-color s) |
| 118 |
(sphere-color s)) |
| 119 |
|
| 120 |
(define (intersect s pt ray) |
| 121 |
(sphere-intersect s pt ray)) |
| 122 |
|
| 123 |
(define (sphere-intersect s pt ray) |
| 124 |
(let* ((xr (point-x ray)) |
| 125 |
(yr (point-y ray)) |
| 126 |
(zr (point-z ray)) |
| 127 |
(c (sphere-center s)) |
| 128 |
(n (minroot |
| 129 |
(fl+ (sq xr) (sq yr) (sq zr)) |
| 130 |
(fl* 2.0 |
| 131 |
(fl+ (fl* (fl- (point-x pt) (point-x c)) xr) |
| 132 |
(fl* (fl- (point-y pt) (point-y c)) yr) |
| 133 |
(fl* (fl- (point-z pt) (point-z c)) zr))) |
| 134 |
(fl+ (sq (fl- (point-x pt) (point-x c))) |
| 135 |
(sq (fl- (point-y pt) (point-y c))) |
| 136 |
(sq (fl- (point-z pt) (point-z c))) |
| 137 |
(fl- (sq (sphere-radius s))))))) |
| 138 |
(if n |
| 139 |
(make-point (fl+ (point-x pt) (fl* n xr)) |
| 140 |
(fl+ (point-y pt) (fl* n yr)) |
| 141 |
(fl+ (point-z pt) (fl* n zr))) |
| 142 |
#f))) |
| 143 |
|
| 144 |
(define (normal s pt) |
| 145 |
(sphere-normal s pt)) |
| 146 |
|
| 147 |
(define (sphere-normal s pt) |
| 148 |
(let ((c (sphere-center s))) |
| 149 |
(unit-vector (fl- (point-x c) (point-x pt)) |
| 150 |
(fl- (point-y c) (point-y pt)) |
| 151 |
(fl- (point-z c) (point-z pt))))) |
| 152 |
|
| 153 |
(define (ray-test . opt) |
| 154 |
(set! *world* '()) |
| 155 |
(defsphere 0.0 -300.0 -1200.0 200.0 0.8) |
| 156 |
(defsphere -80.0 -150.0 -1200.0 200.0 0.7) |
| 157 |
(defsphere 70.0 -100.0 -1200.0 200.0 0.9) |
| 158 |
(do ((x -2 (+ x 1))) |
| 159 |
((> x 2)) |
| 160 |
(do ((z 2 (+ z 1))) |
| 161 |
((> z 7)) |
| 162 |
(defsphere |
| 163 |
(fl* (exact->inexact x) 200.0) |
| 164 |
300.0 |
| 165 |
(fl* (exact->inexact z) -400.0) |
| 166 |
40.0 |
| 167 |
0.75))) |
| 168 |
(tracer "spheres.pgm" (if (null? opt) 1 (car opt)))) |
| 169 |
|
| 170 |
(define (run) |
| 171 |
(ray-test 1) |
| 172 |
'ok) |
| 173 |
|
| 174 |
(define (main . args) |
| 175 |
(run-benchmark |
| 176 |
"ray" |
| 177 |
ray-iters |
| 178 |
(lambda (result) |
| 179 |
(equal? result 'ok)) |
| 180 |
(lambda () (lambda () (run)))))) |