From 3ef1112d085a101aa55d11ff894f1fb515d29c27 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 27 Oct 2020 14:33:25 -0500 Subject: add mesh lua functions and refactor lua type names --- src/mesh/mesh.c | 152 +++++++++++++++++++++++++++++--------------------------- src/mesh/mesh.h | 38 ++++++-------- 2 files changed, 92 insertions(+), 98 deletions(-) (limited to 'src/mesh') 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; in_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; ivertex_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)); } diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h index 6392faa..a82db52 100644 --- a/src/mesh/mesh.h +++ b/src/mesh/mesh.h @@ -12,46 +12,36 @@ #include "../shader/shader.h" typedef struct { - float* vertices; - unsigned int n_vertices; - unsigned int* indices; - unsigned int n_indices; - unsigned int vertex_array, vertex_buffer, element_buffer; + unsigned int n_vertices, n_indices; + unsigned int vertex_array, vertex_buffer, element_buffer; } honey_mesh; /** @brief Create a new mesh from vertex and index arrays. * - * Note that this function creates copies of the vertex and index arrays, - * so you can deallocate those immediately. + * This function copies the data, so you can safely permit vertices and + * indices to be garbage collected after calling it. * - * @param[out] mesh Pointer to the destination honey_mesh struct - * @param[in] vertices Array of floats representing the vertices - * @param[in] n_attributes The number of attributes per vertex - * @param[in] attribute_sizes An array containing for each attribute how many floats it contains - * @param[in] n_vertices The number of vertices (NOT the number of floats in the vertex array) - * @param[in] indices Array of vertex indices - * @param[in] n_indices The number of elements in the index array + * @param[in] vertices Userdata array containing the vertices. + * @param[in] indices Userdata array defining the mesh faces. + * @param[in] attributes Lua table containing the attribute sizes. + * @param[in] n_vertices Integer number of vertices. + * @param[in] n_indices Integer number of indices. + * + * @returns Userdata containing the mesh's OpenGL handles. */ -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); +int honey_mesh_new(lua_State* L); /** @brief Draw a mesh on screen. * * @param[in] mesh The mesh to draw * @param[in] shader The shader to use when drawing the mesh */ -void honey_mesh_draw(honey_mesh mesh, - int shader); +int honey_mesh_draw(lua_State* L); /** @brief Delete a mesh. * * @param[in] mesh The mesh to delete */ -void honey_mesh_delete(honey_mesh mesh); +int honey_mesh_delete(lua_State* L); #endif -- cgit v1.2.1