1 (in-package :tuple-trace
)
3 (defconstant +trace-depth
+ 6)
6 ;; (defclass raytracer ()
7 ;; ((scene :accessor scene-of)
8 ;; (screen :accessor screen-of :initform (list -4.0 4.0 3.0 -3.0))))
10 ;; (def-tuple-op add-diffuse-colour
11 ;; ((source-colour colour (rs gs bs as))
13 ;; (diffuse-color colour (rd gd bd ad))
14 ;; (light-colour colour (rl gl bl al)))
15 ;; (colour-tuple (+ (* diffusion rd rl) rs)
16 ;; (+ (* diffusion gd gl) gs)
17 ;; (+ (* diffusion bd bl) bs)
20 ;; (defun compute-intersection-colour (scene primitive intersection)
21 ;; (let ((intersection-colour (new-colour)))
23 ;; for light across (primitives-of scene)
24 ;; when (typep primitive 'light)
25 ;; do (let* ((light-vector
28 ;; (delta-vector3d (vertex3d intersection)
29 ;; (vertex3d (centre-of light))))))
30 ;; (normal (normal primitive intersection))
31 ;; (material-colour (colour-of (material-of primitive)))
32 ;; (light-colour (colour-of (material-of light)))
34 ;; (vector3d light-vector)
35 ;; (vector3d normal))))
37 ;; (setf (colour intersection-colour)
38 ;; (add-diffuse-colour
39 ;; (colour intersection-colour)
40 ;; (diffusion-of (material-of primitive))
41 ;; (colour material-colour)
42 ;; (colour light-colour))))))
43 ;; intersection-colour))
45 ;; (defmethod raytrace ((ray ray) (tracer raytracer) &optional (depth 0))
46 ;; (unless (> depth +trace-depth+)
47 ;; (let* ((scene (scene-of tracer))
48 ;; (primitives (primitives-of scene))
49 ;; (distance 11000000.0)
50 ;; (intersection-colour (new-colour)))
52 ;; for primitive across primitives
54 ;; (let ((intersection (intersect primitive ray distance)))
56 ;; (if (typep primitive 'light)
57 ;; (make-colour (colour-tuple 1.0 1.0 1.0 0.0))
59 ;; (setf intersection-colour
60 ;; (compute-intersection-colour scene primitive intersection)))))))
61 ;; intersection-colour)))
63 ;; (defmethod render ((raytracer raytracer))
64 ;; (let* ((png (make-instance 'png :width 320 :height 200))
65 ;; (image (data-array png))
66 ;; (delta-x (/ (- (third (screen-of raytracer)) (first (screen-of raytracer))) (width png)))
67 ;; (delta-y (/ (- (fourth (screen-of raytracer)) (second (screen-of raytracer))) (height png))))
69 ;; for current-y = (second (screen-of raytracer)) then (+ current-y delta-y)
70 ;; for target-y from 0 below (height png)
73 ;; for target-x from 0 below (width png)
74 ;; for current-x = (first (screen-of raytracer)) then (+ current-x delta-x)
76 ;; (let ((ray (make-instance 'ray)))
78 ;; (setf (direction-of ray)
79 ;; (make-vector3d (vector3d-normal (vector3d-tuple current-x current-y -5.0))))
80 ;; (setf (origin-of ray)
81 ;; (make-vector3d (vector3d-tuple 0.0 0.0 -5.0)))
83 ;; (colour (raytrace ray raytracer))
84 ;; (red green blue alpha)
85 ;; (setf (aref image target-y target-x 0) (round (* 255 red)))
86 ;; (setf (aref image target-y target-x 1) (round (* 255 green)))
87 ;; (setf (aref image target-y target-x 2) (round (* 255 blue))))))))
91 ;; to do -- far/near clipping?
93 (defun render (camera scene image
)
94 (let ((pixel-dx (/ (window-width-of camera
)
96 (pixel-dy (/ (window-height-of camera
)
98 (ray (make-instance 'ray
))
99 (sphere-colour (make-colour* 1.0 0.0 0.0 0.0))
100 (background-colour (make-colour* 1.0 1.0 1.0 0.0)))
102 for pixel-x from
0 below
(width-of image
)
105 (format t
"Tracing row ~A~%" pixel-x
)
107 for pixel-y from
0 below
(height-of image
)
110 ((xp (+ (* 0.5 pixel-dx
) (* pixel-x pixel-dx
) (window-left-of camera
)))
111 (yp (+ (* 0.5 pixel-dy
) (* pixel-x pixel-dy
) (window-top-of camera
)))
113 ;; (format t "XP ~A YP ~A~%" xp yp)
114 (setf (origin-of ray
) (position-of camera
))
115 (setf (direction-of ray
) (vector3d-difference
118 (position-of camera
)))
120 for primitive across
(primitives-of scene
)
122 (let ((intersection (intersect primitive ray
)))
124 (setf (pixel-of image pixel-x pixel-y
) sphere-colour
)
125 (format nil
"Intersections @ ~A " intersection
))
126 (setf (pixel-of image pixel-x pixel-y
) background-colour
)))))))))