From 040ba6826237eb124aaa4576fc302e2e07dd40c5 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Wed, 20 May 2020 20:51:05 -0500 Subject: add honey_mesh and assorted related functions --- .gitignore | 3 +- CMakeLists.txt | 12 +++++--- demo.c | 82 ++++++++++++--------------------------------------- demo.fs | 2 +- honey_engine_demo | Bin 222968 -> 447408 bytes include/common.h | 17 +++++++++++ include/honey.h | 17 +++++++++++ include/mesh.h | 59 +++++++++++++++++++++++++++++++++++++ include/shader.h | 2 +- src/honey_setup.c | 28 ++++++++++++++++++ src/mesh.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/primitives.c | 3 ++ src/shader.c | 2 -- 13 files changed, 239 insertions(+), 74 deletions(-) create mode 100644 include/common.h create mode 100644 include/honey.h create mode 100644 include/mesh.h create mode 100644 src/honey_setup.c create mode 100644 src/mesh.c create mode 100644 src/primitives.c 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 -#include -#include - -#include "include/stb_image.h" - -#include "include/glad.h" -#include - -#include -#include - -#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 Binary files a/honey_engine_demo and b/honey_engine_demo 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 +#include +#include +#include + +#include "glad.h" +#include + +#include +#include + +#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 +#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 -#include "include/glad.h" #include "include/shader.h" /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -- cgit v1.2.1