blob: 89e700c93345787e04120ea441df35d02ecc1ea0 (
plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
;;; Sly
;;; Copyright (C) 2014 David Thompson <davet@gnu.org>
;;;
;;; Sly is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Sly is distributed in the hope that it will be useful, but WITHOUT
;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;;; License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; A mesh is a 2D/3D model comprised of a shader and vertex buffers.
;;
;;; Code:
(define-module (sly mesh)
#:use-module (ice-9 match)
#:use-module (rnrs bytevectors)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-43)
#:use-module (system foreign)
#:use-module (gl)
#:use-module (gl low-level)
#:use-module (sly wrappers gl)
#:use-module (sly color)
#:use-module (sly shader)
#:use-module (sly texture)
#:use-module (sly math vector)
#:use-module (sly signal)
#:export (make-mesh
mesh?
mesh-length
mesh-shader
mesh-texture
draw-mesh))
;;;
;;; Vertex Buffers and Vertex Arrays
;;;
(define-record-type <vertex-buffer>
(%make-vertex-buffer id type attr-size length)
vertex-buffer?
(id vertex-buffer-id)
(type vertex-buffer-type)
(attr-size vertex-buffer-attr-size)
(length vertex-buffer-length))
(define (generate-vertex-buffer)
(let ((bv (u32vector 1)))
(glGenBuffers 1 (bytevector->pointer bv))
(u32vector-ref bv 0)))
(define (bind-vertex-buffer vbo)
(glBindBuffer (vertex-buffer-type vbo)
(vertex-buffer-id vbo)))
(define-syntax-rule (with-vertex-buffer vbo body ...)
(let ((type (vertex-buffer-type vbo)))
(glBindBuffer type (vertex-buffer-id vbo))
body ...
(glBindBuffer type 0)))
(define (vertices-bytevector vertices index?)
(let* ((elem (vector-ref vertices 0))
(bv (if index?
(make-u32vector (vector-length vertices))
(make-f32vector (* (vector-length vertices)
(attribute-size elem)))))
(setter (if index? u32vector-set! f32vector-set!)))
(vector-for-each
(match-lambda*
((i (? number? k))
(setter bv i k))
((i ($ <vector2> x y))
(let ((offset (* i 2)))
(setter bv offset x)
(setter bv (1+ offset) y)))
((i ($ <vector3> x y z))
(let ((offset (* i 3)))
(setter bv offset x)
(setter bv (1+ offset) y)
(setter bv (+ offset 2) z)))
((i ($ <vector4> x y z w))
(let ((offset (* i 4)))
(setter bv offset x)
(setter bv (1+ offset) y)
(setter bv (+ offset 2) z)
(setter bv (+ offset 3) w)))
((i (color? c))
(let ((offset (* i 4)))
(setter bv offset (color-r c))
(setter bv (1+ offset) (color-g c))
(setter bv (+ offset 2) (color-b c))
(setter bv (+ offset 3) (color-a c)))))
vertices)
bv))
(define (attribute-size attr)
(cond
((number? attr) 1)
((vector2? attr) 2)
((vector3? attr) 3)
((or (vector4? attr)
(color? attr))
4)
(else
(error "Unsupported attribute: " attr))))
(define (gl-buffer-type index?)
(if index?
(arb-vertex-buffer-object element-array-buffer-arb)
(arb-vertex-buffer-object array-buffer-arb)))
(define* (make-vertex-buffer vertices #:optional (index? #f))
(let ((bv (vertices-bytevector vertices index?))
(vbo (%make-vertex-buffer (generate-vertex-buffer)
(gl-buffer-type index?)
(attribute-size (vector-ref vertices 0))
(vector-length vertices))))
(with-vertex-buffer vbo
(glBufferData (vertex-buffer-type vbo)
(bytevector-length bv)
(bytevector->pointer bv)
(arb-vertex-buffer-object static-draw-arb)))
vbo))
(define (generate-vertex-array)
(let ((bv (u32vector 1)))
(glGenVertexArrays 1 (bytevector->pointer bv))
(u32vector-ref bv 0)))
(define-syntax-rule (with-vertex-array vao body ...)
(begin
(glBindVertexArray vao)
body ...
(glBindVertexArray 0)))
(define (vertex-attrib-pointer shader attribute vbo)
(let ((location (shader-program-attribute-location shader attribute)))
(glEnableVertexAttribArray location)
(with-vertex-buffer vbo
(glVertexAttribPointer location (vertex-buffer-attr-size vbo)
(data-type float) #f 0 %null-pointer))))
;;;
;;; Mesh
;;;
(define-record-type <mesh>
(%make-mesh vao length shader)
mesh?
(vao mesh-vao)
(length mesh-length)
(shader mesh-shader))
(define* (make-mesh #:optional #:key shader indices data)
(let ((vao (generate-vertex-array)))
(with-vertex-array vao
(let loop ((data data))
(match data
(((attribute vertices) . rest)
(vertex-attrib-pointer shader attribute
(make-vertex-buffer vertices))
(loop rest))
(() #f)))
(bind-vertex-buffer (make-vertex-buffer indices #t)))
(%make-mesh vao (vector-length indices) shader)))
(define (draw-mesh mesh uniforms)
(define (draw)
(glDrawElements (begin-mode triangles)
(mesh-length mesh)
(data-type unsigned-int)
%null-pointer))
(with-shader-program (mesh-shader mesh)
(for-each (lambda (uniform)
(match uniform
((name value)
(uniform-set! (mesh-shader mesh) name
(signal-ref-maybe value)))))
uniforms)
(with-vertex-array (mesh-vao mesh)
(draw))))
|