| 1 |
#!/usr/bin/env ypsilon |
| 2 |
|
| 3 |
;; glut-demo.scm: |
| 4 |
;; GLUT, OpenGL, FFI, and Concurrent GC Stress Test |
| 5 |
;; |
| 6 |
;; Requirements: |
| 7 |
;; Darwin: OpenGL.framework GLUT.framework |
| 8 |
;; Windows: opengl32.dll glut32.dll |
| 9 |
;; Linux: libGL.so.1 libglut.so.3 |
| 10 |
|
| 11 |
|
| 12 |
(import |
| 13 |
(gl) (glut) |
| 14 |
(ypsilon-compat) |
| 15 |
(rename (except (rnrs) angle display) (reverse rnrs:reverse)) |
| 16 |
(rnrs programs)) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
(define object glutSolidIcosahedron) |
| 21 |
(define reverse #t) |
| 22 |
(define angle 0.0) |
| 23 |
(define last-update 0) |
| 24 |
|
| 25 |
(define display |
| 26 |
(lambda () |
| 27 |
(glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)) |
| 28 |
(do ((y 2.0 (+ y 3.0))) |
| 29 |
((> y 14.0)) |
| 30 |
(do ((x 2.0 (+ x 3.0))) |
| 31 |
((> x 14.0)) |
| 32 |
(if reverse |
| 33 |
(render-one x y |
| 34 |
0.4 (/ x 40.0) (/ y 40.0) |
| 35 |
(/ x 20.0) (/ y 20.0) 0.4 |
| 36 |
(/ x 20.0) 0.2 (/ y 20.0) |
| 37 |
(/ (+ x y) 20.0 100.0)) |
| 38 |
(render-one x y |
| 39 |
(/ y 40.0) (/ x 40.0) 0.4 |
| 40 |
(/ x 20.0) 0.4 (/ y 20.0) |
| 41 |
0.2 (/ x 20.0) (/ y 20.0) |
| 42 |
(/ (+ x y) 20.0 100.0))))) |
| 43 |
(glutSwapBuffers))) |
| 44 |
|
| 45 |
(define rotate |
| 46 |
(lambda () |
| 47 |
(cond ((< (+ last-update 16000) (microsecond)) |
| 48 |
(if (= (glutGetWindow) 0) (exit 0)) |
| 49 |
(if reverse |
| 50 |
(let ((new-angle (+ angle 2.0))) |
| 51 |
(if (>= new-angle 360.0) |
| 52 |
(set! angle (- new-angle 360.0)) |
| 53 |
(set! angle new-angle))) |
| 54 |
(let ((new-angle (- angle 2.0))) |
| 55 |
(if (< new-angle 360.0) |
| 56 |
(set! angle (+ new-angle 360.0)) |
| 57 |
(set! angle new-angle)))) |
| 58 |
(set! last-update (microsecond)) |
| 59 |
(glutPostRedisplay))))) |
| 60 |
|
| 61 |
(define mouse |
| 62 |
(lambda (button state x y) |
| 63 |
(and (= state 0) (set! reverse (not reverse))) |
| 64 |
(format #t "mouse callback ~s ~s ~s ~s ~%" button state x y))) |
| 65 |
|
| 66 |
(define show-dodecahedron (lambda () (glScalef 0.6 0.6 0.6) (glutSolidDodecahedron))) |
| 67 |
(define show-sphere (lambda () (glutSolidSphere 1.0 32 16))) |
| 68 |
(define show-cone (lambda () (glutSolidCone 1.0 2.0 32 1))) |
| 69 |
(define show-cube (lambda () (glutSolidCube 1.5))) |
| 70 |
(define show-torus (lambda () (glutSolidTorus 0.5 1.0 16 32))) |
| 71 |
|
| 72 |
(define menu |
| 73 |
(lambda (m) |
| 74 |
(format #t "menu callback ~s ~%" m) |
| 75 |
(case m |
| 76 |
((1) (set! object glutSolidIcosahedron)) |
| 77 |
((2) (set! object glutSolidOctahedron)) |
| 78 |
((3) (set! object glutSolidTetrahedron)) |
| 79 |
((4) (set! object show-dodecahedron)) |
| 80 |
((5) (set! object show-sphere)) |
| 81 |
((6) (set! object show-cone)) |
| 82 |
((7) (set! object show-cube)) |
| 83 |
((8) (set! object show-torus)) |
| 84 |
((9) (glShadeModel GL_SMOOTH)) |
| 85 |
((10) (glShadeModel GL_FLAT)) |
| 86 |
((11) (exit))))) |
| 87 |
|
| 88 |
(define reshape |
| 89 |
(lambda (w h) |
| 90 |
(format #t "reshape callback ~s ~s ~%" w h) |
| 91 |
(and (> w 0) |
| 92 |
(> h 0) |
| 93 |
(begin |
| 94 |
(glViewport 0 0 w h) |
| 95 |
(glMatrixMode GL_PROJECTION) |
| 96 |
(glLoadIdentity) |
| 97 |
(if (<= w h) |
| 98 |
(glOrtho 0.0 16.0 0.0 (/ (* 16.0 h) w) -10.0 10.0) |
| 99 |
(glOrtho 0.0 (/ (* 16.0 w) h) 0.0 16.0 -10.0 10.0)) |
| 100 |
(glMatrixMode GL_MODELVIEW))))) |
| 101 |
|
| 102 |
(define visibility |
| 103 |
(lambda (state) |
| 104 |
(format #t "visibility callback ~s ~%" state))) |
| 105 |
|
| 106 |
(define f32vector |
| 107 |
(lambda lst |
| 108 |
(define-syntax f32set! |
| 109 |
(syntax-rules () |
| 110 |
((_ bv n value) |
| 111 |
(bytevector-ieee-single-native-set! bv (* n 4) value)))) |
| 112 |
(let ((bv (make-bytevector (* (length lst) 4)))) |
| 113 |
(let loop ((i 0) (lst lst)) |
| 114 |
(cond ((null? lst) bv) |
| 115 |
(else |
| 116 |
(f32set! bv i (car lst)) |
| 117 |
(loop (+ i 1) (cdr lst)))))))) |
| 118 |
|
| 119 |
(define render-one |
| 120 |
(lambda (x y ambr ambg ambb difr difg difb specr specg specb shine) |
| 121 |
(glPushMatrix) |
| 122 |
(glTranslatef x y 0.0) |
| 123 |
(cond ((eq? object show-sphere) |
| 124 |
(glRotatef 90.0 0.0 1.0 0.0) |
| 125 |
(glRotatef angle 0.0 0.0 1.0)) |
| 126 |
(else |
| 127 |
(glRotatef angle -0.3 1.0 -0.5))) |
| 128 |
(glMaterialfv GL_FRONT GL_AMBIENT (f32vector ambr ambg ambb 1.0)) |
| 129 |
(glMaterialfv GL_FRONT GL_DIFFUSE (f32vector difr difg difb 1.0)) |
| 130 |
(glMaterialfv GL_FRONT GL_SPECULAR (f32vector specr specg specb 1.0)) |
| 131 |
(glMaterialf GL_FRONT GL_SHININESS (* shine 128.0)) |
| 132 |
(object) |
| 133 |
(glPopMatrix))) |
| 134 |
|
| 135 |
#;(import (trace)) |
| 136 |
#;(trace render-one) |
| 137 |
#;(collect-notify #t) |
| 138 |
|
| 139 |
(define run |
| 140 |
(lambda () |
| 141 |
(glutInitDisplayMode (+ GLUT_DOUBLE GLUT_RGBA GLUT_DEPTH)) |
| 142 |
(glutInitWindowPosition 100 100) |
| 143 |
(glutInitWindowSize 500 500) |
| 144 |
(glutInit (vector (length (command-line))) (apply vector (command-line))) |
| 145 |
(glutCreateWindow "Hello GLUT") |
| 146 |
(glLightfv GL_LIGHT0 GL_AMBIENT (f32vector 0.0 0.0 0.0 1.0)) |
| 147 |
(glLightfv GL_LIGHT0 GL_DIFFUSE (f32vector 1.0 1.0 1.0 1.0)) |
| 148 |
(glLightfv GL_LIGHT0 GL_POSITION (f32vector 0.0 3.0 3.0 0.0)) |
| 149 |
(glLightModelfv GL_LIGHT_MODEL_AMBIENT (f32vector 0.2 0.2 0.2 1.0)) |
| 150 |
(glLightModelfv GL_LIGHT_MODEL_LOCAL_VIEWER (f32vector 0.0)) |
| 151 |
(glShadeModel GL_FLAT) |
| 152 |
(glFrontFace GL_CW) |
| 153 |
(glEnable GL_LIGHTING) |
| 154 |
(glEnable GL_LIGHT0) |
| 155 |
(glEnable GL_AUTO_NORMAL) |
| 156 |
(glEnable GL_NORMALIZE) |
| 157 |
(glEnable GL_DEPTH_TEST) |
| 158 |
(glDepthFunc GL_LESS) |
| 159 |
(glutDisplayFunc display) |
| 160 |
(glutReshapeFunc reshape) |
| 161 |
(glutVisibilityFunc visibility) |
| 162 |
(glutMouseFunc mouse) |
| 163 |
(glutIdleFunc rotate) |
| 164 |
(glutCreateMenu menu) |
| 165 |
(glutAddMenuEntry "Icosahedron" 1) |
| 166 |
(glutAddMenuEntry "Octahedron" 2) |
| 167 |
(glutAddMenuEntry "Tetrahedron" 3) |
| 168 |
(glutAddMenuEntry "Dodecahedron" 4) |
| 169 |
(glutAddMenuEntry "Sphere" 5) |
| 170 |
(glutAddMenuEntry "Cone" 6) |
| 171 |
(glutAddMenuEntry "Cube" 7) |
| 172 |
(glutAddMenuEntry "Torus" 8) |
| 173 |
(glutAddMenuEntry "[smooth]" 9) |
| 174 |
(glutAddMenuEntry "[flat]" 10) |
| 175 |
(glutAddMenuEntry "Exit" 11) |
| 176 |
(glutAttachMenu GLUT_RIGHT_BUTTON) |
| 177 |
(glutMainLoop))) |
| 178 |
|
| 179 |
(run) |