#include #include #include "gl/glad/glad.h" #include #include #include #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 buffer_binding_targets = hs_create_table(L, hs_str_int("arrayBuffer", GL_ARRAY_BUFFER), hs_str_int("elementArrayBuffer", GL_ELEMENT_ARRAY_BUFFER), ); int buffer_usage_patterns = hs_create_table(L, hs_str_int("streamDraw", GL_STREAM_DRAW), hs_str_int("staticDraw", GL_STATIC_DRAW), hs_str_int("dynamicDraw", GL_DYNAMIC_DRAW), ); hs_create_table(L, hs_str_cfunc("createBuffer", gl_create_buffer), hs_str_cfunc("bindBuffer", gl_bind_buffer), hs_str_cfunc("bufferData", gl_buffer_data), hs_str_cfunc("createVertexArray", gl_vertex_array_create), hs_str_cfunc("bindVertexArray", gl_vertex_array_bind), hs_str_cfunc("vertexAttribPointer", gl_vertex_attrib_pointer), hs_str_cfunc("vertexArrayEnableAttrib", gl_vertex_array_enable_attrib), hs_str_tbl("bufferTarget", buffer_binding_targets), hs_str_tbl("bufferUsage", buffer_usage_patterns), ); lua_setfield(L, gl_index, "data"); } 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