-
Notifications
You must be signed in to change notification settings - Fork 1
/
interactive.scm
76 lines (65 loc) · 2.85 KB
/
interactive.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;;;; interactive.scm
;;;; An example of an interactive glls environment
;;;; Run with csi
(import chicken scheme)
(use glls-render (prefix glfw3 glfw:) (prefix opengl-glew gl:) gl-math gl-utils
srfi-18)
;; Mesh
(define rect (make-mesh vertices: '(attributes: ((position #:float 2)
(color #:unsigned-byte 3
normalized: #t))
initial-elements: ((position . (-1 -1
1 -1
1 1
-1 1))
(color . (1 0 0
0 1 0
0 0 1
1 0 1))))
indices: '(type: #:ushort
initial-elements: (0 1 2
0 2 3))))
;;; Matrices
(define projection-matrix
(perspective 640 480 0.1 100 70))
(define view-matrix
(look-at (make-point 1 0 3)
(make-point 0 0 0)
(make-point 0 1 0)))
(define model-matrix (mat4-identity))
(define mvp (m* projection-matrix
(m* view-matrix model-matrix)
#t ; Matrix should be in a non-GC'd area
))
;;; Pipeline definition
;; Change me, then re-evaluate this form!
;; If you change attributes and uniforms, though, you'll need to create a new renderable.
(define-pipeline simple-shader
((#:vertex input: ((position #:vec2) (color #:vec3))
uniform: ((mvp #:mat4))
output: ((c #:vec3)))
(define (main) #:void
(set! gl:position (* mvp (vec4 position 0.0 1.0)))
(set! c color)))
((#:fragment input: ((c #:vec3))
output: ((frag-color #:vec4)))
(define (main) #:void
(set! frag-color (vec4 c 1.0)))))
;;; Initialization and main loop
;;; Run in a thread so that you can still use the REPL
(thread-start!
(lambda ()
(glfw:with-window (640 480 "Example" resizable: #f)
(gl:init)
(compile-pipelines)
(mesh-make-vao! rect (pipeline-mesh-attributes simple-shader) #:dynamic)
(let ((renderable (make-simple-shader-renderable mesh: rect
mvp: mvp)))
(let loop ()
(glfw:swap-buffers (glfw:window))
(gl:clear (bitwise-ior gl:+color-buffer-bit+ gl:+depth-buffer-bit+))
(render-simple-shader renderable)
(thread-yield!) ; Let the main thread eval stuff
(glfw:poll-events)
(unless (glfw:window-should-close? (glfw:window))
(loop)))))))