#include #include #include "gl/glad/glad.h" #include #include #include #include "util/util.h" #include "gl.h" int gl_create_buffer(lua_State *L); int gl_bind_buffer(lua_State *L); int gl_buffer_data(lua_State *L); int gl_vertex_array_create(lua_State *L); int gl_vertex_array_bind(lua_State *L); int gl_vertex_attrib_pointer(lua_State *L); int gl_vertex_array_enable_attrib(lua_State *L); void setup_data(lua_State *L, int gl_index) { int tbl = hs_create_table(L, /* functions */ hs_str_cfunc("GenBuffers", gl_create_buffer), hs_str_cfunc("BindBuffer", gl_bind_buffer), hs_str_cfunc("BufferData", gl_buffer_data), hs_str_cfunc("GenVertexArrays", gl_vertex_array_create), hs_str_cfunc("BindVertexArray", gl_vertex_array_bind), hs_str_cfunc("VertexAttribPointer", gl_vertex_attrib_pointer), hs_str_cfunc("EnableVertexAttribArray", gl_vertex_array_enable_attrib), /******** enums ********/ /* buffer bind targets */ hs_str_int("ARRAY_BUFFER", GL_ARRAY_BUFFER), hs_str_int("ELEMENT_ARRAY_BUFFER", GL_ELEMENT_ARRAY_BUFFER), /* buffer usage patters */ hs_str_int("STREAM_DRAW", GL_STREAM_DRAW), hs_str_int("STATIC_DRAW", GL_STATIC_DRAW), hs_str_int("DYNAMIC_DRAW", GL_DYNAMIC_DRAW), ); append_table(L, gl_index, tbl); lua_pop(L, 1); } int gl_create_buffer(lua_State *L) { int buf; glGenBuffers(1, &buf); lua_pushinteger(L, buf); return 1; } int gl_bind_buffer(lua_State *L) { lua_Integer target, buf; hs_parse_args(L, hs_int(target), hs_int(buf)); glBindBuffer(target, buf); return 0; } #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