1 (in-package #:clnl-interface)
3 (defvar *turtle-lists* nil)
4 (defvar *patch-list* nil)
6 (defvar *glut-window-opened* nil)
7 (defvar *dimensions* nil)
8 (defvar *window-width* 1024)
9 (defvar *window-height* 768)
11 (defvar *default-shapes* nil)
13 (defvar *textbox* nil)
14 (defvar *inputbox* nil)
16 ; For now, shapes can live in here
18 ; * name <like default>
19 ; * rotatable (equal to "true" if yes)
21 ; then after, the elements are like so:
23 ; filled == filled in (always for now, ha)
24 ; marked == use the turtle color instead of a color
25 ; polygon -> Polygon <color> <filled> <marked> <alternating x y coords>
26 ; circle -> Circle <color> <filled> <marked> <left> <top> <diameter> ; here, the left and top are NOT the center
27 ; rectangle -> Rectangle <color> <filled> <marked> <left> <top> <right> <bottom>
29 ; then ends with an empty string
31 (defun parse-circle (sections)
33 :color (parse-integer (car sections))
34 :filled (string= (nth 1 sections) "true")
35 :marked (string= (nth 2 sections) "true")
36 :left (parse-integer (nth 3 sections))
37 :top (parse-integer (nth 4 sections))
38 :diameter (parse-integer (nth 5 sections))))
40 (defun parse-rectangle (sections)
43 :color (parse-integer (car sections))
44 :filled (string= (nth 1 sections) "true")
45 :marked (string= (nth 2 sections) "true")
46 :left (parse-integer (nth 3 sections))
47 :top (parse-integer (nth 4 sections))
48 :right (parse-integer (nth 5 sections))
49 :bottom (parse-integer (nth 6 sections))))
51 (defun parse-polygon (sections)
53 ((parse-points (sections)
56 (list (- 300 (parse-integer (car sections))) (parse-integer (cadr sections)))
57 (parse-points (cddr sections))))))
60 :color (parse-integer (car sections))
61 :filled (string= (nth 1 sections) "true")
62 :marked (string= (nth 2 sections) "true")
63 :coords (parse-points (nthcdr 3 sections)))))
65 (defun parse-shape (str)
67 ((parse-element (line)
69 ((sections (cl-ppcre:split " " line)))
71 ((string= (car sections) "Circle") (parse-circle (cdr sections)))
72 ((string= (car sections) "Rectangle") (parse-rectangle (cdr sections)))
73 ((string= (car sections) "Polygon") (parse-polygon (cdr sections))))))
76 ((line (read-line str nil)))
77 (when (and line (string/= line ""))
82 ((next-line (read-line str nil)))
86 :rotatable (string= "true" (read-line str))
87 :rgb (read-line str) ; this is ignored for now, I think
88 :elements (parse-elements))))))
90 ; Clipping ears algorithm. This can be slow due to the fact that it will only be run once.
91 (defun triangulate (points &optional (ccw :unknown))
94 (< 0 (- (* (- (car y) (car x)) (- (cadr z) (cadr x))) (* (- (car z) (car x)) (- (cadr y) (cadr x))))))
95 (tri-is-concave (x y z) (if (tri-is-ccw x y z) (not ccw) ccw))
96 (poly-is-ccw (points &optional cur-tri)
99 (poly-is-ccw (append points (list (car points))) (list (car (last points)) (car points) (cadr points))))
100 ((eql (length points) 2)
101 (apply #'tri-is-ccw cur-tri))
103 (< (car (cadr points)) (car (cadr cur-tri)))
105 (= (car (cadr points)) (car (cadr cur-tri)))
106 (< (cadr (cadr points)) (cadr (cadr cur-tri)))))
107 (poly-is-ccw (cdr points) (subseq points 0 3)))
108 (t (poly-is-ccw (cdr points) cur-tri))))
109 (point-in-tri (x y z p)
110 ; Barycentric system test
112 ((denom (+ (* (- (cadr y) (cadr z)) (- (car x) (car z))) (* (- (car z) (car y)) (- (cadr x) (cadr z)))))
113 (a (/ (+ (* (- (cadr y) (cadr z)) (- (car p) (car z))) (* (- (car z) (car y)) (- (cadr p) (cadr z)))) denom))
114 (b (/ (+ (* (- (cadr z) (cadr x)) (- (car p) (car z))) (* (- (car x) (car z)) (- (cadr p) (cadr z)))) denom))
116 (and (<= 0 a 1) (<= 0 b 1) (<= 0 c 1))))
117 (no-points-in-tri (tri points)
118 (every (lambda (point) (not (point-in-tri (car tri) (cadr tri) (caddr tri) point))) points))
119 (tri-is-actually-line (x y z)
120 (zerop (+ (* (- (cadr y) (cadr z)) (- (car x) (car z))) (* (- (car z) (car y)) (- (cadr x) (cadr z)))))))
122 ((not (find :end points)) (triangulate (append points (list :end)) ccw))
123 ((< (length points) 4) (error "Must have at least 3 points..."))
124 ((= (length points) 4) (list (remove :end points)))
125 ((eql ccw :unknown) (triangulate points (poly-is-ccw (remove :end points))))
126 ((eql :end (car points)) (error "This polygon may not be triangulateable"))
129 ((endless (remove :end points))
130 (tri (subseq endless 0 3)))
132 ((apply #'tri-is-actually-line tri)
133 (triangulate (cons (car endless) (cddr endless)) ccw))
134 ((apply #'tri-is-concave tri)
135 (triangulate (append (cdr points) (list (car points))) ccw))
136 ((no-points-in-tri tri (nthcdr 3 endless))
137 (cons tri (triangulate (cons (car endless) (cddr endless)) ccw)))
138 (t (triangulate (append (cdr points) (list (car points))) ccw))))))))
140 (defun element->gl-list (shape)
142 (when (not (getf (cdr shape) :marked))
143 (gl:push-attrib :all-attrib-bits)
145 (/ (ash (ldb (byte 24 0) (getf (cdr shape) :color)) -16) 255)
146 (/ (ash (ldb (byte 16 0) (getf (cdr shape) :color)) -8) 255)
147 (/ (ldb (byte 8 0) (getf (cdr shape) :color)) 255)))
148 (gl:begin :triangles)
152 (lambda (point) (gl:vertex (car point) (cadr point) 0))
153 (apply #'append (triangulate (getf (cdr shape) :coords)))))
156 (lambda (point) (gl:vertex (car point) (cadr point) 0))
160 (list (- 300 (getf (cdr shape) :left)) (getf (cdr shape) :top))
161 (list (- 300 (getf (cdr shape) :right)) (getf (cdr shape) :top))
162 (list (- 300 (getf (cdr shape) :right)) (getf (cdr shape) :bottom))
163 (list (- 300 (getf (cdr shape) :left)) (getf (cdr shape) :bottom)))))))
166 (lambda (point) (gl:vertex (car point) (cadr point) 0))
171 :with c := (strictmath:cos (strictmath:to-radians 1))
172 :with s := (strictmath:sin (strictmath:to-radians 1))
173 :with r := (/ (getf (cdr shape) :diameter) 2)
174 :with left := (getf (cdr shape) :left)
175 :with top := (getf (cdr shape) :top)
177 :for x := r :then (- (* c x) (* s y))
178 :for y := 0 :then (+ (* s n) (* c y))
179 :collect (list (- 300 (+ (+ x left) r)) (+ (+ y top) r))))))))
181 (when (not (getf (cdr shape) :marked))
184 (defun parse-shapes (str)
186 ((shape (parse-shape str)))
187 (when shape (cons shape (parse-shapes str)))))
189 (defun default-shapes ()
190 (with-open-file (str "resources/defaultshapes") (parse-shapes str)))
192 (eval-when (:load-toplevel)
193 (when (probe-file "resources/defaultshapes")
194 (setf *default-shapes* (default-shapes))))
197 '((140 140 140) ; gray (5)
198 (215 48 39) ; red (15)
199 (241 105 19) ; orange (25)
200 (156 109 70) ; brown (35)
201 (237 237 47) ; yellow (45)
202 (87 176 58) ; green (55)
203 (42 209 57) ; lime (65)
204 (27 158 119) ; turquoise (75)
205 (82 196 196) ; cyan (85)
206 (43 140 190) ; sky (95)
207 (50 92 168) ; blue (105)
208 (123 78 163) ; violet (115)
209 (166 25 105) ; magenta (125)
210 (224 126 149) ; pink (135)
212 (255 255 255))) ; white
214 (defun nl-color->rgb (color)
216 ((step (+ (/ (- (mod (floor (* color 10)) 100) 50) 50.48) 0.012)))
218 (lambda (x) (/ (+ x (floor (* (if (< step 0d0) x (- 255 x)) step))) 255))
219 (nth (floor color 10) *colors*))))
221 (defun render-scene ()
222 (gl:matrix-mode :projection)
223 (gl:with-pushed-matrix
226 (floor (* (- (getf *dimensions* :xmin) 0.5) (patch-size)))
227 (floor (* (+ (getf *dimensions* :xmax) 0.5) (patch-size)))
228 (floor (* (- (getf *dimensions* :ymin) 0.5) (patch-size)))
229 (floor (* (+ (getf *dimensions* :ymax) 0.5) (patch-size)))
231 (gl:matrix-mode :modelview)
232 (gl:with-pushed-matrix
234 (destructuring-bind (turtles patches) (clnl-nvm:current-state)
238 ((color (nl-color->rgb (getf patch :color))))
239 (gl:color (car color) (cadr color) (caddr color)))
240 (gl:with-pushed-matrix
241 (gl:translate (* (getf patch :xcor) (patch-size)) (* (getf patch :ycor) (patch-size)) 0)
242 (gl:translate (floor (* -.5d0 (patch-size))) (floor (* -.5d0 (patch-size))) 0)
243 (gl:scale (patch-size) (patch-size) 1)
244 (gl:call-list *patch-list*)))
249 ((color (nl-color->rgb (getf turtle :color))))
250 (gl:color (car color) (cadr color) (caddr color)))
252 (lambda (x-modification y-modification)
253 (gl:with-pushed-matrix
254 (gl:translate (* (getf turtle :xcor) (patch-size)) (* (getf turtle :ycor) (patch-size)) 0)
255 (gl:translate x-modification y-modification 0)
257 ((turtle-list (find (getf turtle :shape) *turtle-lists* :test #'string= :key #'car)))
259 (when (second turtle-list)
260 (gl:rotate (getf turtle :heading) 0 0 -1))
261 (gl:scale (patch-size) (patch-size) 1)
262 (gl:scale (getf turtle :size) (getf turtle :size) 1)
263 (gl:call-list (third turtle-list))))))
264 (list 0 (1- (world-width-in-pixels)) (- (1- (world-width-in-pixels))) 0 0)
265 (list 0 0 0 (1- (world-height-in-pixels)) (- (1- (world-height-in-pixels))))))
267 (gl:matrix-mode :projection))
270 (defun render-widgets ()
271 (clnl-gltk:render *textbox*)
272 (clnl-gltk:render *inputbox*))
275 (gl:clear :color-buffer-bit :depth-buffer-bit)
277 ((width (world-width-in-pixels))
278 (height (world-height-in-pixels)))
279 (gl:viewport 0 0 *window-width* *window-height*)
281 (gl:matrix-mode :projection)
282 (gl:with-pushed-matrix
284 (gl:ortho 0 *window-width* 0 *window-height* 0 5000)
288 (gl:vertex (- *window-width* width 10) (- *window-height* height 10))
289 (gl:vertex (- *window-width* width 10) (- *window-height* 9))
291 (gl:vertex (- *window-width* width 10) (- *window-height* 9))
292 (gl:vertex (- *window-width* 9) (- *window-height* 9))
294 (gl:vertex (- *window-width* 9) (- *window-height* 9))
295 (gl:vertex (- *window-width* 9) (- *window-height* height 10))
297 (gl:vertex (- *window-width* 9) (- *window-height* height 10))
298 (gl:vertex (- *window-width* width 10) (- *window-height* height 10))
301 (gl:viewport (- *window-width* width 10) (- *window-height* height 10) width height)
306 (cl-glut:swap-buffers))
309 (cl-glut:post-redisplay))
312 ;(glut:leave-main-loop)
313 (sb-ext:exit :abort t))
315 (defun reshape (width height)
316 (when (and (/= 0 width) (/= 0 height))
317 (setf *window-width* width)
318 (setf *window-height* height)
320 ((box-width (truncate (- width 12) clnl-gltk:*font-width*)))
321 (clnl-gltk:resize *textbox* box-width 12)
322 (clnl-gltk:resize *inputbox* box-width 1))))
326 (with-output-to-string (*standard-output*)
327 (clnl:run-commands str))
328 (error (e) (format nil "Ok, something went wrong: ~A~%Try :help" e))))
330 (defun key-pressed (key x y)
331 (declare (ignore x y))
334 ((cmd (clnl-gltk:value *inputbox*))
335 (resp (execute cmd)))
336 (setf (clnl-gltk:textbox-text *textbox*) (format nil "> ~A~%~%~A" cmd resp))
337 (clnl-gltk:clear *inputbox*))
338 (clnl-gltk:key-pressed *inputbox* key)))
340 (cffi:defcallback display :void () (display))
341 (cffi:defcallback idle :void () (idle))
342 (cffi:defcallback close-func :void () (close-func))
343 (cffi:defcallback reshape :void ((width :int) (height :int)) (reshape width height))
344 (cffi:defcallback key-pressed :void ((key :uchar) (x :int) (y :int)) (key-pressed key x y))
345 (cffi:defcallback special-key-pressed :void ((key glut:special-keys) (x :int) (y :int)) (key-pressed key x y))
347 (defun set-turtle-lists ()
356 (getf shape :rotatable)
358 (gl:with-new-list ((third turtle-list) :compile)
359 (gl:rotate 180d0 0d0 0d0 -1d0)
360 (gl:scale (/ 1d0 300d0) (/ 1d0 300d0) 1)
361 (gl:translate -150d0 -150d0 -0.0d0)
362 (mapcar #'element->gl-list (getf shape :elements)))
364 (or *default-shapes* (default-shapes)))))
366 (defun set-patch-list ()
367 (setf *patch-list* (gl:gen-lists 1))
368 (gl:with-new-list (*patch-list* :compile)
376 (defvar *initial-banner*
379 / \\ Welcome to CLNL version ~A!
383 CLNL is an experiment at creating an alternate implementation of NetLogo.
385 You can enter in various netlogo commands below, or use :q to quit the program.
387 See http://github.com/frankduncan/clnl for more information about CLNL and to
388 keep apprised of any updates that may happen.")
390 (defun initialize (&key dims)
391 "INITIALIZE &key DIMS => RESULT
393 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX :patch-size PATCH-SIZE)
395 ARGUMENTS AND VALUES:
398 XMIN: An integer representing the minimum patch coord in X
399 XMAX: An integer representing the maximum patch coord in X
400 YMIN: An integer representing the minimum patch coord in Y
401 YMAX: An integer representing the maximum patch coord in Y
402 PATCH-SIZE: A double representing the size of the patches in pixels
406 This is where the initialization of the interface that sits behind
407 the interface lives. From here, one can go into headless or running
408 mode, but for certain things this interface will still need to act,
409 and also allows for bringing up and taking down of visual elements."
410 (setf *dimensions* dims))
415 ARGUMENTS AND VALUES:
417 RESULT: undefined, should never get here
421 RUN runs the view in an external window.
423 This should be run inside another thread as it starts the glut main-loop.
424 Closing this window will then cause the entire program to terminate."
425 ; I do this because I don't know who or what in the many layers
426 ; is causing the floating point errors, but I definitely don't
427 ; want to investigate until simply ignoring them becomes a problem.
428 (sb-int:with-float-traps-masked (:invalid :inexact :divide-by-zero :overflow :underflow)
430 (cl-glut:init-window-size *window-width* *window-height*)
431 (cl-glut:init-display-mode :double :rgba)
432 (cl-glut:create-window "CLNL Test Window")
433 (setf *glut-window-opened* t)
434 (gl:clear-color 0 0 0 1)
435 (cl-glut:display-func (cffi:get-callback 'display))
436 (glut:reshape-func (cffi:callback reshape))
437 (cl-glut:idle-func (cffi:get-callback 'idle))
438 (cl-glut:close-func (cffi:get-callback 'close-func))
439 (cl-glut:keyboard-func (cffi:get-callback 'key-pressed))
440 (cl-glut:special-func (cffi:get-callback 'special-key-pressed))
441 (gl:depth-func :lequal)
442 (gl:blend-func :src-alpha :one-minus-src-alpha)
449 5 (+ clnl-gltk:*font-height* 14)
451 (format nil *initial-banner* (asdf:component-version (asdf:find-system :clnl)))))
452 (setf *inputbox* (clnl-gltk:inputbox 5 5 10))
453 (cl-glut:main-loop)))
455 (defun patch-size () (getf *dimensions* :patch-size))
457 (defun world-width-in-pixels ()
458 (floor (* (patch-size) (1+ (- (getf *dimensions* :xmax) (getf *dimensions* :xmin))))))
460 (defun world-height-in-pixels ()
461 (floor (* (patch-size) (1+ (- (getf *dimensions* :ymax) (getf *dimensions* :ymin))))))
463 (defun export-view ()
464 "EXPORT-VIEW => IMAGE-DATA
466 ARGUMENTS AND VALUES:
468 IMAGE-DATA: A vector, pixel data as returned by opengls readPixels
472 EXPORT-VIEW returns the current view in raw data of RGBA pixels.
474 Each pixel is made up of 4 bytes of data, which an be walked over. The number
475 of pixels is the current width x height. Converting to some other image format
476 is a matter of pulling that information out and putting it into whatever format
479 This requires opengl to run, but can be used with xvfb in a headless mode."
480 (sb-int:with-float-traps-masked (:invalid)
481 (when (not *glut-window-opened*)
483 (cl-glut:init-window-size 1 1)
484 (cl-glut:create-window "CLNL Test Window")
485 (gl:clear-color 0 0 0 1)
488 (setf *glut-window-opened* t))
490 ((fbo (first (gl:gen-framebuffers 1)))
491 (render-buf (first (gl:gen-renderbuffers 1)))
492 (width (world-width-in-pixels))
493 (height (world-height-in-pixels)))
494 (gl:bind-framebuffer :framebuffer fbo)
495 (gl:bind-renderbuffer :renderbuffer render-buf)
496 (gl:renderbuffer-storage :renderbuffer :rgba8 width height)
497 (gl:framebuffer-renderbuffer :draw-framebuffer :color-attachment0 :renderbuffer render-buf)
498 (gl:viewport 0 0 width height)
500 (gl:read-pixels 0 0 width height :rgba :unsigned-byte))))