#include #include #include #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) { struct honey_tbl_t tbl[] = { /* functions */ H_FUNC("GenBuffers", gl_create_buffer), H_FUNC("BindBuffer", gl_bind_buffer), H_FUNC("BufferData", gl_buffer_data), H_FUNC("GenVertexArrays", gl_vertex_array_create), H_FUNC("BindVertexArray", gl_vertex_array_bind), H_FUNC("VertexAttribPointer", gl_vertex_attrib_pointer), H_FUNC("EnableVertexAttribArray", gl_vertex_array_enable_attrib), /******** enums ********/ /* buffer bind targets */ H_INT("ARRAY_BUFFER", GL_ARRAY_BUFFER), H_INT("ELEMENT_ARRAY_BUFFER", GL_ELEMENT_ARRAY_BUFFER), /* buffer usage patters */ H_INT("STREAM_DRAW", GL_STREAM_DRAW), H_INT("STATIC_DRAW", GL_STATIC_DRAW), H_INT("DYNAMIC_DRAW", GL_DYNAMIC_DRAW), H_END }; create_table(L, tbl); append_table(L, gl_index, lua_gettop(L)); 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) { int target = luaL_checkinteger(L, 1); int buf = luaL_checkinteger(L, 2); 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) \ luaL_error(L, "failed to allocate intermediary buffer"); \ for (int i=0; i