diff options
Diffstat (limited to 'src/opengl/data.c')
-rw-r--r-- | src/opengl/data.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/opengl/data.c b/src/opengl/data.c new file mode 100644 index 0000000..b7dc581 --- /dev/null +++ b/src/opengl/data.c @@ -0,0 +1,164 @@ +#include <stdlib.h> +#include <stdbool.h> +#include <glad/glad.h> +#include <GLFW/glfw3.h> +#include <lua.h> +#include <lauxlib.h> +#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<len; i++) { \ + lua_rawgeti(L, tbl, i+1); \ + if (!lua_isnumber(L, -1)) \ + luaL_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; + target = luaL_checkinteger(L, 1); + type = luaL_checkinteger(L, 2); + luaL_checktype(L, 3, LUA_TTABLE); + int table = 3; + usage = luaL_checkinteger(L, 4); + + /* build raw buffer */ + 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: + luaL_error(L, "invalid type"); + } + + /* call */ + glBufferData(target, len, buf, usage); + free(buf); + return 0; +} + + +int gl_vertex_array_create(lua_State *L) +{ + int array; + glGenVertexArrays(1, &array); + lua_pushinteger(L, array); + return 1; +} + + +int gl_vertex_array_bind(lua_State *L) +{ + lua_Integer array = luaL_checkinteger(L, 1); + glBindVertexArray(array); + return 0; +} + + +int gl_vertex_attrib_pointer(lua_State *L) +{ + lua_Integer index, size, stride, offset; + bool normalized; + index = luaL_checkinteger(L, 1); + size = luaL_checkinteger(L, 2); + normalized = lua_toboolean(L, 3); + stride = luaL_checkinteger(L, 4); + offset = luaL_checkinteger(L, 5); + + glVertexAttribPointer(index, size, GL_FLOAT, + normalized, stride*sizeof(float), + (void*) (offset*sizeof(float))); + return 0; +} + + +int gl_vertex_array_enable_attrib(lua_State *L) +{ + lua_Integer index = luaL_checkinteger(L, 1); + glEnableVertexAttribArray(index); + return 0; +} |