diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-05-10 13:24:05 +1000 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-05-10 13:24:05 +1000 |
commit | f7944239b793d1d5c49336856965d3a793f99316 (patch) | |
tree | 2effd38a16a15aee505eb8c27a231dfbe35c822a | |
parent | 3a0557d98ba311b031ad53ceb8fc6025013f65dc (diff) |
make core a static and add a default scene to it
-rw-r--r-- | examples/triangle/ex_triangle.c | 14 | ||||
-rw-r--r-- | src/core.c | 35 | ||||
-rw-r--r-- | src/core.h | 16 | ||||
-rw-r--r-- | src/maths/maths_types.h | 3 | ||||
-rw-r--r-- | src/renderer/archive/render_types.h | 30 | ||||
-rw-r--r-- | src/renderer/render_types.h | 33 | ||||
-rw-r--r-- | src/scene.c | 56 | ||||
-rw-r--r-- | src/scene.h | 42 |
8 files changed, 165 insertions, 64 deletions
diff --git a/examples/triangle/ex_triangle.c b/examples/triangle/ex_triangle.c index 3a9b7db..97d6484 100644 --- a/examples/triangle/ex_triangle.c +++ b/examples/triangle/ex_triangle.c @@ -12,8 +12,10 @@ // Example setting up a renderer +extern core g_core; + int main() { - core* core = core_bringup(); + core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); gpu_renderpass_desc pass_description = {}; @@ -44,11 +46,11 @@ int main() { gpu_pipeline* gfx_pipeline = gpu_graphics_pipeline_create(pipeline_description); // Main loop - while (!should_exit(core)) { + while (!should_exit(&g_core)) { glfwPollEvents(); - input_update(&core->input); + input_update(&g_core.input); - render_frame_begin(&core->renderer); + render_frame_begin(&g_core.renderer); static f64 x = 0.0; x += 0.01; @@ -74,11 +76,11 @@ int main() { // Submit gpu_backend_end_frame(); - render_frame_end(&core->renderer); + render_frame_end(&g_core.renderer); // glfwSwapBuffers(core->renderer.window); } - renderer_shutdown(&core->renderer); + renderer_shutdown(&g_core.renderer); return 0; } @@ -13,25 +13,28 @@ #define SCR_WIDTH 1000 #define SCR_HEIGHT 1000 -core* core_bringup() { +core g_core; /** @brief global `core` that other files can use */ + +inline core* get_global_core() { return &g_core; } + +void core_bringup() { INFO("Initiate Core bringup"); - core* c = malloc(sizeof(core)); renderer_config conf = { .window_name = { "Celeritas Engine Core" }, .scr_width = SCR_WIDTH, .scr_height = SCR_HEIGHT, .clear_colour = (vec3){ .08, .08, .1 } }; - c->renderer.config = conf; - c->renderer.backend_context = NULL; + g_core.renderer.config = conf; + g_core.renderer.backend_context = NULL; // threadpool_create(&c->threadpool, 6, 256); // threadpool_set_ctx(&c->threadpool, c); // Gives the threadpool access to the core // initialise all subsystems - if (!renderer_init(&c->renderer)) { + if (!renderer_init(&g_core.renderer)) { // FATAL("Failed to start renderer"); ERROR_EXIT("Failed to start renderer\n"); } - if (!input_system_init(&c->input, c->renderer.window)) { + if (!input_system_init(&g_core.input, g_core.renderer.window)) { // the input system needs the glfw window which is created by the renderer // hence the order here is important FATAL("Failed to start input system"); @@ -48,26 +51,22 @@ core* core_bringup() { } */ - c->models = model_darray_new(10); - - return c; + g_core.models = model_darray_new(10); } #include <glfw3.h> -#include "input.h" -#include "render.h" bool should_window_close(core* core) { glfwWindowShouldClose(core->renderer.window); } -void core_input_update(core* core) { input_update(&core->input); } +void core_input_update() { input_update(&g_core.input); } void core_frame_begin(core* core) { render_frame_begin(&core->renderer); } void core_frame_end(core* core) { render_frame_end(&core->renderer); } -void core_shutdown(core* core) { +void core_shutdown() { // threadpool_destroy(&core->threadpool); - input_system_shutdown(&core->input); - renderer_shutdown(&core->renderer); + input_system_shutdown(&g_core.input); + renderer_shutdown(&g_core.renderer); } -bool should_exit(core* core) { - return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(core->renderer.window); -}
\ No newline at end of file +bool should_exit() { + return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(g_core.renderer.window); +} @@ -1,9 +1,8 @@ #pragma once -#include "defines.h" #include "input.h" -#include "ral.h" #include "screenspace.h" +#include "scene.h" #include "terrain.h" #include "text.h" // #include "threadpool.h" @@ -19,12 +18,17 @@ typedef struct core { terrain_state terrain; screenspace_state screenspace; // data storage + scene default_scene; model_darray* models; } core; +core* get_global_core(); + // --- Lifecycle -core* core_bringup(); -void core_shutdown(core* core); -bool should_exit(core* core); -void core_input_update(core* core); +/** @brief Throws error if the core cannot be instantiated */ +void core_bringup(); +void core_shutdown(); +bool should_exit(); + +void core_input_update(); diff --git a/src/maths/maths_types.h b/src/maths/maths_types.h index aa86eb0..5ef09db 100644 --- a/src/maths/maths_types.h +++ b/src/maths/maths_types.h @@ -61,6 +61,7 @@ typedef struct transform { f32 scale; bool is_dirty; } transform; +typedef transform transform3d; typedef struct vec4i { i32 x, y, z, w; @@ -99,4 +100,4 @@ typedef struct vec2 f32x2; #define f32x2(x, y) ((f32x2){ x, y }) typedef struct vec3 f32x3; -#define f32x3(x, y, z) ((f32x3){ x, y, z })
\ No newline at end of file +#define f32x3(x, y, z) ((f32x3){ x, y, z }) diff --git a/src/renderer/archive/render_types.h b/src/renderer/archive/render_types.h index 13a6651..f5ea986 100644 --- a/src/renderer/archive/render_types.h +++ b/src/renderer/archive/render_types.h @@ -94,20 +94,20 @@ KITC_DECL_TYPED_ARRAY(animation_clip) // creates "material_darray" #endif // // lights -// typedef struct point_light { -// vec3 position; -// f32 constant, linear, quadratic; -// vec3 ambient; -// vec3 diffuse; -// vec3 specular; -// } point_light; - -// typedef struct directional_light { -// vec3 direction; -// vec3 ambient; -// vec3 diffuse; -// vec3 specular; -// } directional_light; +typedef struct point_light { + vec3 position; + f32 constant, linear, quadratic; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} point_light; + +typedef struct directional_light { + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} directional_light; // void point_light_upload_uniforms(shader shader, point_light *light, char index); // void dir_light_upload_uniforms(shader shader, directional_light *light); @@ -207,4 +207,4 @@ typedef struct model { // // typedef enum pipeline_kind { // // GRAPHICS, // // COMPUTE, -// // } pipeline_kind;
\ No newline at end of file +// // } pipeline_kind; diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 08b9e94..3763967 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -116,10 +116,31 @@ KITC_DECL_TYPED_ARRAY(animation_clip) /** @brief Describes all the data required for the renderer to start executing draws */ typedef struct render_entity { - buffer_handle index_buffer; - u32 index_count; - u32 index_offset; - buffer_handle vertex_buffer; - material* material; + /* buffer_handle index_buffer; */ + /* u32 index_count; */ + /* u32 index_offset; */ + /* buffer_handle vertex_buffer; */ + model_handle model; transform tf; -} render_entity;
\ No newline at end of file +} render_entity; + +#ifndef TYPED_RENDER_ENTITY_ARRAY +KITC_DECL_TYPED_ARRAY(render_entity) +#define TYPED_RENDER_ENTITY_ARRAY +#endif + +// --- Lights +typedef struct point_light { + vec3 position; + f32 constant, linear, quadratic; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} point_light; + +typedef struct directional_light { + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} directional_light; diff --git a/src/scene.c b/src/scene.c new file mode 100644 index 0000000..d9fea05 --- /dev/null +++ b/src/scene.c @@ -0,0 +1,56 @@ +#include "scene.h" +#include "core.h" +#include "log.h" +#include "maths.h" +#include "render_types.h" + +extern core g_core; + +void scene_init(scene *s) { + memset(s, 0, sizeof(scene)); + s->renderables = render_entity_darray_new(10); + // default camera position - moved slightly along Z axis looking at 0,0,0 + vec3 cam_pos = vec3_create(0, 0, -5); + s->camera = camera_create(cam_pos, vec3_negate(cam_pos), VEC3_Y, deg_to_rad(45.0)); +} +void scene_free(scene *s) { render_entity_darray_free(s->renderables); } + +void scene_set_dir_light(directional_light light) { g_core.default_scene.dir_light = light; } +void scene_add_point_light(point_light light) { + scene s = g_core.default_scene; + if (s.point_lights_count == 4) { + WARN("Already 4 point lights, we can't add more."); + } else { + s.point_lights[s.point_lights_count] = light; + s.point_lights_count++; + } +} +void scene_add_model(model_handle model, transform3d transform) { + render_entity renderable = { .model = model, .tf = transform }; + render_entity_darray_push(g_core.default_scene.renderables, renderable); +} + +bool scene_remove_model(model_handle model) { + scene s = g_core.default_scene; + for (u32 i = 0; i <= s.renderables->len; i++) { + if (s.renderables->data[i].model.raw == model.raw) { + // TODO: add remove function to darray + } + } + return true; +} + +void scene_set_model_transform(model_handle model, transform3d new_transform) { + scene s = g_core.default_scene; + for (u32 i = 0; i <= s.renderables->len; i++) { + if (s.renderables->data[i].model.raw == model.raw) { + s.renderables->data[i].tf = new_transform; + } + } +} + +void scene_set_camera(vec3 pos, vec3 front) { + scene s = g_core.default_scene; + s.camera.position = pos; + s.camera.front = front; +} diff --git a/src/scene.h b/src/scene.h index 6cac061..5399ab7 100644 --- a/src/scene.h +++ b/src/scene.h @@ -8,23 +8,41 @@ * @copyright Copyright (c) 2024 * */ +#pragma once +#include "camera.h" #include "defines.h" -#include "types.h" +#include "render_types.h" +#include "maths_types.h" typedef struct scene { - // directional_light dir_light; - // point_light point_lights[4]; - // size_t n_point_lights; + // camera + camera camera; + // lights + directional_light dir_light; + point_light point_lights[4]; + size_t point_lights_count; + // geometry + render_entity_darray* renderables; + // TODO: tree - transform_hierarchy } scene; -bool scene_add_directional_light(scene* s /* TODO */); -bool scene_add_point_light(scene* s /* TODO */); +void scene_init(scene* s); +void scene_free(scene* s); -// There can only be one heightmap terrain at a time right now. -bool scene_add_heightmap(scene* s /* TODO */); -bool scene_delete_heightmap(scene* s); +// Simplified API - no scene pointer; gets and sets global scene -bool scene_add_model(scene* s, model_handle model); -void scene_remove_model(scene* s, model_handle model); +// Add/Remove objects from the scene +void scene_set_dir_light(directional_light light); +void scene_add_point_light(point_light light); +void scene_add_model(model_handle model, transform3d transform); +bool scene_remove_model(model_handle model); -// TODO: functions to load and save scenes from disk
\ No newline at end of file +// Getter & Setters +void scene_set_model_transform(model_handle model, transform3d new_transform); +void scene_set_camera(vec3 pos, vec3 front); + +/* // There can only be one heightmap terrain at a time right now. */ +/* bool scene_add_heightmap(scene* s /\* TODO *\/); */ +/* bool scene_delete_heightmap(scene* s); */ + +// TODO: functions to load and save scenes from disk |