4 (use-modules (figl glut)
9 (figl contrib packed-struct))
11 (define-packed-struct color-quad
44 (define-packed-struct particle
52 (define *quads* (make-packed-array color-quad 0))
53 (define *particles* (make-packed-array particle 0))
55 (define (draw-particles)
56 (gl-enable-client-state (enable-cap vertex-array))
57 (gl-enable-client-state (enable-cap color-array))
58 (set-gl-vertex-array (vertex-pointer-type float)
60 #:stride (/ (packed-struct-size color-quad) 4))
61 (set-gl-color-array (color-pointer-type float)
63 #:stride (/ (packed-struct-size color-quad) 4)
64 #:offset (/ (packed-struct-size color-quad) 8))
65 (gl-draw-arrays (begin-mode quads) 0
66 (* 4 (packed-array-length *quads* color-quad)))
67 (gl-disable-client-state (enable-cap color-array))
68 (gl-disable-client-state (enable-cap vertex-array)))
70 (define (update-quads)
74 (lambda (n x y z vx vy vz)
75 (let ((r (/ (abs vx) 5))
82 (pack *quads* n color-quad
92 (define (update-particles dt)
93 (let ((half-dt-squared (* 0.5 dt dt)))
97 (lambda (n x y z vx vy vz)
98 (let* ((distance-squared (+ (* x x) (* y y) (* z z)))
99 (distance (sqrt distance-squared))
100 (F (/ -200 distance-squared))
101 (ax (* F (/ x distance)))
102 (ay (* F (/ y distance)))
103 (az (* F (/ z distance))))
104 (values (+ x (* vx dt) (* ax half-dt-squared))
105 (+ y (* vy dt) (* ay half-dt-squared))
106 (+ z (* vz dt) (* az half-dt-squared))
109 (+ vz (* az dt))))))))
111 (define (prepare-particles n)
112 (set! *particles* (make-packed-array particle n))
113 (set! *quads* (make-packed-array color-quad n))
120 (* (random:normal) 30)
121 (* (random:normal) 30)
122 (* (random:normal) 30)
125 (* (random:normal) 2)
126 (* (random:normal) 2)
127 (* (random:normal) 2))))
130 (define main-window #f)
132 (define (make-fps-accumulator period)
133 (let* ((frame-count 0)
134 (last-fps-time (get-internal-real-time))
135 (last-fps-run-time (get-internal-run-time))
136 (last-frame-time (get-internal-real-time))
137 (max-frame-time (get-internal-real-time))
139 (jiffies-per-sec (exact->inexact internal-time-units-per-second))
140 (jiffies-per-ms (/ jiffies-per-sec 1000)))
142 (let ((now (get-internal-real-time)))
143 (set! frame-count (1+ frame-count))
144 (when (> (- now last-frame-time) max-frame-time)
145 (set! max-frame-time (- now last-frame-time)))
146 (set! last-frame-time now)
147 (when (> (- now last-fps-time) period)
148 (let* ((run (get-internal-run-time))
149 (frames (- frame-count last-frame-count))
150 (secs (/ (- now last-fps-time) jiffies-per-sec))
151 (fps (/ frames secs))
152 (ms-per-frame (/ (* secs 1000) frames))
153 (max-ms-per-frame (/ max-frame-time jiffies-per-ms))
154 (cpu (* 100.0 (/ (- run last-fps-run-time)
155 (- now last-fps-time)))))
158 ";;; ~a frames in ~,2f sec = ~,2f fps; ~,2f ms/frame, ~,2f ms max; ~,2f% CPU\n"
159 frames secs fps ms-per-frame max-ms-per-frame cpu)
160 (set! last-frame-count frame-count)
161 (set! max-frame-time 0)
162 (set! last-fps-time now)
163 (set! last-fps-run-time run)))))))
165 (define accumulate-fps!
166 (make-fps-accumulator (* 2 internal-time-units-per-second)))
168 (define (draw-axis scale)
169 ;; Could disable lighting and depth test.
170 (gl-begin (begin-mode lines)
173 (gl-vertex scale 0 0)
177 (gl-vertex 0 scale 0)
181 (gl-vertex 0 0 scale)))
185 (gl-clear (clear-buffer-mask color-buffer depth-buffer))
191 ;; With double-buffering, swap-buffers will wait for the frame to be shown,
192 ;; which limits this program to the frame rate.
195 (define (on-reshape width height)
196 (pk 'reshape! width height)
197 (gl-viewport 0 0 width height)
199 ;; Projection matrix.
200 (set-gl-matrix-mode (matrix-mode projection))
202 (glu-perspective 60 (/ width height) 0.1 1000))
204 (define full-screen? #f)
207 (define (on-keyboard keycode x y)
208 (let ((c (integer->char keycode)))
211 (set! full-screen? (not full-screen?))
212 (full-screen main-window full-screen?))
214 (format #t "~s pressed; quitting.\n" c)
217 ;; The rotations are a hack to re-orient so that a translation in the Z
218 ;; axis will move us towards the origin.
219 (gl-rotate -10 1 0 0)
220 (gl-translate 0 0 10)
221 (gl-rotate 10 1 0 0))
223 (gl-rotate -10 1 0 0)
224 (gl-translate 0 0 -10)
225 (gl-rotate 10 1 0 0))
227 (set! running? (not running?))
228 (set-idle-callback (and running? (lambda () (on-idle)))))
230 (pk 'keyboard c x y)))))
232 (define (on-special keycode x y)
233 (pk 'special keycode x y))
235 (define (on-mouse button state x y)
236 (pk 'mouse button state x y))
238 (define (on-motion x y)
241 (define (on-visibility visible?)
242 (pk 'visible visible?))
245 (set-gl-matrix-mode (matrix-mode modelview))
246 ;; Rotate the camera 0.05 degree per frame about the Z axis. At 60 fps that's
247 ;; 120 seconds per full rotation. Rotating about the Z axis keeps all the
248 ;; quads facing our way.
249 (gl-rotate 0.05 0 0 1)
250 (update-particles 0.016)
252 (post-redisplay main-window))
254 (define (register-glut-callbacks)
255 ;; The trampolines allow the handlers to be overridden at runtime by
256 ;; an attached Guile REPL client.
257 (set-display-callback (lambda () (on-display)))
258 (set-reshape-callback (lambda (w h) (on-reshape w h)))
259 (set-keyboard-callback (lambda (k x y) (on-keyboard k x y)))
260 (set-special-callback (lambda (k x y) (on-special k x y)))
261 (set-mouse-callback (lambda (b s x y) (on-mouse b s x y)))
262 (set-motion-callback (lambda (x y) (on-motion x y)))
263 (set-visibility-callback (lambda (visible?) (on-visibility visible?)))
264 (set-idle-callback (lambda () (on-idle))))
268 (initialize-glut args
269 #:window-size '(640 . 480)
270 #:display-mode (display-mode rgba alpha double
272 (set! main-window (make-window "particle-system"))
273 (register-glut-callbacks)
274 (set-gl-clear-color 0 0 0 1)
275 (set-gl-clear-depth 1)
277 (set-gl-shade-model (shading-model smooth))
279 ;; Resetting the modelview matrix mode leaves the camera at the origin,
280 ;; oriented to look down the Z axis. Rotate up a bit so that we can see the
282 (set-gl-matrix-mode (matrix-mode modelview))
284 (gl-translate 0 0 -100)
287 (gl-enable (enable-cap depth-test))
289 (set! *random-state* (random-state-from-platform))
290 (prepare-particles (match args
292 ((_ n) (string->number n))))
297 (exit (main (program-arguments))))