-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.lisp
267 lines (233 loc) · 9.04 KB
/
draw.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
(in-package :graphics)
(defvar *max-s-step* 100)
(defvar *max-u-step* 10)
(defvar *max-v-step* 10)
(defun add-point (point &key matrix)
(when (= (length point) 3)
(setf point (apply 'vector
(append (loop for i across point collect i) '(1.0)))))
(matrix-add-column matrix point))
(defun add-xyz (x y z &key matrix)
(matrix-add-column matrix (vector x y z 1.0)))
(defun add-edge (point1 point2 &key (matrix *edge-matrix*))
(add-point point1 :matrix matrix)
(add-point point2 :matrix matrix))
(defun add-edges (matrix)
(matrix-append *edge-matrix* matrix))
(defun clear-edge-matrix ()
(setf *edge-matrix* (make-matrix :dimensions '(4 0))))
(defun clear-triangle-matrix ()
(setf *triangle-matrix* (make-matrix :dimensions '(4 0))))
(defun clear-coordinate-systems ()
(setf *coordinate-systems* (list (make-identity-matrix))))
(defun clear-screen ()
(setf *screen* (make-array (list *x-resolution* *y-resolution*)
:initial-element (cons *background-color* nil))))
(defun ambient-illumination (A Ka)
(round (bound (* A Ka) 0 255)))
(defun diffuse-illumination (L N R Kd)
(round (bound (* L Kd (vector-dot N R)) 0 255)))
(defun specular-illumination (L N R V Ks)
(round (bound (* L Ks (vector-dot (vector-subtract
(vector-scale (* 2 (vector-dot N R)) N) R) V))
0 255)))
(defun total-illumination (N V constant-name)
(multiple-value-bind (kar kdr ksr kag kdg ksg kab kdb ksb)
(values-list (loop
for n across (cdr (gethash constant-name *symbols*))
collect n))
(let ((Ir 0)
(Ig 0)
(Ib 0))
(let ((Iar (ambient-illumination (aref *ambient* 0) kar))
(Iag (ambient-illumination (aref *ambient* 1) kag))
(Iab (ambient-illumination (aref *ambient* 2) kab))
(Idr 0)
(Idg 0)
(Idb 0)
(Isr 0)
(Isg 0)
(Isb 0))
;;(format t "Ambient: ~a ~a ~a~%" Iar Iag Iab)
(incf Ir Iar)
(incf Ig Iag)
(incf Ib Iab)
(loop
for key being the hash-keys of *symbols* using (hash-value value)
when (string-equal (car value) "light")
do
(let* ((light (cdr value))
(R (vector-normalize (subseq light 0 3))))
(incf Idr (diffuse-illumination (aref light 3) N R kdr))
(incf Idg (diffuse-illumination (aref light 4) N R kdg))
(incf Idb (diffuse-illumination (aref light 5) N R kdb))
(incf Isr (specular-illumination (aref light 3) N R V ksr))
(incf Isg (specular-illumination (aref light 4) N R V ksg))
(incf Isb (specular-illumination (aref light 5) N R V ksb))))
;;(format t "Diffuse: ~a ~a ~a~%" Idr Idg Idb)
;;(format t "Specular: ~a ~a ~a~%" Isr Isg Isb)
(incf Ir (+ Idr Isr))
(incf Ig (+ Idg Isg))
(incf Ib (+ Idb Isb))
;;(format t "Total: ~a ~a ~a~%" Ir Ig Ib)
(apply 'vector (mapcar #'(lambda (n) (round (bound n 0 255))) (list Ir Ig Ib)))))))
(defun draw-triangles (&optional (constant-name nil))
(setf *triangle-matrix* (matrix-matrix-mult (get-coordinate-system) *triangle-matrix*))
(loop
for i from 0 below (matrix-dimension *triangle-matrix* 1) by 3
do
(let* ((point1 (matrix-get-column *triangle-matrix* i))
(point2 (matrix-get-column *triangle-matrix* (+ i 1)))
(point3 (matrix-get-column *triangle-matrix* (+ i 2)))
(normal (triangle-normal point1 point2 point3))
color)
(when (> (aref normal 2) 0)
(setf color (if constant-name
(total-illumination normal *view-vector* constant-name)
*foreground-color*))
(draw-triangle point1 point2 point3 color)))))
(defun draw-triangle (point1 point2 point3 &optional (color *foreground-color*))
(cond
((string-equal *shading-type* "wireframe")
(draw-line point1 point2)
(draw-line point2 point3)
(draw-line point1 point3))
((string-equal *shading-type* "flat")
(scan-line point1 point2 point3 color))))
(defun scan-line (point1 point2 point3 &optional (color *foreground-color*))
(multiple-value-bind (bottom middle top)
(values-list (sort (list point1 point2 point3) #'< :key #'(lambda (point)
(aref point 1))))
(let* ((Bx (aref bottom 0))
(By (round (aref bottom 1)))
(Bz (aref bottom 2))
(Mx (aref middle 0))
(My (round (aref middle 1)))
(Mz (aref middle 2))
(Tx (aref top 0))
(Ty (round (aref top 1)))
(Tz (aref top 2))
(dx0 (if (= Ty By)
0
(/ (- Tx Bx) (- Ty By))))
(dx1 (if (= My By)
0
(/ (- Mx Bx) (- My By))))
(dz0 (if (= Ty By)
0
(/ (- Tz Bz) (- Ty By))))
(dz1 (if (= My By)
0
(/ (- Mz Bz) (- My By))))
(mode 0))
(loop
for x0 = Bx then (+ x0 dx0)
for x1 = Bx then (+ x1 dx1)
for y from By to Ty
for z0 = Bz then (+ z0 dz0)
for z1 = Bz then (+ z1 dz1)
for p0 = (vector (round x0) y z0)
for p1 = (vector (round x1) y z1)
do
(draw-line p0 p1 color)
when (and (= mode 0) (>= y My))
do
(setf mode 1)
(setf x1 Mx)
(setf z1 Mz)
(setf dx1 (if (= Ty My)
0
(/ (- Tx Mx) (- Ty My))))
(setf dz1 (if (= Ty My)
0
(/ (- Tz Mz) (- Ty My))))))))
(defun draw-edges (&key (matrix *edge-matrix*) color-function)
(setf matrix (matrix-matrix-mult (get-coordinate-system) matrix))
(loop
for i from 0 below (matrix-dimension matrix 1) by 2
do
(let ((point1 (matrix-get-column matrix i))
(point2 (matrix-get-column matrix (+ i 1))))
(draw-line point1 point2 :color-function color-function))))
(defun push-coordinate-system ()
(setf *coordinate-systems* (cons (matrix-copy (car *coordinate-systems*)) *coordinate-systems*)))
(defun pop-coordinate-system ()
(setf *coordinate-systems* (cdr *coordinate-systems*)))
(defun get-coordinate-system ()
(car *coordinate-systems*))
(defun set-coordinate-system (system)
(setf (car *coordinate-systems*) system))
(defun draw-line (point1 point2 &optional (color *foreground-color*))
(let ((x1 (aref point1 0))
(y1 (aref point1 1))
(z1 (aref point1 2))
(x2 (aref point2 0))
(y2 (aref point2 1))
(z2 (aref point2 2)))
(if (< x2 x1)
(draw-line point2 point1 color)
(let* ((dx (- x2 x1))
(dy (- y2 y1))
(dz (- z2 z1))
(A dy)
(B (- dx)))
(cond
((= dx dy 0)
(plot-pixel x1 y1 z1 color)
(plot-pixel x2 y2 z2 color))
((= dx 0)
(loop
with x = x1
for y from y1 to y2
for z from z1 by (/ dz dy)
do (plot-pixel x y z color)))
((= dy 0)
(loop
for x from x1 to x2
with y = y1
for z from z1 by (/ dz dx)
do (plot-pixel x y z color)))
((and (>= dy 0) (< dy dx))
(loop
for x from x1 to x2
with y = y1
for z from z1 by (/ dz dx)
for d = (+ (* A 2) B) then (+ d (* A 2))
do (plot-pixel x y z color)
when (> d 0)
do
(incf y)
(incf d (* B 2))))
((>= dy dx)
(loop
with x = x1
for y from y1 to y2
for z from z1 by (/ dz dy)
for d = (+ A (* B 2)) then (+ d (* B 2))
do (plot-pixel x y z color)
when (< d 0)
do
(incf x)
(incf d (* A 2))))
((and (< dy 0) (<= (- dy) dx))
(loop
for x from x1 to x2
with y = y1
for z from z1 by (/ dz dx)
for d = (- (* A 2) B) then (+ d (* A 2))
do (plot-pixel x y z color)
when (< d 0)
do
(decf y)
(decf d (* B 2))))
((> (- dy) dx)
(loop
with x = x1
for y from y1 downto y2
for z from z1 by (/ dz dy)
for d = (- A (* B 2)) then (- d (* B 2))
do (plot-pixel x y z color)
when (> d 0)
do
(incf x)
(incf d (* A 2)))))))))