summaryrefslogtreecommitdiff
path: root/src/mesh
diff options
context:
space:
mode:
Diffstat (limited to 'src/mesh')
-rw-r--r--src/mesh/mesh.c86
-rw-r--r--src/mesh/mesh.h57
2 files changed, 143 insertions, 0 deletions
diff --git a/src/mesh/mesh.c b/src/mesh/mesh.c
new file mode 100644
index 0000000..231fcc9
--- /dev/null
+++ b/src/mesh/mesh.c
@@ -0,0 +1,86 @@
+#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;
+}
+
+/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
+
+void honey_mesh_draw(honey_mesh mesh, honey_shader shader) {
+ honey_shader_use(shader);
+
+ 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));
+}
diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h
new file mode 100644
index 0000000..a3e1e2a
--- /dev/null
+++ b/src/mesh/mesh.h
@@ -0,0 +1,57 @@
+#ifndef HONEY_MESH_H
+#define HONEY_MESH_H
+
+/** @file mesh.h
+ *
+ * @brief Defines the honey_mesh struct and related basic mesh functions.
+*/
+
+
+
+#include "../common.h"
+#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;
+} 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.
+ *
+ * @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
+ */
+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
+ */
+void honey_mesh_draw(honey_mesh mesh,
+ honey_shader shader);
+
+/** @brief Delete a mesh.
+ *
+ * @param[in] mesh The mesh to delete
+ */
+void honey_mesh_delete(honey_mesh mesh);
+
+#endif