4 (use-modules (figl glut)
9 (figl contrib packed-struct))
11 (define-packed-struct zquad
23 (define-packed-struct position
31 (define *zquads* (make-packed-array zquad 0))
32 (define *particles* (make-packed-array position 0))
34 (define (draw-particles)
35 (gl-begin (begin-mode quads)
36 (unpack-each/serial *zquads* zquad
37 (lambda (n r g b x- y- x+ y+ z)
42 (gl-vertex x- y+ z)))))
44 (define (update-quads)
48 (lambda (n x y z vx vy vz)
49 (let ((r (/ (abs vx) 5))
56 (pack *zquads* n zquad
57 r g b x- y- x+ y+ z)))))
59 (define (update-particles dt)
60 (let ((half-dt-squared (* 0.5 dt dt)))
64 (lambda (n x y z vx vy vz)
65 (let* ((distance-squared (+ (* x x) (* y y) (* z z)))
66 (distance (sqrt distance-squared))
67 (F (/ -200 distance-squared))
68 (ax (* F (/ x distance)))
69 (ay (* F (/ y distance)))
70 (az (* F (/ z distance))))
71 (values (+ x (* vx dt) (* ax half-dt-squared))
72 (+ y (* vy dt) (* ay half-dt-squared))
73 (+ z (* vz dt) (* az half-dt-squared))
76 (+ vz (* az dt))))))))
78 (define (prepare-particles n)
79 (set! *particles* (make-packed-array position n))
80 (set! *zquads* (make-packed-array zquad n))
87 (* (random:normal) 30)
88 (* (random:normal) 30)
89 (* (random:normal) 30)
94 (* (random:normal) 2))))
97 (define main-window #f)
99 (define (make-fps-accumulator period)
100 (let* ((frame-count 0)
101 (last-fps-time (get-internal-real-time))
102 (last-fps-run-time (get-internal-run-time))
103 (last-frame-time (get-internal-real-time))
104 (max-frame-time (get-internal-real-time))
106 (jiffies-per-sec (exact->inexact internal-time-units-per-second))
107 (jiffies-per-ms (/ jiffies-per-sec 1000)))
109 (let ((now (get-internal-real-time)))
110 (set! frame-count (1+ frame-count))
111 (when (> (- now last-frame-time) max-frame-time)
112 (set! max-frame-time (- now last-frame-time)))
113 (set! last-frame-time now)
114 (when (> (- now last-fps-time) period)
115 (let* ((run (get-internal-run-time))
116 (frames (- frame-count last-frame-count))
117 (secs (/ (- now last-fps-time) jiffies-per-sec))
118 (fps (/ frames secs))
119 (ms-per-frame (/ (* secs 1000) frames))
120 (max-ms-per-frame (/ max-frame-time jiffies-per-ms))
121 (cpu (* 100.0 (/ (- run last-fps-run-time)
122 (- now last-fps-time)))))
125 ";;; ~a frames in ~,2f sec = ~,2f fps; ~,2f ms/frame, ~,2f ms max; ~,2f% CPU\n"
126 frames secs fps ms-per-frame max-ms-per-frame cpu)
127 (set! last-frame-count frame-count)
128 (set! max-frame-time 0)
129 (set! last-fps-time now)
130 (set! last-fps-run-time run)))))))
132 (define accumulate-fps!
133 (make-fps-accumulator (* 2 internal-time-units-per-second)))
135 (define (draw-axis scale)
136 ;; Could disable lighting and depth test.
137 (gl-begin (begin-mode lines)
140 (gl-vertex scale 0 0)
144 (gl-vertex 0 scale 0)
148 (gl-vertex 0 0 scale)))
152 (gl-clear (clear-buffer-mask color-buffer depth-buffer))
158 ;; With double-buffering, swap-buffers will wait for the frame to be shown,
159 ;; which limits this program to the frame rate.
162 (define (on-reshape width height)
163 (pk 'reshape! width height)
164 (gl-viewport 0 0 width height)
166 ;; Projection matrix.
167 (set-gl-matrix-mode (matrix-mode projection))
169 (glu-perspective 60 (/ width height) 0.1 1000))
171 (define full-screen? #f)
174 (define (on-keyboard keycode x y)
175 (let ((c (integer->char keycode)))
178 (set! full-screen? (not full-screen?))
179 (full-screen main-window full-screen?))
181 (format #t "~s pressed; quitting.\n" c)
184 ;; The rotations are a hack to re-orient so that a translation in the Z
185 ;; axis will move us towards the origin.
186 (gl-rotate -10 1 0 0)
187 (gl-translate 0 0 10)
188 (gl-rotate 10 1 0 0))
190 (gl-rotate -10 1 0 0)
191 (gl-translate 0 0 -10)
192 (gl-rotate 10 1 0 0))
194 (set! running? (not running?))
195 (set-idle-callback (and running? (lambda () (on-idle)))))
197 (pk 'keyboard c x y)))))
199 (define (on-special keycode x y)
200 (pk 'special keycode x y))
202 (define (on-mouse button state x y)
203 (pk 'mouse button state x y))
205 (define (on-motion x y)
208 (define (on-visibility visible?)
209 (pk 'visible visible?))
212 (set-gl-matrix-mode (matrix-mode modelview))
213 ;; Rotate the camera 0.05 degree per frame about the Z axis. At 60 fps that's
214 ;; 120 seconds per full rotation. Rotating about the Z axis keeps all the
215 ;; quads facing our way.
216 (gl-rotate 0.05 0 0 1)
217 (update-particles 0.016)
219 (post-redisplay main-window))
221 (define (register-glut-callbacks)
222 ;; The trampolines allow the handlers to be overridden at runtime by
223 ;; an attached Guile REPL client.
224 (set-display-callback (lambda () (on-display)))
225 (set-reshape-callback (lambda (w h) (on-reshape w h)))
226 (set-keyboard-callback (lambda (k x y) (on-keyboard k x y)))
227 (set-special-callback (lambda (k x y) (on-special k x y)))
228 (set-mouse-callback (lambda (b s x y) (on-mouse b s x y)))
229 (set-motion-callback (lambda (x y) (on-motion x y)))
230 (set-visibility-callback (lambda (visible?) (on-visibility visible?)))
231 (set-idle-callback (lambda () (on-idle))))
235 (initialize-glut args
236 #:window-size '(640 . 480)
237 #:display-mode (display-mode rgba alpha double
239 (set! main-window (make-window "particle-system"))
240 (register-glut-callbacks)
241 (set-gl-clear-color 0 0 0 1)
242 (set-gl-clear-depth 1)
243 (set-gl-shade-model (shading-model smooth))
245 ;; Resetting the modelview matrix mode leaves the camera at the origin,
246 ;; oriented to look down the Z axis. Rotate up a bit so that we can see the
248 (set-gl-matrix-mode (matrix-mode modelview))
250 (gl-translate 0 0 -100)
253 (gl-enable (enable-cap depth-test))
255 (set! *random-state* (random-state-from-platform))
256 (prepare-particles (match args
258 ((_ n) (string->number n))))
263 (exit (main (program-arguments))))