summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.am1
-rw-r--r--sly/render/context.scm18
-rw-r--r--sly/render/font.scm4
-rw-r--r--sly/render/mesh.scm158
-rw-r--r--sly/render/model.scm8
-rw-r--r--sly/render/shape.scm165
-rw-r--r--sly/render/sprite.scm39
-rw-r--r--sly/render/vertex-array.scm173
8 files changed, 258 insertions, 308 deletions
diff --git a/Makefile.am b/Makefile.am
index c43415e..68805f6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -55,7 +55,6 @@ SOURCES = \
sly/render/shape.scm \
sly/render/sprite.scm \
sly/render/tileset.scm \
- sly/render/vertex-array.scm \
sly/render/context.scm \
$(WRAPPER_SOURCES)
diff --git a/sly/render/context.scm b/sly/render/context.scm
index d3bd0c3..fb28eda 100644
--- a/sly/render/context.scm
+++ b/sly/render/context.scm
@@ -33,7 +33,7 @@
#:use-module (sly render shader)
#:use-module (sly render texture)
#:use-module (sly render utils)
- #:use-module (sly render vertex-array)
+ #:use-module (sly render mesh)
#:export (make-render-context
render-context?
with-render-context with-temp-transform
@@ -41,17 +41,17 @@
render-context-depth-test? set-render-context-depth-test?!
render-context-texture set-render-context-texture!
render-context-shader set-render-context-shader!
- render-context-vertex-array set-render-context-vertex-array!))
+ render-context-mesh set-render-context-mesh!))
(define-record-type <render-context>
(%make-render-context blend-mode depth-test? texture shader
- vertex-array transform-stack)
+ mesh transform-stack)
render-context?
(blend-mode render-context-blend-mode %set-render-context-blend-mode!)
(depth-test? render-context-depth-test? %set-render-context-depth-test?!)
(texture render-context-texture %set-render-context-texture!)
(shader render-context-shader %set-render-context-shader!)
- (vertex-array render-context-vertex-array %set-render-context-vertex-array!)
+ (mesh render-context-mesh %set-render-context-mesh!)
(transform-stack render-context-transform-stack))
(define (make-null-transform)
@@ -79,7 +79,7 @@
(glUseProgram 0)
(%set-render-context-shader! context #f)
(glBindVertexArray 0)
- (%set-render-context-vertex-array! context #f))
+ (%set-render-context-mesh! context #f))
(define-syntax-rule (with-render-context context body ...)
(begin (render-context-reset! context) body ...))
@@ -118,10 +118,10 @@
(apply-shader-program shader)
(%set-render-context-shader! context shader)))
-(define (set-render-context-vertex-array! context vertex-array)
- (unless (equal? (render-context-vertex-array context) vertex-array)
- (apply-vertex-array vertex-array)
- (%set-render-context-vertex-array! context vertex-array)))
+(define (set-render-context-mesh! context mesh)
+ (unless (equal? (render-context-mesh context) mesh)
+ (apply-mesh mesh)
+ (%set-render-context-mesh! context mesh)))
;; emacs: (put 'with-temp-transform 'scheme-indent-function 2)
(define-syntax-rule (with-temp-transform context name body ...)
diff --git a/sly/render/font.scm b/sly/render/font.scm
index 6be7353..e50c96a 100644
--- a/sly/render/font.scm
+++ b/sly/render/font.scm
@@ -116,7 +116,7 @@ HEIGHT, 32 bit color bytevector."
0 0 1 1)))
(define* (make-label font text #:optional #:key
- (anchor 'top-left) (color white)
+ (anchor 'top-left)
(shader (load-default-shader)))
(let ((texture (render-text font text)))
- (make-sprite texture #:shader shader #:anchor anchor #:color color)))
+ (make-sprite texture #:shader shader #:anchor anchor)))
diff --git a/sly/render/mesh.scm b/sly/render/mesh.scm
index 992ef85..98510d3 100644
--- a/sly/render/mesh.scm
+++ b/sly/render/mesh.scm
@@ -17,29 +17,157 @@
;;; Commentary:
;;
-;; A mesh is a 2D/3D model comprised of a shader and vertex buffers.
+;; Meshes encapsulate the geometry for a single OpenGL draw call.
;;
;;; Code:
(define-module (sly render mesh)
- #:use-module (oop goops)
+ #:use-module (system foreign)
#:use-module (ice-9 match)
#:use-module (srfi srfi-9)
- #:use-module (system foreign)
+ #:use-module (srfi srfi-43)
+ #:use-module (rnrs bytevectors)
#:use-module (gl)
#:use-module (gl low-level)
#:use-module (sly wrappers gl)
+ #:use-module (sly math vector)
#:use-module (sly render color)
#:use-module (sly render shader)
- #:use-module (sly render texture)
- #:use-module (sly math vector)
- #:use-module (sly signal)
- #:use-module (sly math transform)
- #:use-module (sly render utils)
- #:use-module (sly render vertex-array)
- #:use-module (sly render model)
- #:export (make-mesh))
-
-(define* (make-mesh #:optional #:key shader texture indices positions textures)
- (make-model #:shader shader #:texture texture
- #:mesh (make-vertex-array indices positions textures)))
+ #:export (make-mesh
+ mesh?
+ mesh-id mesh-length
+ apply-mesh with-mesh))
+
+;;;
+;;; Vertex Buffer
+;;;
+
+(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
+ (match-lambda
+ ((? number? _) 1)
+ ((? vector2? _) 2)
+ ((? vector3? _) 3)
+ ((or (? vector4? _)
+ (? color? _))
+ 4)
+ (attr
+ (error "Unsupported vertex buffer 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))
+
+;;;
+;;; Mesh
+;;;
+
+(define-record-type <mesh>
+ (%make-mesh id length)
+ mesh?
+ (id mesh-id)
+ (length mesh-length))
+
+(define (generate-vertex-array)
+ (let ((bv (u32vector 1)))
+ (glGenVertexArrays 1 (bytevector->pointer bv))
+ (u32vector-ref bv 0)))
+
+(define (apply-mesh vao)
+ (glBindVertexArray (mesh-id vao)))
+
+;; emacs: (put 'with-mesh 'scheme-indent-function 1)
+(define-syntax-rule (with-mesh vao body ...)
+ (begin
+ (apply-mesh vao)
+ body ...
+ (glBindVertexArray 0)))
+
+(define (vertex-attrib-pointer location vbo)
+ (glEnableVertexAttribArray location)
+ (with-vertex-buffer vbo
+ (glVertexAttribPointer location (vertex-buffer-attr-size vbo)
+ (data-type float) #f 0 %null-pointer)))
+
+(define (make-mesh indices positions textures)
+ (let ((mesh (%make-mesh (generate-vertex-array)
+ (vector-length indices)))
+ (positions (make-vertex-buffer positions))
+ (textures (make-vertex-buffer textures)))
+ (with-mesh mesh
+ (vertex-attrib-pointer vertex-position-location positions)
+ (if textures
+ (vertex-attrib-pointer vertex-texture-location textures))
+ (bind-vertex-buffer (make-vertex-buffer indices #t)))
+ mesh))
diff --git a/sly/render/model.scm b/sly/render/model.scm
index c766b76..7f9ead9 100644
--- a/sly/render/model.scm
+++ b/sly/render/model.scm
@@ -37,7 +37,7 @@
#:use-module (sly render camera)
#:use-module (sly render color)
#:use-module (sly render context)
- #:use-module (sly render vertex-array)
+ #:use-module (sly render mesh)
#:export (make-model model model-inherit
model?
model-mesh model-texture model-shader model-color
@@ -49,8 +49,6 @@
(define-record-type <model>
(%make-model mesh texture shader color blend-mode depth-test?)
model?
- ;; This is a vertex array.
- ;; TODO: Rename <vertex-array> to mesh and remove old mesh type.
(mesh model-mesh)
(texture model-texture)
(shader model-shader)
@@ -103,13 +101,13 @@ CONTEXT."
(set-render-context-depth-test?! context depth-test?)
(set-render-context-blend-mode! context blend-mode)
(set-render-context-shader! context shader)
- (set-render-context-vertex-array! context mesh)
+ (set-render-context-mesh! context mesh)
(set-render-context-texture! context texture)
;; TODO: Support user-defined uniforms.
(uniform-set! shader "mvp" mvp)
(uniform-set! shader "color" color)
(glDrawElements (begin-mode triangles)
- (vertex-array-length mesh)
+ (mesh-length mesh)
(data-type unsigned-int)
%null-pointer)))))
diff --git a/sly/render/shape.scm b/sly/render/shape.scm
index d6d9bf3..2df6b93 100644
--- a/sly/render/shape.scm
+++ b/sly/render/shape.scm
@@ -24,6 +24,7 @@
(define-module (sly render shape)
#:use-module (sly math)
#:use-module (sly render mesh)
+ #:use-module (sly render model)
#:use-module (sly render shader)
#:use-module (sly render texture)
#:use-module (sly math vector)
@@ -31,86 +32,84 @@
(define* (make-cube size #:optional #:key (texture #f)
(shader (load-default-shader)))
- (let ((half-size (half size)))
- (make-mesh
- #:shader shader
- #:texture texture
- #:indices #(
- ;; Front
- 0 3 2 0 2 1
- ;; Back
- 4 6 7 4 5 6
- ;; Top
- 8 11 10 8 10 9
- ;; Bottom
- 12 14 15 12 13 14
- ;; Left
- 16 19 18 16 18 17
- ;; Right
- 20 22 23 20 21 22)
- #:positions (vector
- ;; Front
- (vector3 (- half-size) (- half-size) (- half-size))
- (vector3 half-size (- half-size) (- half-size))
- (vector3 half-size half-size (- half-size))
- (vector3 (- half-size) half-size (- half-size))
- ;; Back
- (vector3 (- half-size) (- half-size) half-size)
- (vector3 half-size (- half-size) half-size)
- (vector3 half-size half-size half-size)
- (vector3 (- half-size) half-size half-size)
- ;; Top
- (vector3 (- half-size) half-size (- half-size))
- (vector3 half-size half-size (- half-size))
- (vector3 half-size half-size half-size)
- (vector3 (- half-size) half-size half-size)
- ;; Bottom
- (vector3 (- half-size) (- half-size) (- half-size))
- (vector3 half-size (- half-size) (- half-size))
- (vector3 half-size (- half-size) half-size)
- (vector3 (- half-size) (- half-size) half-size)
- ;; Left
- (vector3 (- half-size) (- half-size) (- half-size))
- (vector3 (- half-size) half-size (- half-size))
- (vector3 (- half-size) half-size half-size)
- (vector3 (- half-size) (- half-size) half-size)
- ;; Right
- (vector3 half-size (- half-size) (- half-size))
- (vector3 half-size half-size (- half-size))
- (vector3 half-size half-size half-size)
- (vector3 half-size (- half-size) half-size))
- #:textures (let ((s1 (texture-s1 texture))
- (t1 (texture-t1 texture))
- (s2 (texture-s2 texture))
- (t2 (texture-t2 texture)))
- (vector
- ;; Front
- (vector2 s1 t1)
- (vector2 s2 t1)
- (vector2 s2 t2)
- (vector2 s1 t2)
- ;; Back
- (vector2 s1 t1)
- (vector2 s2 t1)
- (vector2 s2 t2)
- (vector2 s1 t2)
- ;; Top
- (vector2 s1 t1)
- (vector2 s2 t1)
- (vector2 s2 t2)
- (vector2 s1 t2)
- ;; Bottom
- (vector2 s1 t1)
- (vector2 s2 t1)
- (vector2 s2 t2)
- (vector2 s1 t2)
- ;; Left
- (vector2 s1 t1)
- (vector2 s2 t1)
- (vector2 s2 t2)
- (vector2 s1 t2)
- ;; Right
- (vector2 s1 t1)
- (vector2 s2 t1)
- (vector2 s2 t2)
- (vector2 s1 t2))))))
+ (let* ((half-size (half size))
+ (mesh (make-mesh #(
+ ;; Front
+ 0 3 2 0 2 1
+ ;; Back
+ 4 6 7 4 5 6
+ ;; Top
+ 8 11 10 8 10 9
+ ;; Bottom
+ 12 14 15 12 13 14
+ ;; Left
+ 16 19 18 16 18 17
+ ;; Right
+ 20 22 23 20 21 22)
+ (vector
+ ;; Front
+ (vector3 (- half-size) (- half-size) (- half-size))
+ (vector3 half-size (- half-size) (- half-size))
+ (vector3 half-size half-size (- half-size))
+ (vector3 (- half-size) half-size (- half-size))
+ ;; Back
+ (vector3 (- half-size) (- half-size) half-size)
+ (vector3 half-size (- half-size) half-size)
+ (vector3 half-size half-size half-size)
+ (vector3 (- half-size) half-size half-size)
+ ;; Top
+ (vector3 (- half-size) half-size (- half-size))
+ (vector3 half-size half-size (- half-size))
+ (vector3 half-size half-size half-size)
+ (vector3 (- half-size) half-size half-size)
+ ;; Bottom
+ (vector3 (- half-size) (- half-size) (- half-size))
+ (vector3 half-size (- half-size) (- half-size))
+ (vector3 half-size (- half-size) half-size)
+ (vector3 (- half-size) (- half-size) half-size)
+ ;; Left
+ (vector3 (- half-size) (- half-size) (- half-size))
+ (vector3 (- half-size) half-size (- half-size))
+ (vector3 (- half-size) half-size half-size)
+ (vector3 (- half-size) (- half-size) half-size)
+ ;; Right
+ (vector3 half-size (- half-size) (- half-size))
+ (vector3 half-size half-size (- half-size))
+ (vector3 half-size half-size half-size)
+ (vector3 half-size (- half-size) half-size))
+ (let ((s1 (texture-s1 texture))
+ (t1 (texture-t1 texture))
+ (s2 (texture-s2 texture))
+ (t2 (texture-t2 texture)))
+ (vector
+ ;; Front
+ (vector2 s1 t1)
+ (vector2 s2 t1)
+ (vector2 s2 t2)
+ (vector2 s1 t2)
+ ;; Back
+ (vector2 s1 t1)
+ (vector2 s2 t1)
+ (vector2 s2 t2)
+ (vector2 s1 t2)
+ ;; Top
+ (vector2 s1 t1)
+ (vector2 s2 t1)
+ (vector2 s2 t2)
+ (vector2 s1 t2)
+ ;; Bottom
+ (vector2 s1 t1)
+ (vector2 s2 t1)
+ (vector2 s2 t2)
+ (vector2 s1 t2)
+ ;; Left
+ (vector2 s1 t1)
+ (vector2 s2 t1)
+ (vector2 s2 t2)
+ (vector2 s1 t2)
+ ;; Right
+ (vector2 s1 t1)
+ (vector2 s2 t1)
+ (vector2 s2 t2)
+ (vector2 s1 t2))))))
+ (make-model #:mesh mesh #:texture texture #:shader shader)))
diff --git a/sly/render/sprite.scm b/sly/render/sprite.scm
index 950a81b..a4e4cd4 100644
--- a/sly/render/sprite.scm
+++ b/sly/render/sprite.scm
@@ -34,6 +34,7 @@
#:use-module (sly utils)
#:use-module (sly math)
#:use-module (sly render mesh)
+ #:use-module (sly render model)
#:use-module (sly render shader)
#:use-module (sly signal)
#:use-module (sly render texture)
@@ -48,8 +49,7 @@
(define* (make-sprite texture #:optional #:key
(shader (load-default-shader))
- (anchor 'center)
- (color white))
+ (anchor 'center))
"Return a 2D rectangular mesh that displays the image TEXTURE. The
size of the mesh is the size of TEXTURE, in pixels. Optionally, a
custom SHADER can be specified."
@@ -61,28 +61,27 @@ custom SHADER can be specified."
(s1 (texture-s1 texture))
(t1 (texture-t1 texture))
(s2 (texture-s2 texture))
- (t2 (texture-t2 texture)))
- (make-mesh
- #:shader shader
- #:texture texture
- #:indices #(0 3 2 0 2 1)
- #:positions (vector
- (vector3 x1 y1 0)
- (vector3 x2 y1 0)
- (vector3 x2 y2 0)
- (vector3 x1 y2 0))
- #:textures (vector
- (vector2 s1 t1)
- (vector2 s2 t1)
- (vector2 s2 t2)
- (vector2 s1 t2)))))
+ (t2 (texture-t2 texture))
+ (mesh (make-mesh #(0 3 2 0 2 1)
+ (vector
+ (vector3 x1 y1 0)
+ (vector3 x2 y1 0)
+ (vector3 x2 y2 0)
+ (vector3 x1 y2 0))
+ (vector
+ (vector2 s1 t1)
+ (vector2 s2 t1)
+ (vector2 s2 t2)
+ (vector2 s1 t2)))))
+ (make-model #:shader shader
+ #:texture texture
+ #:mesh mesh)))
(define* (load-sprite file-name #:optional #:key (shader (load-default-shader))
- (anchor 'center) (color white))
+ (anchor 'center))
"Return a sprite mesh for the texture loaded from FILE-NAME.
Optionally, a custom SHADER can be specified."
- (make-sprite (load-texture file-name) #:shader shader
- #:anchor anchor #:color color))
+ (make-sprite (load-texture file-name) #:shader shader #:anchor anchor))
(define* (make-animated-sprite textures frame-duration #:optional #:key
(loop? #t)
diff --git a/sly/render/vertex-array.scm b/sly/render/vertex-array.scm
deleted file mode 100644
index 45fcdd7..0000000
--- a/sly/render/vertex-array.scm
+++ /dev/null
@@ -1,173 +0,0 @@
-;;; 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:
-;;
-;; Vertex arrays encapsulate the geometry for a single OpenGL draw
-;; call.
-;;
-;;; Code:
-
-(define-module (sly render vertex-array)
- #:use-module (system foreign)
- #:use-module (ice-9 match)
- #:use-module (srfi srfi-9)
- #:use-module (srfi srfi-43)
- #:use-module (rnrs bytevectors)
- #:use-module (gl)
- #:use-module (gl low-level)
- #:use-module (sly wrappers gl)
- #:use-module (sly math vector)
- #:use-module (sly render color)
- #:use-module (sly render shader)
- #:export (make-vertex-array
- vertex-array?
- vertex-array-id vertex-array-length
- apply-vertex-array with-vertex-array))
-
-;;;
-;;; Vertex Buffers
-;;;
-
-(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
- (match-lambda
- ((? number? _) 1)
- ((? vector2? _) 2)
- ((? vector3? _) 3)
- ((or (? vector4? _)
- (? color? _))
- 4)
- (attr
- (error "Unsupported vertex buffer 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))
-
-;;;
-;;; Vertex Arrays
-;;;
-
-(define-record-type <vertex-array>
- (%make-vertex-array id length)
- vertex-array?
- (id vertex-array-id)
- (length vertex-array-length))
-
-(define (generate-vertex-array)
- (let ((bv (u32vector 1)))
- (glGenVertexArrays 1 (bytevector->pointer bv))
- (u32vector-ref bv 0)))
-
-(define (apply-vertex-array vao)
- (glBindVertexArray (vertex-array-id vao)))
-
-(define-syntax-rule (with-vertex-array vao body ...)
- (begin
- (apply-vertex-array vao)
- body ...
- (glBindVertexArray 0)))
-
-(define (vertex-attrib-pointer location vbo)
- (glEnableVertexAttribArray location)
- (with-vertex-buffer vbo
- (glVertexAttribPointer location (vertex-buffer-attr-size vbo)
- (data-type float) #f 0 %null-pointer)))
-
-(define (make-vertex-array indices positions textures)
- (let ((vao (%make-vertex-array (generate-vertex-array)
- (vector-length indices)))
- (positions (make-vertex-buffer positions))
- (textures (make-vertex-buffer textures)))
- (with-vertex-array vao
- (vertex-attrib-pointer vertex-position-location positions)
- (if textures
- (vertex-attrib-pointer vertex-texture-location textures))
- (bind-vertex-buffer (make-vertex-buffer indices #t)))
- vao))