From 249833947ff691dd79453c4d817387a6922236d6 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 11:38:26 -0500 Subject: add element buffers --- demo/honey.lua | 20 ++++++++++++---- src/gl/data.c | 72 +++++++++++++++++++++++++++++--------------------------- src/gl/drawing.c | 11 +++++++++ src/gl/gl.c | 3 ++- 4 files changed, 65 insertions(+), 41 deletions(-) diff --git a/demo/honey.lua b/demo/honey.lua index 5e14461..2b1759f 100644 --- a/demo/honey.lua +++ b/demo/honey.lua @@ -62,14 +62,20 @@ gl.shader.delete(fragmentShader) local vertices = { - -0.5, -0.5, 0.0, - 0.5, -0.5, 0.0, - 0.0, 0.5, 0.0 -} + 0.5, 0.5, 0.0, + 0.5, -0.5, 0.0, + -0.5, -0.5, 0.0, + -0.5, 0.5, 0.0 +} +local indices = { + 0, 1, 3, + 1, 2, 3 +} local vertexArray = gl.data.createVertexArray() local vertexBuffer = gl.data.createBuffer() +local elementBuffer = gl.data.createBuffer() gl.data.bindVertexArray(vertexArray) gl.data.bindBuffer(gl.data.bufferTarget.arrayBuffer, vertexBuffer) @@ -78,6 +84,9 @@ if err ~= gl.errorType.noError then error(gl.errorName(err)) end gl.data.bufferData(gl.data.bufferTarget.arrayBuffer, gl.dataType.float, vertices, gl.data.bufferUsage.staticDraw) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end +gl.data.bindBuffer(gl.data.bufferTarget.elementArrayBuffer, elementBuffer) +gl.data.bufferData(gl.data.bufferTarget.elementArrayBuffer, gl.dataType.uint, indices, gl.data.bufferUsage.staticDraw) + gl.data.vertexAttribPointer(0, 3, false, 3, 0) if gl.getError() ~= gl.errorType.noError then error(gl.getError()) end gl.data.vertexArrayEnableAttrib(0) @@ -92,7 +101,8 @@ while not window.shouldClose(w) do gl.shader.use(shader) gl.data.bindVertexArray(vertexArray) - gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) + --gl.draw.drawArrays(gl.draw.primitiveType.triangles, 0, 3) + gl.draw.drawElements(gl.draw.primitiveType.triangles, 6, gl.dataType.uint, 0) window.swapBuffers(w) window.pollEvents() diff --git a/src/gl/data.c b/src/gl/data.c index f598784..777689a 100644 --- a/src/gl/data.c +++ b/src/gl/data.c @@ -65,48 +65,50 @@ int gl_bind_buffer(lua_State *L) } +#define GET_BUFFER_TYPE(type, name, conversion) \ + void * get_buffer_ ## name (lua_State *L, size_t *sz, int tbl) { \ + size_t len = lua_objlen(L, tbl); \ + *sz = len * sizeof(type); \ + type *buf = malloc(*sz); \ + if (buf == NULL) \ + hs_throw_error(L, "failed to allocate intermediary buffer"); \ + for (int i=0; i