Prims - Implement die
[clnl] / src / main / interface.lisp
1 (in-package #:clnl-interface)
2
3 (defvar *patch-size* 13d0)
4
5 (defvar *turtle-list* nil)
6
7 ; It may be useful to keep windows around
8 (defvar *glut-window-opened* nil)
9 (defvar *dimensions* nil)
10
11 (defvar *colors*
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)
26    (0 0 0) ; black
27    (255 255 255))) ; white
28
29 (defun nl-color->rgb (color)
30  (let*
31   ((step (+ (/ (- (mod (floor (* color 10)) 100) 50) 50.48) 0.012)))
32   (mapcar
33    (lambda (x) (/ (+ x (floor (* (if (< step 0d0) x (- 255 x)) step))) 255))
34    (nth (floor color 10) *colors*))))
35
36 (defun render-scene ()
37  (gl:clear :color-buffer-bit :depth-buffer-bit)
38  (gl:matrix-mode :projection)
39  (gl:load-identity)
40  (gl:ortho -71 71 -71 71 1 5000)
41  (gl:matrix-mode :modelview)
42  (gl:load-identity)
43  (mapcar
44   (lambda (turtle)
45    (let
46     ((color (nl-color->rgb (getf turtle :color))))
47     (gl:color (car color) (cadr color) (caddr color)))
48    (mapcar
49     (lambda (x-modification y-modification)
50      (gl:with-pushed-matrix
51       (gl:translate (* (getf turtle :xcor) *patch-size*) (* (getf turtle :ycor) *patch-size*) 0)
52       (gl:translate x-modification y-modification 0)
53       (gl:rotate (getf turtle :heading) 0 0 -1)
54       (gl:call-list *turtle-list*)))
55     (list 0 (1- (world-width-in-pixels)) (- (1- (world-width-in-pixels))) 0 0)
56     (list 0 0 0 (1- (world-height-in-pixels)) (- (1- (world-height-in-pixels))))))
57   (clnl-nvm:current-state))
58  (gl:flush))
59
60 (defun display ()
61  (render-scene)
62  (cl-glut:swap-buffers))
63
64 (defun idle ()
65  (cl-glut:post-redisplay))
66
67 (defun close-func ()
68  (sb-ext:exit :abort t))
69
70 (defun reshape (width height)
71  (when (and (/= 0 width) (/= 0 height))
72   (gl:viewport 0 0 width height)))
73
74 (cffi:defcallback display :void () (display))
75 (cffi:defcallback idle :void () (idle))
76 (cffi:defcallback close-func :void () (close-func))
77 (cffi:defcallback reshape :void ((width :int) (height :int)) (reshape width height))
78
79 (defun set-turtle-list ()
80  (setf *turtle-list* (gl:gen-lists 1))
81  (gl:with-new-list (*turtle-list* :compile)
82   (gl:rotate 180 0 0 -1)
83   (gl:scale (* (/ 1d0 300d0) 13) (* (/ 1d0 300d0) 13) 1)
84   (gl:translate -150 -150 -4.0)
85   (gl:begin :polygon)
86   (gl:vertex 150 5 0)
87   (gl:vertex 40 250 0)
88   (gl:vertex 150 205 0)
89   (gl:vertex 260 250 0)
90   (gl:end)))
91
92 (defun initialize (&key dims)
93  "INITIALIZE &key DIMS => RESULT
94
95   DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
96
97 ARGUMENTS AND VALUES:
98
99   RESULT: undefined
100   XMIN: An integer representing the minimum patch coord in X
101   XMAX: An integer representing the maximum patch coord in X
102   YMIN: An integer representing the minimum patch coord in Y
103   YMAX: An integer representing the maximum patch coord in Y
104
105 DESCRIPTION:
106
107   This is where the initialization of the interface that sits behind
108   the interface lives.  From here, one can go into headless or running
109   mode, but for certain things this interface will still need to act,
110   and also allows for bringing up and taking down of visual elements."
111  (setf *dimensions* dims))
112
113 (defun run ()
114  "RUN => RESULT
115
116 ARGUMENTS AND VALUES:
117
118   RESULT: undefined, should never get here
119
120 DESCRIPTION:
121
122   RUN runs the view in an external window.
123
124   This should be run inside another thread as it starts the glut main-loop.
125   Closing this window will then cause the entire program to terminate."
126  ; I do this because I don't know who or what in the many layers
127  ; is causing the floating point errors, but I definitely don't
128  ; want to investigate until simply ignoring them becomes a problem.
129  (sb-int:with-float-traps-masked (:invalid)
130   (cl-glut:init)
131   (cl-glut:init-window-size
132    (world-width-in-pixels)
133    (world-height-in-pixels))
134   (cl-glut:init-display-mode :double :rgba)
135   (cl-glut:create-window "CLNL Test Window")
136   (setf *glut-window-opened* t)
137   (gl:clear-color 0 0 0 1)
138   (cl-glut:display-func (cffi:get-callback 'display))
139   (glut:reshape-func (cffi:callback reshape))
140   (cl-glut:idle-func (cffi:get-callback 'idle))
141   (cl-glut:close-func (cffi:get-callback 'close-func))
142   (set-turtle-list)
143   (cl-glut:main-loop)))
144
145 (defun world-width-in-pixels ()
146  (floor (* *patch-size* (1+ (- (getf *dimensions* :xmax) (getf *dimensions* :xmin))))))
147
148 (defun world-height-in-pixels ()
149  (floor (* *patch-size* (1+ (- (getf *dimensions* :ymax) (getf *dimensions* :ymin))))))
150
151 (defun export-view ()
152  "EXPORT-VIEW => IMAGE-DATA
153
154 ARGUMENTS AND VALUES:
155
156   IMAGE-DATA: A vector, pixel data as returned by opengls readPixels
157
158 DESCRIPTION:
159
160   EXPORT-VIEW returns the current view in raw data of RGBA pixels.
161
162   Each pixel is made up of 4 bytes of data, which an be walked over.  The number
163   of pixels is the current width x height.  Converting to some other image format
164   is a matter of pulling that information out and putting it into whatever format
165   you like.
166
167   This requires opengl to run, but can be used with xvfb in a headless mode."
168  (sb-int:with-float-traps-masked (:invalid)
169   (when (not *glut-window-opened*)
170    (cl-glut:init)
171    (cl-glut:init-window-size 1 1)
172    (cl-glut:create-window "CLNL Test Window")
173    (gl:clear-color 0 0 0 1)
174    (set-turtle-list)
175    (setf *glut-window-opened* t))
176   (let
177    ((fbo (first (gl:gen-framebuffers 1)))
178     (render-buf (first (gl:gen-renderbuffers 1)))
179    ;(width
180    ; (floor (* *patch-size* (1+ (-
181    ;                             (getf *dimensions* :ymax)
182    ;                             (getf *dimensions* :ymin))))))
183    ;(height
184    ; (floor (* *patch-size* (1+ (- (getf *world-dims* :xmax) (getf *world-dims* :xmin))))))
185    ; (floor (* *patch-size* (1+ (-
186    ;                            (getf *dimensions* :xmax)
187    ;                            (getf *dimensions* :xmin)))))
188     (width (world-width-in-pixels))  ; Hard coded for now, yay v1 (if you see this comment in a year, please cry for me)
189     (height (world-height-in-pixels)))
190    (gl:bind-framebuffer :framebuffer fbo)
191    (gl:bind-renderbuffer :renderbuffer render-buf)
192    (gl:renderbuffer-storage :renderbuffer :rgba8 width height)
193    (gl:framebuffer-renderbuffer :draw-framebuffer :color-attachment0 :renderbuffer render-buf)
194    (gl:viewport 0 0 width height)
195    (render-scene)
196    (gl:read-pixels 0 0 width height :rgba :unsigned-byte))))