diff options
-rw-r--r-- | demo/honey.lua | 20 | ||||
-rw-r--r-- | src/gl/data.c | 72 | ||||
-rw-r--r-- | src/gl/drawing.c | 11 | ||||
-rw-r--r-- | 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<len; i++) { \ + lua_rawgeti(L, tbl, i+1); \ + if (!lua_isnumber(L, -1)) \ + hs_throw_error(L, "all elements must be numbers (failed at index %d)", i); \ + buf[i] = conversion(L, -1); \ + lua_pop(L, 1); \ + } \ + return buf; \ + } + +GET_BUFFER_TYPE(unsigned int, uint, lua_tointeger) +GET_BUFFER_TYPE(int, int, lua_tointeger) +GET_BUFFER_TYPE(float, float, lua_tonumber) + int gl_buffer_data(lua_State *L) { lua_Integer target, type, usage; int table; hs_parse_args(L, hs_int(target), hs_int(type), hs_tbl(table), hs_int(usage)); - if (type != GL_INT && type != GL_FLOAT) { - hs_throw_error(L, "invalid type"); - } - /* build raw buffer */ - size_t len = lua_objlen(L, table); - void *buf; - if (type == GL_FLOAT) { - float *fbuf = malloc(len * sizeof(float)); - if (fbuf == NULL) - hs_throw_error(L, "failed to allocate intermediary fbuffer"); - for (int i=0; i<len; i++) { - lua_rawgeti(L, table, i+1); - if (!lua_isnumber(L, -1)) { - hs_throw_error(L, "all table items must be numbers (failed at index %d)", i); - } - fbuf[i] = lua_tonumber(L, -1); - lua_pop(L, 1); - } - len = len * sizeof(float); - buf = fbuf; - } - else { - int *ibuf = malloc(len * sizeof(int)); - if (ibuf == NULL) - hs_throw_error(L, "failed to allocate intermediary ibuffer"); - for (int i=0; i<len; i++) { - lua_rawgeti(L, table, i+1); - if (!lua_isnumber(L, -1)) { - hs_throw_error(L, "all table items must be integers (failed at index %d)", i); - } - ibuf[i] = lua_tointeger(L, -1); - lua_pop(L, 1); - } - len = len * sizeof(int); - buf = ibuf; + void *buf; size_t len; + switch(type) { + case GL_UNSIGNED_INT: + buf = get_buffer_uint(L, &len, table); + break; + + case GL_INT: + buf = get_buffer_int(L, &len, table); + break; + + case GL_FLOAT: + buf = get_buffer_float(L, &len, table); + break; + + default: + hs_throw_error(L, "invalid type"); } /* call */ diff --git a/src/gl/drawing.c b/src/gl/drawing.c index a64f56d..ca914e0 100644 --- a/src/gl/drawing.c +++ b/src/gl/drawing.c @@ -5,6 +5,7 @@ int gl_set_viewport(lua_State *L); int gl_draw_arrays(lua_State *L); +int gl_draw_elements(lua_State *L); int gl_set_clear_color(lua_State *L); int gl_clear(lua_State *L); @@ -24,6 +25,7 @@ void setup_drawing(lua_State *L, int gl_index) hs_create_table(L, hs_str_cfunc("drawArrays", gl_draw_arrays), + hs_str_cfunc("drawElements", gl_draw_elements), hs_str_cfunc("setClearColor", gl_set_clear_color), hs_str_cfunc("clear", gl_clear), hs_str_cfunc("setViewport", gl_set_viewport), @@ -62,6 +64,15 @@ int gl_draw_arrays(lua_State *L) } +int gl_draw_elements(lua_State *L) +{ + lua_Integer mode, count, type, offset; + hs_parse_args(L, hs_int(mode), hs_int(count), hs_int(type), hs_int(offset)); + glDrawElements(mode, count, type, (const void*)offset); + return 0; +} + + int gl_set_viewport(lua_State *L) { lua_Integer x, y, w, h; diff --git a/src/gl/gl.c b/src/gl/gl.c index 8a1cb17..8f56d33 100644 --- a/src/gl/gl.c +++ b/src/gl/gl.c @@ -21,7 +21,8 @@ int gl_get_error(lua_State *L); void setup_gl(lua_State *L, int honey_index) { int data_types = hs_create_table(L, - hs_str_int("integer", GL_INT), + hs_str_int("uint", GL_UNSIGNED_INT), + hs_str_int("int", GL_INT), hs_str_int("float", GL_FLOAT), ); |