From 7b3afcaf77f96e7d62f6cd1623ead7f17512d79f Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sat, 24 Feb 2024 22:47:46 +1100 Subject: repo init. partial port of existing code --- src/renderer/backends/backend_opengl.c | 62 ++++++++++++++++++++++++++++++++ src/renderer/backends/backend_test.c | 1 + src/renderer/backends/backend_vulkan.c | 1 + src/renderer/render.c | 42 ++++++++++++++++++++++ src/renderer/render.h | 16 +++++++++ src/renderer/render_backend.h | 15 ++++++++ src/renderer/render_types.h | 64 ++++++++++++++++++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 src/renderer/backends/backend_opengl.c create mode 100644 src/renderer/backends/backend_test.c create mode 100644 src/renderer/backends/backend_vulkan.c create mode 100644 src/renderer/render.c create mode 100644 src/renderer/render.h create mode 100644 src/renderer/render_backend.h create mode 100644 src/renderer/render_types.h (limited to 'src/renderer') diff --git a/src/renderer/backends/backend_opengl.c b/src/renderer/backends/backend_opengl.c new file mode 100644 index 0000000..ed2c70f --- /dev/null +++ b/src/renderer/backends/backend_opengl.c @@ -0,0 +1,62 @@ +#include +#define CEL_PLATFORM_LINUX + +#include "defines.h" +#include "log.h" +#include "maths_types.h" +#include "render_types.h" + +#if CEL_REND_BACKEND_OPENGL + +#include + +#include + +/** @brief Internal backend state */ +typedef struct opengl_state { +} opengl_state; + +bool gfx_backend_init(renderer *ren) { + INFO("loading OpenGL backend"); + + // glfwInit(); // Already handled in `renderer_init` + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + + // glad: load all OpenGL function pointers + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + ERROR("Failed to initialise GLAD \n"); + + return false; + } + + glEnable(GL_DEPTH_TEST); + + opengl_state *internal = malloc(sizeof(opengl_state)); + ren->backend_state = (void *)internal; + + return true; +} +void gfx_backend_shutdown(renderer *ren) {} + +void uniform_vec3f(u32 program_id, const char *uniform_name, vec3 *value) { + glUniform3fv(glGetUniformLocation(program_id, uniform_name), 1, &value->x); +} +void uniform_f32(u32 program_id, const char *uniform_name, f32 value) { + glUniform1f(glGetUniformLocation(program_id, uniform_name), value); +} +void uniform_i32(u32 program_id, const char *uniform_name, i32 value) { + glUniform1i(glGetUniformLocation(program_id, uniform_name), value); +} +void uniform_mat4f(u32 program_id, const char *uniform_name, mat4 *value) { + glUniformMatrix4fv(glGetUniformLocation(program_id, uniform_name), 1, GL_FALSE, value->data); +} + +void clear_screen(vec3 colour) { + glClearColor(colour.x, colour.y, colour.z, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +#endif \ No newline at end of file diff --git a/src/renderer/backends/backend_test.c b/src/renderer/backends/backend_test.c new file mode 100644 index 0000000..6347e27 --- /dev/null +++ b/src/renderer/backends/backend_test.c @@ -0,0 +1 @@ +// #FUTURE \ No newline at end of file diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c new file mode 100644 index 0000000..6347e27 --- /dev/null +++ b/src/renderer/backends/backend_vulkan.c @@ -0,0 +1 @@ +// #FUTURE \ No newline at end of file diff --git a/src/renderer/render.c b/src/renderer/render.c new file mode 100644 index 0000000..ce908d6 --- /dev/null +++ b/src/renderer/render.c @@ -0,0 +1,42 @@ +#include "render.h" + +#include + +#include "log.h" +#include "render_backend.h" + +bool renderer_init(renderer* ren) { + INFO("Renderer init"); + + // NOTE: all platforms use GLFW at the moment but thats subject to change + glfwInit(); + + // glfw window creation + GLFWwindow* window = glfwCreateWindow(ren->config.scr_width, ren->config.scr_height, + ren->config.window_name, NULL, NULL); + if (window == NULL) { + printf("Failed to create GLFW window\n"); + glfwTerminate(); + return false; + } + ren->window = window; + + glfwMakeContextCurrent(ren->window); + + if (!gfx_backend_init(ren)) { + FATAL("Couldnt load graphics api backend"); + return false; + } + + return true; +} + +void render_frame_begin(renderer* ren) { + vec3 color = ren->config.clear_colour; + clear_screen(color); +} +void render_frame_end(renderer* ren) { + // present frame + glfwSwapBuffers(ren->window); + glfwPollEvents(); +} \ No newline at end of file diff --git a/src/renderer/render.h b/src/renderer/render.h new file mode 100644 index 0000000..c89c364 --- /dev/null +++ b/src/renderer/render.h @@ -0,0 +1,16 @@ +#pragma once + +#include "render_types.h" + +// --- Lifecycle +/** @brief initialise the render system frontend */ +bool renderer_init(renderer* ren); +/** @brief shutdown the render system frontend */ +void renderer_shutdown(renderer* ren); + +// --- Frame + +void render_frame_begin(renderer* ren); +void render_frame_end(renderer* ren); + +// --- \ No newline at end of file diff --git a/src/renderer/render_backend.h b/src/renderer/render_backend.h new file mode 100644 index 0000000..61c7ab5 --- /dev/null +++ b/src/renderer/render_backend.h @@ -0,0 +1,15 @@ +/** + * @brief + */ +#pragma once + +#include "maths_types.h" +#include "render_types.h" + +/// --- Lifecycle +bool gfx_backend_init(renderer* ren); +void gfx_backend_shutdown(renderer* ren); + +void clear_screen(vec3 colour); + +// --- Uniforms diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h new file mode 100644 index 0000000..896a1a5 --- /dev/null +++ b/src/renderer/render_types.h @@ -0,0 +1,64 @@ +/** + * @file render_types.h + * @author Omniscient + * @brief Type definitions for the majority of data required by the renderer system + * @date 2024-02-24 + * + */ +#pragma once + +#include "darray.h" +#include "maths_types.h" + +struct GLFWwindow; + +/** @brief configuration passed to the renderer at init time */ +typedef struct renderer_config { + char window_name[256]; + u32 scr_width, scr_height; + vec3 clear_colour; /** colour that the screen gets cleared to every frame */ +} renderer_config; + +typedef struct renderer { + struct GLFWwindow *window; /** Currently all platforms use GLFW*/ + void *backend_state; /** Graphics API-specific state */ + renderer_config config; +} renderer; + +/** @brief Vertex format for a static mesh */ +typedef struct vertex { + vec3 position; + vec3 normal; + vec2 uv; +} vertex; + +#ifndef TYPED_VERTEX_ARRAY +KITC_DECL_TYPED_ARRAY(vertex) // creates "vertex_darray" +#define TYPED_VERTEX_ARRAY +#endif + +// --- Models & Meshes + +typedef struct mesh { + vertex_darray *vertices; + u32 vertex_size; /** size in bytes of each vertex including necessary padding */ + bool has_indices; + u32 *indices; + u32 indices_len; + size_t material_index; + u32 vbo, vao; /** OpenGL data */ +} mesh; + +#ifndef TYPED_MESH_ARRAY +KITC_DECL_TYPED_ARRAY(mesh) // creates "mesh_darray" +#define TYPED_MESH_ARRAY +#endif + +typedef struct model { + char name[256]; +} model; + +#ifndef TYPED_MODEL_ARRAY +KITC_DECL_TYPED_ARRAY(model) // creates "model_darray" +#define TYPED_MODEL_ARRAY +#endif \ No newline at end of file -- cgit v1.2.3-70-g09d2