-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertex.rkt
59 lines (43 loc) · 1.81 KB
/
vertex.rkt
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
#lang racket/base
(require ffi/vector
opengl
racket/match)
(provide (all-defined-out))
;;; ----------------------------------------------------------------------------
(struct vertex-buffer (vbo))
(define (make-vbo target data usage)
(define vbo (u32vector-ref (glGenBuffers 1) 0))
(glBindBuffer target vbo)
(glBufferData target (gl-vector-sizeof data) data usage)
vbo)
(define (bind-vbo vbo)
(glBindBuffer GL_ARRAY_BUFFER vbo))
;;; ----------------------------------------------------------------------------
(struct vertex-attribute (vbo index size type)
#:name gfx:vertex-attribute
#:constructor-name make-vertex-attribute)
(define (vertex-attribute index size data
#:type [type GL_FLOAT]
#:target [target GL_ARRAY_BUFFER]
#:usage [usage GL_STATIC_DRAW])
(make-vertex-attribute (make-vbo target data usage) index size type))
(define (enable-vertex-attribute attr)
(match-define (gfx:vertex-attribute vbo index size type) attr)
(glEnableVertexAttribArray index)
(bind-vbo vbo)
(glVertexAttribPointer index size type #f 0 0))
(define (disable-vertex-attribute attr)
(glDisableVertexAttribArray (vertex-attribute-index attr)))
(define-syntax-rule (with-vertex-attribute attr-expr body ...)
(let ([attr attr-expr])
(enable-vertex-attribute attr) body ... (disable-vertex-attribute attr)))
(define-syntax-rule (with-vertex-attributes attrs-expr body ...)
(let ([attrs attrs-expr])
(for-each enable-vertex-attribute attrs)
body ...
(for-each disable-vertex-attribute attrs)))
;; ;;; ----------------------------------------------------------------------------
;; (struct uniform (type name)
;; #:name gfx:uniform
;; #:constructor-name make-uniform)
;; (define (uniform name type . ))