diff options
author | sanine-a <sanine.not@pm.me> | 2020-10-27 14:33:25 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2020-10-27 14:33:25 -0500 |
commit | 3ef1112d085a101aa55d11ff894f1fb515d29c27 (patch) | |
tree | 32095f981cdfedbf6a8791b834f8ab5c82f8646e /src/mesh/mesh.c | |
parent | acc262b019206d09ce0d297f7b185862fe432601 (diff) |
add mesh lua functions and refactor lua type names
Diffstat (limited to 'src/mesh/mesh.c')
-rw-r--r-- | src/mesh/mesh.c | 152 |
1 files changed, 78 insertions, 74 deletions
diff --git a/src/mesh/mesh.c b/src/mesh/mesh.c index 99210eb..55de82c 100644 --- a/src/mesh/mesh.c +++ b/src/mesh/mesh.c @@ -1,86 +1,90 @@ #include "mesh.h" -honey_result honey_mesh_new(honey_mesh* mesh, - float* vertices, - unsigned int n_vertices, - unsigned int n_attributes, - unsigned int* attribute_sizes, - unsigned int* indices, - unsigned int n_indices) { - if (vertices == NULL || n_vertices == 0) { - return HONEY_MESH_BAD_VERTEX_DATA; - } - if (indices == NULL || n_indices == 0) { - return HONEY_MESH_BAD_INDEX_DATA; - } - - unsigned int vertex_size = 0; - for (int i=0; i<n_attributes; i++) { - vertex_size += attribute_sizes[i]; - } - - (*mesh).vertices = malloc(vertex_size*n_vertices * sizeof(float)); - if ((*mesh).vertices == NULL) { - return HONEY_MEMORY_ALLOCATION_ERROR; - } - memcpy((*mesh).vertices, vertices, vertex_size*n_vertices*sizeof(float)); - - (*mesh).indices = malloc(n_indices * sizeof(unsigned int)); - if ((*mesh).indices == NULL) { - return HONEY_MEMORY_ALLOCATION_ERROR; - } - memcpy((*mesh).indices, indices, n_indices * sizeof(unsigned int)); - - (*mesh).n_vertices = n_vertices; - (*mesh).n_indices = n_indices; - - glGenVertexArrays(1, &((*mesh).vertex_array)); - glGenBuffers(1, &((*mesh).vertex_buffer)); - glGenBuffers(1, &((*mesh).element_buffer)); - - glBindVertexArray((*mesh).vertex_array); - - glBindBuffer(GL_ARRAY_BUFFER, (*mesh).vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, vertex_size*n_vertices*sizeof(float), (*mesh).vertices, GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (*mesh).element_buffer); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, n_indices * sizeof(unsigned int), (*mesh).indices, GL_STATIC_DRAW); - - /* set up vertex attributes */ - unsigned int offset = 0; - for (int i=0; i<n_attributes; i++) { - glEnableVertexAttribArray(i); - glVertexAttribPointer(i, - attribute_sizes[i], - GL_FLOAT, - GL_FALSE, - vertex_size*sizeof(float), - (void*) (offset*sizeof(float))); - offset += attribute_sizes[i]; - } - - glBindVertexArray(0); - - return HONEY_OK; +int honey_mesh_new(lua_State* L) +{ + float* vertices; + unsigned int* indices; + int n_vertices, n_indices; + honey_lua_parse_arguments(L, 5, + HONEY_USERDATA, &vertices, + HONEY_USERDATA, &indices, + HONEY_TABLE, + HONEY_INTEGER, &n_vertices, + HONEY_INTEGER, &n_indices); + + size_t n_attributes = lua_objlen(L, 3); + unsigned int* attribute_sizes = malloc(n_attributes * sizeof(unsigned int)); + if (attribute_sizes == NULL) { + lua_pushstring(L, "failed to allocate memory for vertex attributes"); + lua_error(L); + } + + unsigned int vertex_size = 0; + for (int i=0; i<n_attributes; i++) { + lua_rawgeti(L, 2, i+1); + attribute_sizes[i] = lua_tointeger(L, -1); + vertex_size += attribute_sizes[i]; + } + lua_pop(L, n_attributes); + + honey_mesh* mesh = lua_newuserdata(L, sizeof(honey_mesh)); + mesh->n_indices = n_indices; + + glGenVertexArrays(1, &mesh->vertex_array); + glGenBuffers(1, &mesh->vertex_buffer); + glGenBuffers(1, &mesh->element_buffer); + + glBindVertexArray(mesh->vertex_array); + + glBindBuffer(GL_ARRAY_BUFFER, mesh->vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, + vertex_size * n_vertices * sizeof(float), + vertices, GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->element_buffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, + n_indices * sizeof(unsigned int), + indices, GL_STATIC_DRAW); + + unsigned int offset = 0; + for (int i=0; i<n_attributes; i++) { + glEnableVertexAttribArray(i); + glVertexAttribPointer(i, + attribute_sizes[i], + GL_FLOAT, GL_FALSE, + vertex_size * sizeof(float), + (void*) (offset * sizeof(float))); + offset += attribute_sizes[i]; + } + + glBindVertexArray(0); + return 1; } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void honey_mesh_draw(honey_mesh mesh, int shader) { - glUseProgram(shader); +int honey_mesh_draw(lua_State* L) +{ + honey_mesh* mesh; + int shader; + honey_lua_parse_arguments(L, 2, + HONEY_USERDATA, &mesh, + HONEY_INTEGER, &shader); + + glUseProgram(shader); - glBindVertexArray(mesh.vertex_array); - glDrawElements(GL_TRIANGLES, mesh.n_indices, GL_UNSIGNED_INT, 0); - glBindVertexArray(0); + glBindVertexArray(mesh->vertex_array); + glDrawElements(GL_TRIANGLES, mesh->n_indices, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); } /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -void honey_mesh_delete(honey_mesh mesh) { - free(mesh.vertices); - free(mesh.indices); - - glDeleteVertexArrays(1, &(mesh.vertex_array)); - glDeleteBuffers(1, &(mesh.vertex_buffer)); - glDeleteBuffers(1, &(mesh.element_buffer)); +int honey_mesh_delete(lua_State* L) +{ + honey_mesh* mesh; + honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &mesh); + glDeleteVertexArrays(1, &(mesh->vertex_array)); + glDeleteBuffers(1, &(mesh->vertex_buffer)); + glDeleteBuffers(1, &(mesh->element_buffer)); } |