summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--CMakeLists.txt12
-rw-r--r--demo.c82
-rw-r--r--demo.fs2
-rwxr-xr-xhoney_engine_demobin222968 -> 447408 bytes
-rw-r--r--include/common.h17
-rw-r--r--include/honey.h17
-rw-r--r--include/mesh.h59
-rw-r--r--include/shader.h2
-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
13 files changed, 239 insertions, 74 deletions
diff --git a/.gitignore b/.gitignore
index 446a8d7..4339f88 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,7 +3,6 @@
CMakeCache.txt
CMakeFiles/*
Makefile
-
cmake_install.cmake
-
doc/html
+*#
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d566887..9ae826f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,15 +1,19 @@
cmake_minimum_required(VERSION 3.2)
project(honey_engine_demo)
+#set(CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic")
+
find_package(OpenGL REQUIRED)
add_executable(honey_engine_demo demo.c)
-add_library(honey_shader "src/shader.c")
-add_library(glad "src/glad.c")
-add_library(stb_image "src/stb_image.c")
+set(CMAKE_C_FLAGS "-g")
+
+add_library(honey src/honey_setup.c src/shader.c src/mesh.c)
+add_library(glad src/glad.c)
+add_library(stb_image src/stb_image.c)
-set(LIBRARIES honey_shader glfw GL dl m glad stb_image)
+set(LIBRARIES honey glfw GL dl m glad stb_image)
target_link_libraries(honey_engine_demo ${LIBRARIES})
diff --git a/demo.c b/demo.c
index d079a58..26e54e0 100644
--- a/demo.c
+++ b/demo.c
@@ -1,16 +1,4 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
-
-#include "include/stb_image.h"
-
-#include "include/glad.h"
-#include <GLFW/glfw3.h>
-
-#include <cglm/cglm.h>
-#include <cglm/call.h>
-
-#include "include/shader.h"
+#include "include/honey.h"
unsigned int screen_width = 640;
unsigned int screen_height = 480;
@@ -111,27 +99,10 @@ void processInput(GLFWwindow* window, float dt) {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
int main() {
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
-
- GLFWwindow* window = glfwCreateWindow(screen_width, screen_height, "hello, world!", NULL, NULL);
- if (window == NULL) {
- fprintf(stderr, "ERROR: failed to create GLFW window!\n");
- glfwTerminate();
- return 1;
- }
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, framebufferResizeCallback);
- glfwSetCursorPosCallback(window, mouseCallback);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
-
- if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
- fprintf(stderr, "ERROR: failed to initialize GLAD!\n");
- glfwTerminate();
- return 2;
- }
+ honey_window window = honey_setup(screen_width, screen_height, "hello, world!");
+
+ honey_set_resize_callback(window, framebufferResizeCallback);
+ honey_set_mouse_move_callback(window, mouseCallback);
/* load box texture */
unsigned int boxTex;
@@ -202,29 +173,16 @@ int main() {
0, 1, 4,
1, 4, 5 };
- unsigned int vertexBufferObject, vertexArrayObject, elementBufferObject;
- glGenVertexArrays(1, &vertexArrayObject);
- glGenBuffers(1, &vertexBufferObject);
- glGenBuffers(1, &elementBufferObject);
- glBindVertexArray(vertexArrayObject);
-
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
-
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)0);
- glEnableVertexAttribArray(0);
-
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(3*sizeof(float)));
- glEnableVertexAttribArray(1);
-
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(6*sizeof(float)));
- glEnableVertexAttribArray(2);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
+ honey_mesh cube;
+ unsigned int attribute_sizes[] = { 3, 3, 2 }; /* position, color, texture coordinate */
+ enum honey_mesh_result result = honey_mesh_new(&cube,
+ vertices, 8, 3, attribute_sizes,
+ indices,
+ sizeof(indices)/sizeof(unsigned int));
+ if (result != MESH_OK) {
+ fprintf(stderr, "Failed to load cube\n");
+ return 1;
+ }
honey_shader_set_int(shader, "boxTexture", 0);
honey_shader_set_int(shader, "happyTexture", 1);
@@ -283,20 +241,16 @@ int main() {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, happyTex);
- honey_shader_use(shader);
- glBindVertexArray(vertexArrayObject);
- glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(unsigned int), GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
+ honey_mesh_draw(cube, shader);
glfwSwapBuffers(window);
glfwPollEvents();
}
- glDeleteVertexArrays(1, &vertexArrayObject);
- glDeleteBuffers(1, &vertexArrayObject);
+ honey_mesh_delete(cube);
honey_shader_delete(shader);
- glfwTerminate();
+ honey_quit();
return 0;
}
diff --git a/demo.fs b/demo.fs
index f1c8670..ebdf3f8 100644
--- a/demo.fs
+++ b/demo.fs
@@ -10,5 +10,5 @@ uniform sampler2D happyTexture;
void main()
{
- FragColor = mix(texture(boxTexture, outTexCoord), texture(happyTexture, outTexCoord), 0.2);
+ FragColor = vec4(outColor.xyz, 1.0); //mix(texture(boxTexture, outTexCoord), texture(happyTexture, outTexCoord), 0.2);
}
diff --git a/honey_engine_demo b/honey_engine_demo
index 4ac858a..6e32448 100755
--- a/honey_engine_demo
+++ b/honey_engine_demo
Binary files differ
diff --git a/include/common.h b/include/common.h
new file mode 100644
index 0000000..8c1f976
--- /dev/null
+++ b/include/common.h
@@ -0,0 +1,17 @@
+#ifndef HONEY_COMMON_H
+#define HONEY_COMMON_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+
+#include "glad.h"
+#include <GLFW/glfw3.h>
+
+#include <cglm/cglm.h>
+#include <cglm/call.h>
+
+#include "stb_image.h"
+
+#endif
diff --git a/include/honey.h b/include/honey.h
new file mode 100644
index 0000000..251881e
--- /dev/null
+++ b/include/honey.h
@@ -0,0 +1,17 @@
+#ifndef HONEY_ENGINE_H
+#define HONEY_ENGINE_H
+
+#include "common.h"
+#include "mesh.h"
+#include "shader.h"
+
+typedef GLFWwindow* honey_window;
+
+honey_window honey_setup(int screen_width, int screen_height, char* window_title);
+
+#define honey_set_resize_callback glfwSetFramebufferSizeCallback
+#define honey_set_mouse_move_callback glfwSetCursorPosCallback
+
+#define honey_quit glfwTerminate
+
+#endif
diff --git a/include/mesh.h b/include/mesh.h
new file mode 100644
index 0000000..2040f44
--- /dev/null
+++ b/include/mesh.h
@@ -0,0 +1,59 @@
+/** @brief Defines the honey_mesh struct and related basic mesh functions. */
+
+#ifndef HONEY_MESH_H
+#define HONEY_MESH_H
+
+#include "common.h"
+#include "shader.h"
+
+enum honey_mesh_result {
+ MESH_OK,
+ MEMORY_ERROR,
+ BAD_VERTEX_DATA,
+ BAD_INDEX_DATA,
+ N_MESH_RESULTS };
+
+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
+ */
+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);
+
+/** @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
diff --git a/include/shader.h b/include/shader.h
index d43015d..bfdb090 100644
--- a/include/shader.h
+++ b/include/shader.h
@@ -6,7 +6,7 @@
#ifndef HONEY_SHADER_H
#define HONEY_SHADER_H
-#include <cglm/cglm.h>
+#include "common.h"
enum honey_shader_result {
SHADER_OK,
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"
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */