summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2020-05-20 20:51:05 -0500
committersanine-a <sanine.not@pm.me>2020-05-20 20:51:05 -0500
commit040ba6826237eb124aaa4576fc302e2e07dd40c5 (patch)
treec35f1c3c4a63f307ac8a0f6beac346e3d08b6b7d /src
parent447c3de585cca51013b17017d968e3aee53f5c87 (diff)
add honey_mesh and assorted related functions
Diffstat (limited to 'src')
-rw-r--r--src/honey_setup.c28
-rw-r--r--src/mesh.c86
-rw-r--r--src/primitives.c3
-rw-r--r--src/shader.c2
4 files changed, 117 insertions, 2 deletions
diff --git a/src/honey_setup.c b/src/honey_setup.c
new file mode 100644
index 0000000..247c325
--- /dev/null
+++ b/src/honey_setup.c
@@ -0,0 +1,28 @@
+#include "include/honey.h"
+
+honey_window honey_setup(int screen_width, int screen_height, char* window_title) {
+ glfwInit();
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
+ glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
+ glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
+
+ honey_window window = glfwCreateWindow(screen_width, screen_height, window_title, NULL, NULL);
+ if (window == NULL) {
+ fprintf(stderr, "ERROR: failed to create window!\n");
+ glfwTerminate();
+ return NULL;
+ }
+
+ glfwMakeContextCurrent(window);
+ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
+
+ if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
+ fprintf(stderr, "ERROR: failed to initialize GLAD!\n");
+ glfwTerminate();
+ return NULL;
+ }
+
+ return window;
+}
+
+
diff --git a/src/mesh.c b/src/mesh.c
new file mode 100644
index 0000000..1b0fb1a
--- /dev/null
+++ b/src/mesh.c
@@ -0,0 +1,86 @@
+#include "include/mesh.h"
+
+enum honey_mesh_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 BAD_VERTEX_DATA;
+ }
+ if (indices == NULL || n_indices == 0) {
+ return 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 MEMORY_ERROR;
+ }
+ memcpy((*mesh).vertices, vertices, vertex_size*n_vertices*sizeof(float));
+
+ (*mesh).indices = malloc(n_indices * sizeof(unsigned int));
+ if ((*mesh).indices == NULL) {
+ return MEMORY_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 MESH_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/primitives.c b/src/primitives.c
new file mode 100644
index 0000000..798a691
--- /dev/null
+++ b/src/primitives.c
@@ -0,0 +1,3 @@
+#ifndef HONEY_PRIMITIVE_OBJECTS
+#define HONEY_PRIMITIVE_OBJECTS
+
diff --git a/src/shader.c b/src/shader.c
index 496b9f9..207ce30 100644
--- a/src/shader.c
+++ b/src/shader.c
@@ -1,5 +1,3 @@
-#include <stdio.h>
-#include "include/glad.h"
#include "include/shader.h"
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */