Skip to content

Commit

Permalink
WIP: changes to shader-setup stuff
Browse files Browse the repository at this point in the history
that's still all pretty broken
  • Loading branch information
benswift committed Feb 15, 2015
1 parent 9f5e19d commit b659af5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 52 deletions.
13 changes: 9 additions & 4 deletions examples/external/shader-tutorials/pattern.xtm
Expand Up @@ -8,7 +8,7 @@
(gl_set_depth_test)

(bind-val vertices float* 9)
(bind-val colours float* 9)
(bind-val colours float* 18)

(bind-func init_arrays
(lambda ()
Expand All @@ -34,15 +34,20 @@
(bind-func draw
(let ((vert_vbo (create_vbo vertices 12))
(colour_vbo (create_vbo colours 18))
(vao (create_vao vert_vbo 3 colour_vbo 3)))
(vao (create_vao)))
(bind_vbo vert_vbo)
(bind_vbo colour_vbo)
(println (tref vert_vbo 0))
(bind_attribute vao vert_vbo 0)
(bind_attribute vao colour_vbo 1)
(lambda (program)
(glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
(glUseProgram program)
(glBindVertexArray vao)
(glBindVertexArray (tref vao 0))
(glDrawArrays GL_TRIANGLES_STRIP 0 4)
;; the update call is only necessary if you're changing the
;; vert/colour data
(update_vbo vert_vbo vertices 12)
(update_vbo vert_vbo)
void)))

(define gl-loop
Expand Down
128 changes: 82 additions & 46 deletions examples/external/shader-tutorials/shader-setup.xtm
@@ -1,79 +1,95 @@
(sys:load "libs/external/opengl.xtm")

(define width 1440.0)
(define height 900.0)
(bind-val width float 1440.0)
(bind-val height float 900.0)
(define fullscreen #t)

(define *gl-window* (gl:make-ctx-core ":0.0" fullscreen 0.0 0.0 width height))

;; how did it all go?

(gl_print_driver_info)

;; id, nbytes, data
(bind-type VBO <i32,i32,i8*>)
(bind-alias GLbitfield i32)
(bind-alias GLboolean i8)
(bind-alias GLbyte i8)
(bind-alias GLclampf i32)
(bind-alias GLenum i32)
(bind-alias GLfloat i32)
(bind-alias GLint i32)
(bind-alias GLshort i16)
(bind-alias GLsizei i32)
(bind-alias GLubyte i8)
(bind-alias GLuint i32)
(bind-alias GLushort i16)
(bind-alias GLvoid i8)
(bind-alias GLintptr i64)
(bind-alias GLsizeiptr i64)

;; id, type, type, size (bytes), data
(bind-type VBO <GLuint,GLenum,GLsizeiptr,GLvoid*>)

(bind-func print_VBO
(lambda (vbo:VBO*)
(printf "VBO: <id=%d nbytes=%d data=%p>"
(printf "VBO: <id=%d type=%s nbytes=%d data=%p>"
(tref vbo 0)
(tref vbo 1)
(tref vbo 2))))
(let ((type (tref vbo 1)))
(cond ((= type GL_BYTE) "byte")
((= type GL_SHORT) "short")
((= type GL_INT) "int")
((= type GL_FLOAT) "float")
(else "unknown")))
(tref vbo 2)
(tref vbo 3))))

(bind-poly print print_VBO)

(bind-func tostring_VBO
(lambda (vbo:VBO*)
(let ((s:i8* (salloc 256)))
(sprintf s "VBO: <id=%d nbytes=%d data=%p>"
(sprintf s "VBO: <id=%d type=%s nbytes=%d data=%p>"
(tref vbo 0)
(tref vbo 1)
(tref vbo 2))
(let ((type (tref vbo 1)))
(cond ((= type GL_BYTE) "byte")
((= type GL_SHORT) "short")
((= type GL_INT) "int")
((= type GL_FLOAT) "float")
(else "unknown")))
(tref vbo 2)
(tref vbo 3))
(Str s))))

(bind-poly tostring tostring_VBO)

(bind-func create_vbo_float
(lambda (buf:float* buflen)
(let ((vbo:VBO* (halloc))
(id:i32* (salloc)))
(let ((vbo:VBO* (zalloc))
(id:GLuint* (salloc)))
(glGenBuffers 1 id)
(tfill! vbo (pref id 0) (* buflen 4) (cast buf i8*))
(tfill! vbo (pref id 0) GL_FLOAT (* buflen 4) (cast buf GLvoid*))
(gl_print_error "Error creating VBO")
vbo)))

(bind-func create_vbo_i32
(lambda (buf:i32* buflen)
(let ((vbo:VBO* (halloc))
(id:i32* (salloc)))
(let ((vbo:VBO* (zalloc))
(id:GLuint* (salloc)))
(glGenBuffers 1 id)
(tfill! vbo (pref id 0) (* buflen 4) (cast buf i8*))
(tfill! vbo (pref id 0) GL_INT (* buflen 4) (cast buf GLvoid*))
(gl_print_error "Error creating VBO")
vbo)))

(bind-poly create_vbo create_vbo_float)
(bind-poly create_vbo create_vbo_i32)

(bind-func bind_vbo
(lambda (vbo:VBO* type)
(lambda (vbo:VBO*)
(glBindBuffer GL_ARRAY_BUFFER (tref vbo 0))
(glBufferData GL_ARRAY_BUFFER (tref vbo 1) (tref vbo 2) type)
(glBufferData GL_ARRAY_BUFFER (tref vbo 2) (tref vbo 3) (tref vbo 1))
(glBindBuffer GL_ARRAY_BUFFER 0)
(gl_print_error "Error binding VBO")))

(bind-func update_vbo
(lambda (vbo:VBO* type)
(lambda (vbo:VBO*)
(glBindBuffer GL_ARRAY_BUFFER (tref vbo 0))
(glBufferData GL_ARRAY_BUFFER (tref vbo 1) null type) ;; free the old memory
(glBufferSubData GL_ARRAY_BUFFER 0 (tref vbo 1) (tref vbo 2))
(glBufferData GL_ARRAY_BUFFER (tref vbo 2) null (tref vbo 1)) ;; free the old memory
(glBufferSubData GL_ARRAY_BUFFER 0 (tref vbo 2) (tref vbo 3))
(glBindBuffer GL_ARRAY_BUFFER 0)
(gl_print_error "Error updating VBO")))

(bind-func delete_vbo
(lambda (vbo:VBO*)
(let ((id:i32* (salloc)))
(let ((id:GLuint* (salloc)))
(pset! id 0 (tref vbo 0))
(glDeleteBuffers 1 id)
(gl_print_error "Error deleting VBO")
Expand All @@ -98,29 +114,35 @@

(bind-func create_vao
(lambda ()
(let ((vao:VAO* (halloc))
(id:i32* (salloc)))
(let ((vao:VAO* (zalloc))
(id:GLuint* (salloc)))
(glGenVertexArrays 1 id)
(gl_print_error "Error creating VAO")
(tset! vao 0 (pref id 0))
vao)))

(bind-func bind_attribute_full
(lambda (vao:VAO* vbo:VBO* index size type stride offset)
(lambda (vao:VAO* vbo:VBO* index stride offset)
(println "checkpoint " 0)
(glBindVertexArray (tref vao 0))
(println "checkpoint " 1)
(glEnableVertexAttribArray index)
(println "checkpoint " 2)
(glBindBuffer GL_ARRAY_BUFFER (tref vbo 0))
(glVertexAttribPointer index size type GL_FALSE stride (pref-ptr (cast null i8*) offset))
(glBindBuffer GL_ARRAY_BUFFER 0)
(glBindVertexArray 0)
(println "checkpoint " 3)
(glVertexAttribPointer index (convert (tref vbo 2)) (tref vbo 1) GL_FALSE stride (pref-ptr (cast null GLvoid*) offset))
(println "checkpoint " 4)
;; (glBindBuffer GL_ARRAY_BUFFER 0)
(println "checkpoint " 5)
;; (glBindVertexArray 0)
(gl_print_error "Error binding VAO attribute")))

(bind-func bind_attribute_float_packed
(lambda (vao vbo index size)
(bind_attribute_full vao vbo index size GL_FLOAT 0 0)))
(bind-func bind_attribute_packed
(lambda (vao vbo index)
(bind_attribute_full vao vbo index 0 0)))

(bind-poly bind_attribute bind_attribute_full)
(bind-poly bind_attribute bind_attribute_float_packed)
(bind-poly bind_attribute bind_attribute_packed)

(bind-func draw_vertex_array
(lambda (vao:VAO* draw_mode first count)
Expand All @@ -130,8 +152,8 @@
(gl_print_error "Error drawing vertex array")))

(bind-func delete_vao
(lambda (vao:VBO*)
(let ((id:i32* (salloc)))
(lambda (vao:VAO*)
(let ((id:GLuint* (salloc)))
(pset! id 0 (tref vao 0))
(glDeleteVertexArrays 1 id)
(gl_print_error "Error deleting VAO")
Expand All @@ -141,13 +163,27 @@

(bind-func create_texture
(lambda ()
(let ((id:i32* (salloc)))
(let ((id:GLuint* (salloc)))
(glGenTextures 1 id)
(pref id 0))))

(bind-func delete_texture
(lambda (tex)
(let ((id:i32* (salloc)))
(let ((id:GLuint* (salloc)))
(pset! id 0 tex)
(glDeleteTextures 1 id)
(gl_print_error "Error deleting texture"))))

;; actually do the things...

(define width 1440.0)
(define height 900.0)
(bind-val width float 1440.0)
(bind-val height float 900.0)
(define fullscreen #t)

(define *gl-window* (gl:make-ctx-core ":0.0" fullscreen 0.0 0.0 width height))

;; how did it all go?

(gl_print_driver_info)
8 changes: 6 additions & 2 deletions examples/external/shader-tutorials/triangle.xtm
Expand Up @@ -30,11 +30,15 @@
(bind-func draw
(let ((vert_vbo (create_vbo vertices 9))
(colour_vbo (create_vbo colours 9))
(vao (create_vao vert_vbo 3 colour_vbo 3)))
(vao (create_vao)))
;; (bind_vbo vert_vbo)
;; (bind_vbo colour_vbo)
(bind_attribute vao vert_vbo 0)
(bind_attribute vao colour_vbo 0)
(lambda (program)
(glClear (+ GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
(glUseProgram program)
(glBindVertexArray vao)
;; (glBindVertexArray (tref vao 0))
(glDrawArrays GL_TRIANGLES 0 3)
void)))

Expand Down

0 comments on commit b659af5

Please sign in to comment.