summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-10-27 15:01:15 -0500
committersanine-a <sanine.not@pm.me>2020-10-27 15:01:15 -0500
commit50a8d3dc884816fbb1e3a3df3c401358e62b5eea (patch)
tree8513fa576be4905262f724d2be42bc59d40ee03e /src
parentfc16337c080db7013791bf25ace538546d2779dc (diff)
revert mesh.* with minor refactor and honey_setup_mesh()
Diffstat (limited to 'src')
-rw-r--r--src/mesh/mesh.c114
-rw-r--r--src/mesh/mesh.h40
-rw-r--r--src/primitives/primitives.c356
3 files changed, 257 insertions, 253 deletions
diff --git a/src/mesh/mesh.c b/src/mesh/mesh.c
index 1889bd7..8e7e444 100644
--- a/src/mesh/mesh.c
+++ b/src/mesh/mesh.c
@@ -1,106 +1,106 @@
#include "mesh.h"
+static int honey_mesh_lua_draw(lua_State* L)
+{
+ honey_mesh* mesh;
+ int shader;
+ honey_lua_parse_arguments(L, 2,
+ HONEY_USERDATA, &mesh,
+ HONEY_INTEGER, &shader);
+ honey_mesh_draw(*mesh, shader);
+ return 0;
+}
+
+static int honey_mesh_lua_delete(lua_State* L)
+{
+ honey_mesh* mesh;
+ honey_lua_parse_arguments(L, 1, HONEY_USERDATA, &mesh);
+ honey_mesh_delete(*mesh);
+ return 0;
+}
+
void honey_setup_mesh(lua_State* L)
{
honey_lua_element mesh_elements[] = {
- { "new", HONEY_FUNCTION, { .function = honey_mesh_new } },
- { "draw", HONEY_FUNCTION, { .function = honey_mesh_draw } },
- { "delete", HONEY_FUNCTION, { .function = honey_mesh_delete } },
+ { "draw", HONEY_FUNCTION, { .function = honey_mesh_lua_draw } },
+ { "delete", HONEY_FUNCTION, { .function = honey_mesh_lua_delete } },
};
- honey_lua_create_table(L, mesh_elements, 3);
+ honey_lua_create_table(L, mesh_elements, 2);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-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);
+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++) {
- 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;
+ (*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);
+ glGenVertexArrays(1, &((*mesh).vertex_array));
+ glGenBuffers(1, &((*mesh).vertex_buffer));
+ glGenBuffers(1, &((*mesh).element_buffer));
- glBindVertexArray(mesh->vertex_array);
+ glBindVertexArray((*mesh).vertex_array);
- glBindBuffer(GL_ARRAY_BUFFER, mesh->vertex_buffer);
+ 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);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (*mesh).element_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
n_indices * sizeof(unsigned int),
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)));
+ GL_FLOAT,
+ GL_FALSE,
+ vertex_size*sizeof(float),
+ (void*) (offset*sizeof(float)));
offset += attribute_sizes[i];
}
-
+
glBindVertexArray(0);
- free(attribute_sizes);
-
- return 1;
+ return HONEY_OK;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-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);
+void honey_mesh_draw(honey_mesh mesh, int shader) {
+ honey_shader_use(shader);
- glBindVertexArray(mesh->vertex_array);
- glDrawElements(GL_TRIANGLES, mesh->n_indices, GL_UNSIGNED_INT, 0);
+ glBindVertexArray(mesh.vertex_array);
+ glDrawElements(GL_TRIANGLES, mesh.n_indices, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-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));
+void honey_mesh_delete(honey_mesh 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 1f3bc3e..81bcc94 100644
--- a/src/mesh/mesh.h
+++ b/src/mesh/mesh.h
@@ -6,45 +6,49 @@
* @brief Defines the honey_mesh struct and related basic mesh functions.
*/
-
-
#include "../common.h"
-#include "../shader/shader.h"
typedef struct {
unsigned int n_vertices, n_indices;
- unsigned int vertex_array, vertex_buffer, element_buffer;
+ unsigned int vertex_array, vertex_buffer, element_buffer;
} honey_mesh;
-/** @brief Push the mesh bindings to the lua stack. */
-void honey_setup_mesh();
+/** @brief Lua bindings for mesh drawing and deletion functions. */
+void honey_setup_mesh(lua_State* L);
/** @brief Create a new mesh from vertex and index arrays.
*
- * This function copies the data, so you can safely permit vertices and
- * indices to be garbage collected after calling it.
- *
- * @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.
+ * Note that this function creates copies of the vertex and index arrays,
+ * so you can deallocate those immediately.
*
- * @returns Userdata containing the mesh's OpenGL handles.
+ * @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
*/
-int honey_mesh_new(lua_State* L);
+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);
/** @brief Draw a mesh on screen.
*
* @param[in] mesh The mesh to draw
* @param[in] shader The shader to use when drawing the mesh
*/
-int honey_mesh_draw(lua_State* L);
+void honey_mesh_draw(honey_mesh mesh,
+ int shader);
/** @brief Delete a mesh.
*
* @param[in] mesh The mesh to delete
*/
-int honey_mesh_delete(lua_State* L);
+void honey_mesh_delete(honey_mesh mesh);
#endif
diff --git a/src/primitives/primitives.c b/src/primitives/primitives.c
index 0d8f209..efc4a0f 100644
--- a/src/primitives/primitives.c
+++ b/src/primitives/primitives.c
@@ -1,180 +1,180 @@
#include "primitives.h"
-//honey_result honey_mesh_new_textured_plane(honey_mesh* mesh,
-// float width,
-// float height) {
-// float x0 = 0;
-// float y0 = 0;
-// float x1 = width;
-// float y1 = height;
-//
-// float vertices[] = {
-// /* position normal uv */
-// x0, y0, 0, 0, 0, 1, 0, 0,
-// x1, y0, 0, 0, 0, 1, 1, 0,
-// x0, y1, 0, 0, 0, 1, 0, 1,
-// x1, y1, 0, 0, 0, 1, 1, 1 };
-//
-// unsigned int indices[] = {
-// 0, 1, 2,
-// 1, 2, 3 };
-//
-// unsigned int attrib_sizes[] = { 3, 3, 2 };
-//
-// honey_result result = honey_mesh_new(mesh,
-// vertices, 4,
-// 3, attrib_sizes,
-// indices, 6);
-// return result;
-//}
-//
-///* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-//
-//honey_result honey_mesh_new_cube(honey_mesh* mesh,
-// float width,
-// float height,
-// float depth) {
-// float x0 = 0;
-// float y0 = 0;
-// float z0 = 0;
-//
-// float x1 = width;
-// float y1 = height;
-// float z1 = depth;
-//
-// float vertices[] = {
-// /* position normal tex coord */
-// /* back face */
-// x0, y0, z0, 0, 0, -1,
-// x1, y0, z0, 0, 0, -1,
-// x0, y1, z0, 0, 0, -1,
-// x1, y1, z0, 0, 0, -1,
-//
-// /* front face */
-// x0, y0, z1, 0, 0, 1,
-// x1, y0, z1, 0, 0, 1,
-// x0, y1, z1, 0, 0, 1,
-// x1, y1, z1, 0, 0, 1,
-//
-// /* left face */
-// x0, y0, z0, -1, 0, 0,
-// x0, y1, z0, -1, 0, 0,
-// x0, y0, z1, -1, 0, 0,
-// x0, y1, z1, -1, 0, 0,
-//
-// /* right face */
-// x1, y0, z0, 1, 0, 0,
-// x1, y1, z0, 1, 0, 0,
-// x1, y0, z1, 1, 0, 0,
-// x1, y1, z1, 1, 0, 0,
-//
-// /* bottom face */
-// x0, y0, z0, 0, -1, 0,
-// x1, y0, z0, 0, -1, 0,
-// x0, y0, z1, 0, -1, 0,
-// x1, y0, z1, 0, -1, 0,
-//
-// /* top face */
-// x0, y1, z0, 0, 1, 0,
-// x1, y1, z0, 0, 1, 0,
-// x0, y1, z1, 0, 1, 0,
-// x1, y1, z1, 0, 1, 0 };
-//
-// unsigned int indices[] = {
-// 0, 1, 2,
-// 1, 2, 3,
-// 4, 5, 6,
-// 5, 6, 7,
-// 8, 9, 10,
-// 9, 10, 11,
-// 12, 13, 14,
-// 13, 14, 15,
-// 16, 17, 18,
-// 17, 18, 19,
-// 20, 21, 22,
-// 21, 22, 23 };
-//
-// unsigned int attrib_sizes[] = { 3, 3 };
-//
-// honey_result result = honey_mesh_new(mesh,
-// vertices, 24,
-// 2, attrib_sizes,
-// indices, 36);
-//
-// return result;
-//}
-//
-///* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
-//
-//honey_result honey_mesh_new_textured_cube(honey_mesh* mesh,
-// float width,
-// float height,
-// float depth) {
-// float x0 = 0;
-// float y0 = 0;
-// float z0 = 0;
-//
-// float x1 = width;
-// float y1 = height;
-// float z1 = depth;
-//
-// float vertices[] = {
-// /* position normal tex coord */
-// /* back face */
-// x0, y0, z0, 0, 0, -1, 0, 0,
-// x1, y0, z0, 0, 0, -1, 1, 0,
-// x0, y1, z0, 0, 0, -1, 0, 1,
-// x1, y1, z0, 0, 0, -1, 1, 1,
-//
-// /* front face */
-// x0, y0, z1, 0, 0, 1, 0, 0,
-// x1, y0, z1, 0, 0, 1, 1, 0,
-// x0, y1, z1, 0, 0, 1, 0, 1,
-// x1, y1, z1, 0, 0, 1, 1, 1,
-//
-// /* left face */
-// x0, y0, z0, -1, 0, 0, 0, 0,
-// x0, y1, z0, -1, 0, 0, 1, 0,
-// x0, y0, z1, -1, 0, 0, 0, 1,
-// x0, y1, z1, -1, 0, 0, 1, 1,
-//
-// /* right face */
-// x1, y0, z0, 1, 0, 0, 0, 0,
-// x1, y1, z0, 1, 0, 0, 1, 0,
-// x1, y0, z1, 1, 0, 0, 0, 1,
-// x1, y1, z1, 1, 0, 0, 1, 1,
-//
-// /* bottom face */
-// x0, y0, z0, 0, -1, 0, 0, 0,
-// x1, y0, z0, 0, -1, 0, 1, 0,
-// x0, y0, z1, 0, -1, 0, 0, 1,
-// x1, y0, z1, 0, -1, 0, 1, 1,
-//
-// /* top face */
-// x0, y1, z0, 0, 1, 0, 0, 0,
-// x1, y1, z0, 0, 1, 0, 1, 0,
-// x0, y1, z1, 0, 1, 0, 0, 1,
-// x1, y1, z1, 0, 1, 0, 1, 1 };
-//
-// unsigned int indices[] = {
-// 0, 1, 2,
-// 1, 2, 3,
-// 4, 5, 6,
-// 5, 6, 7,
-// 8, 9, 10,
-// 9, 10, 11,
-// 12, 13, 14,
-// 13, 14, 15,
-// 16, 17, 18,
-// 17, 18, 19,
-// 20, 21, 22,
-// 21, 22, 23 };
-//
-// unsigned int attrib_sizes[] = { 3, 3, 2 };
-//
-// honey_result result;
-// result = honey_mesh_new(mesh, vertices, 24,
-// 3, attrib_sizes,
-// indices, 36);
-// return result;
-//}
+honey_result honey_mesh_new_textured_plane(honey_mesh* mesh,
+ float width,
+ float height) {
+ float x0 = 0;
+ float y0 = 0;
+ float x1 = width;
+ float y1 = height;
+
+ float vertices[] = {
+ /* position normal uv */
+ x0, y0, 0, 0, 0, 1, 0, 0,
+ x1, y0, 0, 0, 0, 1, 1, 0,
+ x0, y1, 0, 0, 0, 1, 0, 1,
+ x1, y1, 0, 0, 0, 1, 1, 1 };
+
+ unsigned int indices[] = {
+ 0, 1, 2,
+ 1, 2, 3 };
+
+ unsigned int attrib_sizes[] = { 3, 3, 2 };
+
+ honey_result result = honey_mesh_new(mesh,
+ vertices, 4,
+ 3, attrib_sizes,
+ indices, 6);
+ return result;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+honey_result honey_mesh_new_cube(honey_mesh* mesh,
+ float width,
+ float height,
+ float depth) {
+ float x0 = 0;
+ float y0 = 0;
+ float z0 = 0;
+
+ float x1 = width;
+ float y1 = height;
+ float z1 = depth;
+
+ float vertices[] = {
+ /* position normal tex coord */
+ /* back face */
+ x0, y0, z0, 0, 0, -1,
+ x1, y0, z0, 0, 0, -1,
+ x0, y1, z0, 0, 0, -1,
+ x1, y1, z0, 0, 0, -1,
+
+ /* front face */
+ x0, y0, z1, 0, 0, 1,
+ x1, y0, z1, 0, 0, 1,
+ x0, y1, z1, 0, 0, 1,
+ x1, y1, z1, 0, 0, 1,
+
+ /* left face */
+ x0, y0, z0, -1, 0, 0,
+ x0, y1, z0, -1, 0, 0,
+ x0, y0, z1, -1, 0, 0,
+ x0, y1, z1, -1, 0, 0,
+
+ /* right face */
+ x1, y0, z0, 1, 0, 0,
+ x1, y1, z0, 1, 0, 0,
+ x1, y0, z1, 1, 0, 0,
+ x1, y1, z1, 1, 0, 0,
+
+ /* bottom face */
+ x0, y0, z0, 0, -1, 0,
+ x1, y0, z0, 0, -1, 0,
+ x0, y0, z1, 0, -1, 0,
+ x1, y0, z1, 0, -1, 0,
+
+ /* top face */
+ x0, y1, z0, 0, 1, 0,
+ x1, y1, z0, 0, 1, 0,
+ x0, y1, z1, 0, 1, 0,
+ x1, y1, z1, 0, 1, 0 };
+
+ unsigned int indices[] = {
+ 0, 1, 2,
+ 1, 2, 3,
+ 4, 5, 6,
+ 5, 6, 7,
+ 8, 9, 10,
+ 9, 10, 11,
+ 12, 13, 14,
+ 13, 14, 15,
+ 16, 17, 18,
+ 17, 18, 19,
+ 20, 21, 22,
+ 21, 22, 23 };
+
+ unsigned int attrib_sizes[] = { 3, 3 };
+
+ honey_result result = honey_mesh_new(mesh,
+ vertices, 24,
+ 2, attrib_sizes,
+ indices, 36);
+
+ return result;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+honey_result honey_mesh_new_textured_cube(honey_mesh* mesh,
+ float width,
+ float height,
+ float depth) {
+ float x0 = 0;
+ float y0 = 0;
+ float z0 = 0;
+
+ float x1 = width;
+ float y1 = height;
+ float z1 = depth;
+
+ float vertices[] = {
+ /* position normal tex coord */
+ /* back face */
+ x0, y0, z0, 0, 0, -1, 0, 0,
+ x1, y0, z0, 0, 0, -1, 1, 0,
+ x0, y1, z0, 0, 0, -1, 0, 1,
+ x1, y1, z0, 0, 0, -1, 1, 1,
+
+ /* front face */
+ x0, y0, z1, 0, 0, 1, 0, 0,
+ x1, y0, z1, 0, 0, 1, 1, 0,
+ x0, y1, z1, 0, 0, 1, 0, 1,
+ x1, y1, z1, 0, 0, 1, 1, 1,
+
+ /* left face */
+ x0, y0, z0, -1, 0, 0, 0, 0,
+ x0, y1, z0, -1, 0, 0, 1, 0,
+ x0, y0, z1, -1, 0, 0, 0, 1,
+ x0, y1, z1, -1, 0, 0, 1, 1,
+
+ /* right face */
+ x1, y0, z0, 1, 0, 0, 0, 0,
+ x1, y1, z0, 1, 0, 0, 1, 0,
+ x1, y0, z1, 1, 0, 0, 0, 1,
+ x1, y1, z1, 1, 0, 0, 1, 1,
+
+ /* bottom face */
+ x0, y0, z0, 0, -1, 0, 0, 0,
+ x1, y0, z0, 0, -1, 0, 1, 0,
+ x0, y0, z1, 0, -1, 0, 0, 1,
+ x1, y0, z1, 0, -1, 0, 1, 1,
+
+ /* top face */
+ x0, y1, z0, 0, 1, 0, 0, 0,
+ x1, y1, z0, 0, 1, 0, 1, 0,
+ x0, y1, z1, 0, 1, 0, 0, 1,
+ x1, y1, z1, 0, 1, 0, 1, 1 };
+
+ unsigned int indices[] = {
+ 0, 1, 2,
+ 1, 2, 3,
+ 4, 5, 6,
+ 5, 6, 7,
+ 8, 9, 10,
+ 9, 10, 11,
+ 12, 13, 14,
+ 13, 14, 15,
+ 16, 17, 18,
+ 17, 18, 19,
+ 20, 21, 22,
+ 21, 22, 23 };
+
+ unsigned int attrib_sizes[] = { 3, 3, 2 };
+
+ honey_result result;
+ result = honey_mesh_new(mesh, vertices, 24,
+ 3, attrib_sizes,
+ indices, 36);
+ return result;
+}