Browse Source

Use an index buffer instead

windows-imgui
Macoy Madson 8 months ago
parent
commit
3477a00471
  1. 42
      src/OpenGL.cake

42
src/OpenGL.cake

@ -85,24 +85,31 @@ void main()\n
(glDeleteShader vertex-shader)
(glDeleteShader fragment-shader)))
(var triangle-array-object gl-id)
(glGenVertexArrays 1 (addr triangle-array-object))
(var start-triangles int 0)
(var num-triangles int 3)
(var mesh-array-object gl-id)
(glGenVertexArrays 1 (addr mesh-array-object))
(var start-indices int 0)
(var num-indices int 3)
(scope
(var-static vertices ([] float)
(array
0.25f 0.25f 0.25f
0.75f 0.25f 0.25f
0.75f 0.75f 0.25f
0.25f 0.75f 0.25f))
(var-static indices ([] int)
(array 0 1 3
1 2 3))
(set num-indices (array-size indices))
;; From this point on, configuration will be saved in the vertex array object
(glBindVertexArray triangle-array-object)
(glBindVertexArray mesh-array-object)
;; Make our triangle vertex array
(var vertex-buffer-id gl-id)
(glGenBuffers 1 (addr vertex-buffer-id))
(glBindBuffer GL_ARRAY_BUFFER vertex-buffer-id)
(var-static vertices ([] float)
(array
0.25f 0.25f 0.25f
0.5f 0.25f 0.25f
0.5f 0.75f 0.25f))
(glBufferData GL_ARRAY_BUFFER (sizeof vertices) vertices GL_STATIC_DRAW)
(var layout-location int 0) ;; Should match vertex shader layout
@ -112,7 +119,14 @@ void main()\n
(* 3 (sizeof float))
;; Buffer start offset
(type-cast 0 (* void)))
(glEnableVertexAttribArray layout-location))
(glEnableVertexAttribArray layout-location)
;; Make our index buffer array
(var index-buffer-id gl-id)
(glGenBuffers 1 (addr index-buffer-id))
(glBindBuffer GL_ELEMENT_ARRAY_BUFFER index-buffer-id)
(glBufferData GL_ELEMENT_ARRAY_BUFFER (sizeof indices) indices GL_STATIC_DRAW))
(var exit-reason (* (const char)) null)
(while (not exit-reason)
@ -128,8 +142,10 @@ void main()\n
(glClear GL_COLOR_BUFFER_BIT)
(glUseProgram shader-program)
(glBindVertexArray triangle-array-object)
(glDrawArrays GL_TRIANGLES start-triangles num-triangles)
(glBindVertexArray mesh-array-object)
(glDrawElements GL_TRIANGLES num-indices GL_UNSIGNED_INT
(type-cast 0 (* void)))
(glBindVertexArray 0) ;; Unbind
(SDL_GL_SwapWindow window))

Loading…
Cancel
Save