1 (in-package #:clnl-interface)
3 (defvar *patch-size* 13d0)
5 (defvar *turtle-list* nil)
7 ; It may be useful to keep windows around
8 (defvar *glut-window-opened* nil)
9 (defvar *dimensions* nil)
12 '((140 140 140) ; gray (5)
13 (215 48 39) ; red (15)
14 (241 105 19) ; orange (25)
15 (156 109 70) ; brown (35)
16 (237 237 47) ; yellow (45)
17 (87 176 58) ; green (55)
18 (42 209 57) ; lime (65)
19 (27 158 119) ; turquoise (75)
20 (82 196 196) ; cyan (85)
21 (43 140 190) ; sky (95)
22 (50 92 168) ; blue (105)
23 (123 78 163) ; violet (115)
24 (166 25 105) ; magenta (125)
25 (224 126 149) ; pink (135)
27 (255 255 255))) ; white
29 (defun nl-color->rgb (color)
31 ((step (+ (/ (- (mod (floor (* color 10)) 100) 50) 50.48) 0.012)))
33 (lambda (x) (/ (+ x (floor (* (if (< step 0d0) x (- 255 x)) step))) 255))
34 (nth (floor color 10) *colors*))))
36 (defun render-scene ()
37 (gl:clear :color-buffer-bit :depth-buffer-bit)
38 (gl:matrix-mode :projection)
40 (gl:ortho -71 71 -71 71 1 5000)
41 (gl:matrix-mode :modelview)
46 ((color (nl-color->rgb (getf turtle :color))))
47 (gl:color (car color) (cadr color) (caddr color)))
48 (gl:with-pushed-matrix
49 (gl:translate (* (getf turtle :xcor) *patch-size*) (* (getf turtle :ycor) *patch-size*) 0)
50 (gl:rotate (getf turtle :heading) 0 0 -1)
51 (gl:call-list *turtle-list*)))
52 (clnl-nvm:current-state))
57 (cl-glut:swap-buffers))
60 (cl-glut:post-redisplay))
63 (sb-ext:exit :abort t))
65 (defun reshape (width height)
66 (when (and (/= 0 width) (/= 0 height))
67 (gl:viewport 0 0 width height)))
69 (cffi:defcallback display :void () (display))
70 (cffi:defcallback idle :void () (idle))
71 (cffi:defcallback close-func :void () (close-func))
72 (cffi:defcallback reshape :void ((width :int) (height :int)) (reshape width height))
74 (defun set-turtle-list ()
75 (setf *turtle-list* (gl:gen-lists 1))
76 (gl:with-new-list (*turtle-list* :compile)
77 (gl:rotate 180 0 0 -1)
78 (gl:scale (* (/ 1d0 300d0) 13) (* (/ 1d0 300d0) 13) 1)
79 (gl:translate -150 -150 -4.0)
87 (defun initialize (&key dims)
88 "INITIALIZE &key DIMS => RESULT
90 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
95 XMIN: An integer representing the minimum patch coord in X
96 XMAX: An integer representing the maximum patch coord in X
97 YMIN: An integer representing the minimum patch coord in Y
98 YMAX: An integer representing the maximum patch coord in Y
102 This is where the initialization of the interface that sits behind
103 the interface lives. From here, one can go into headless or running
104 mode, but for certain things this interface will still need to act,
105 and also allows for bringing up and taking down of visual elements."
106 (setf *dimensions* dims))
111 ARGUMENTS AND VALUES:
113 RESULT: undefined, should never get here
117 RUN runs the view in an external window.
119 This should be run inside another thread as it starts the glut main-loop.
120 Closing this window will then cause the entire program to terminate."
121 ; I do this because I don't know who or what in the many layers
122 ; is causing the floating point errors, but I definitely don't
123 ; want to investigate until simply ignoring them becomes a problem.
124 (sb-int:with-float-traps-masked (:invalid)
126 (cl-glut:init-window-size
127 (floor (* *patch-size* (1+ (- (getf *dimensions* :xmax) (getf *dimensions* :xmin)))))
128 (floor (* *patch-size* (1+ (- (getf *dimensions* :ymax) (getf *dimensions* :ymin))))))
129 (cl-glut:init-display-mode :double :rgba)
130 (cl-glut:create-window "CLNL Test Window")
131 (setf *glut-window-opened* t)
132 (gl:clear-color 0 0 0 1)
133 (cl-glut:display-func (cffi:get-callback 'display))
134 (glut:reshape-func (cffi:callback reshape))
135 (cl-glut:idle-func (cffi:get-callback 'idle))
136 (cl-glut:close-func (cffi:get-callback 'close-func))
138 (cl-glut:main-loop)))
140 (defun export-view ()
141 "EXPORT-VIEW => IMAGE-DATA
143 ARGUMENTS AND VALUES:
145 IMAGE-DATA: A vector, pixel data as returned by opengls readPixels
149 EXPORT-VIEW returns the current view in raw data of RGBA pixels.
151 Each pixel is made up of 4 bytes of data, which an be walked over. The number
152 of pixels is the current width x height. Converting to some other image format
153 is a matter of pulling that information out and putting it into whatever format
156 This requires opengl to run, but can be used with xvfb in a headless mode."
157 (sb-int:with-float-traps-masked (:invalid)
158 (when (not *glut-window-opened*)
160 (cl-glut:init-window-size 1 1)
161 (cl-glut:create-window "CLNL Test Window")
162 (gl:clear-color 0 0 0 1)
164 (setf *glut-window-opened* t))
166 ((fbo (first (gl:gen-framebuffers 1)))
167 (render-buf (first (gl:gen-renderbuffers 1)))
169 ; (floor (* *patch-size* (1+ (-
170 ; (getf *dimensions* :ymax)
171 ; (getf *dimensions* :ymin))))))
173 ; (floor (* *patch-size* (1+ (- (getf *world-dims* :xmax) (getf *world-dims* :xmin))))))
174 ; (floor (* *patch-size* (1+ (-
175 ; (getf *dimensions* :xmax)
176 ; (getf *dimensions* :xmin)))))
177 (width 143) ; Hard coded for now, yay v1 (if you see this comment in a year, please cry for me)
179 (gl:bind-framebuffer :framebuffer fbo)
180 (gl:bind-renderbuffer :renderbuffer render-buf)
181 (gl:renderbuffer-storage :renderbuffer :rgba8 width height)
182 (gl:framebuffer-renderbuffer :draw-framebuffer :color-attachment0 :renderbuffer render-buf)
183 (gl:viewport 0 0 width height)
185 (gl:read-pixels 0 0 width height :rgba :unsigned-byte))))