From cbcd40391c445afb836217a64e0bd96bc54cb805 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 17 Mar 2024 15:39:40 +1100 Subject: rendering using transforms --- src/renderer/render.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/renderer/render.h') diff --git a/src/renderer/render.h b/src/renderer/render.h index 10702e3..61dda75 100644 --- a/src/renderer/render.h +++ b/src/renderer/render.h @@ -17,8 +17,8 @@ void render_frame_end(renderer* ren); // --- models meshes void model_upload_meshes(renderer* ren, model* model); -void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene* scene); -void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* view, mat4* proj); +void draw_model(renderer* ren, camera* camera, model* model, mat4* tf, scene* scene); +void draw_mesh(renderer* ren, mesh* mesh, mat4* tf, material* mat, mat4* view, mat4* proj); // --- texture texture_data_load(const char* path, bool invert_y); // #frontend -- cgit v1.2.3-70-g09d2 From 9ef22d2af0001bbc402454c8d605339ec6a428ae Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 17 Mar 2024 17:03:33 +1100 Subject: draw_scene that uses transform hierarchy to draw all models --- examples/transforms/ex_transforms.c | 21 +++++++++++++-------- src/renderer/render.c | 27 +++++++++++++++++++++++++++ src/renderer/render.h | 2 ++ src/transform_hierarchy.c | 9 +++++---- 4 files changed, 47 insertions(+), 12 deletions(-) (limited to 'src/renderer/render.h') diff --git a/examples/transforms/ex_transforms.c b/examples/transforms/ex_transforms.c index a3bede7..0f1752d 100644 --- a/examples/transforms/ex_transforms.c +++ b/examples/transforms/ex_transforms.c @@ -1,10 +1,11 @@ #include #include "core.h" +#include "maths.h" +#include "maths_types.h" +#include "mem.h" #include "render.h" #include "render_types.h" -#include "maths_types.h" -#include "maths.h" #include "transform_hierarchy.h" const vec3 pointlight_positions[4] = { @@ -23,8 +24,7 @@ int main() { vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); - model_handle cube_handle = - model_load_obj(core, "assets/models/obj/cube/cube.obj", true); + model_handle cube_handle = model_load_obj(core, "assets/models/obj/cube/cube.obj", true); model* cube = &core->models->data[cube_handle.raw]; model_upload_meshes(&core->renderer, cube); @@ -62,6 +62,9 @@ int main() { transform_hierarchy_debug_print(root_node, core); + char* frame_allocator_storage = malloc(1024 * 1024 * 64); + arena frame_arena = arena_create(frame_allocator_storage, 1024 * 1024 * 64); + // Main loop while (!glfwWindowShouldClose(core->renderer.window)) { input_update(&core->input); @@ -72,12 +75,14 @@ int main() { node1->tf.position.x += 0.002; node1->tf.is_dirty = true; - draw_model(&core->renderer, &cam, cube, &node1->world_matrix_tf, &our_scene); - draw_model(&core->renderer, &cam, cube, &node2->world_matrix_tf, &our_scene); - draw_model(&core->renderer, &cam, cube, &node3->world_matrix_tf, &our_scene); - draw_model(&core->renderer, &cam, cube, &node4->world_matrix_tf, &our_scene); + draw_scene(&frame_arena, core->models, &core->renderer, &cam, transform_tree, &our_scene); + // draw_model(&core->renderer, &cam, cube, &node1->world_matrix_tf, &our_scene); + // draw_model(&core->renderer, &cam, cube, &node2->world_matrix_tf, &our_scene); + // draw_model(&core->renderer, &cam, cube, &node3->world_matrix_tf, &our_scene); + // draw_model(&core->renderer, &cam, cube, &node4->world_matrix_tf, &our_scene); render_frame_end(&core->renderer); + arena_free_all(&frame_arena); } transform_hierarchy_free(transform_tree); diff --git a/src/renderer/render.c b/src/renderer/render.c index ca625ed..043a1fc 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -1,3 +1,6 @@ +#include +#include "mem.h" +#include "transform_hierarchy.h" #define STB_IMAGE_IMPLEMENTATION #include @@ -80,6 +83,30 @@ void default_material_init() { texture_data_upload(&DEFAULT_MATERIAL.specular_texture); } +typedef struct draw_ctx { + model_darray* models; + renderer* ren; + camera* cam; + scene* scene; +} draw_ctx; +bool draw_scene_node(transform_node* node, void* ctx_data) { + if (!node || !node->parent) return true; + draw_ctx* ctx = ctx_data; + model* m = &ctx->models->data[node->model.raw]; + draw_model(ctx->ren, ctx->cam, m, &node->world_matrix_tf, ctx->scene); + return true; +} + +void draw_scene(arena* frame, model_darray* models, renderer* ren, camera* camera, + transform_hierarchy* tfh, scene* scene) { + draw_ctx* ctx = arena_alloc(frame, sizeof(draw_ctx)); + ctx->models = models; + ctx->ren = ren; + ctx->cam = camera; + ctx->scene = scene; + transform_hierarchy_dfs(transform_hierarchy_root_node(tfh), draw_scene_node, true, ctx); +} + void draw_model(renderer* ren, camera* camera, model* model, mat4* model_tf, scene* scene) { // TRACE("Drawing model: %s", model->name); mat4 view; diff --git a/src/renderer/render.h b/src/renderer/render.h index 61dda75..bcdeb78 100644 --- a/src/renderer/render.h +++ b/src/renderer/render.h @@ -3,6 +3,7 @@ #include "camera.h" #include "loaders.h" #include "render_types.h" +#include "transform_hierarchy.h" // --- Lifecycle /** @brief initialise the render system frontend */ @@ -19,6 +20,7 @@ void render_frame_end(renderer* ren); void model_upload_meshes(renderer* ren, model* model); void draw_model(renderer* ren, camera* camera, model* model, mat4* tf, scene* scene); void draw_mesh(renderer* ren, mesh* mesh, mat4* tf, material* mat, mat4* view, mat4* proj); +void draw_scene(arena* frame, model_darray* models, renderer* ren, camera* camera, transform_hierarchy *tfh, scene* scene); // --- texture texture_data_load(const char* path, bool invert_y); // #frontend diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c index a8b3df1..a36b8ef 100644 --- a/src/transform_hierarchy.c +++ b/src/transform_hierarchy.c @@ -7,11 +7,11 @@ #include #include +#include "core.h" #include "log.h" #include "maths.h" #include "maths_types.h" #include "render_types.h" -#include "core.h" struct transform_hierarchy { transform_node root; @@ -50,7 +50,8 @@ void transform_hierarchy_free(transform_hierarchy* tfh) { transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh) { return &tfh->root; } -transform_node* transform_hierarchy_add_node(transform_node* parent, model_handle model, transform tf) { +transform_node* transform_hierarchy_add_node(transform_node* parent, model_handle model, + transform tf) { if (!parent) { WARN("You tried to add a node to a bad parent (NULL?)"); return NULL; @@ -163,14 +164,14 @@ bool print_node(transform_node* node, void* ctx_data) { return true; } - // Grab the model + // Grab the model model m = ctx->core->models->data[node->model.raw]; for (int i = 0; i < ctx->indentation_lvl; i++) { printf(" "); } printf("Node %s\n", m.name.buf); ctx->indentation_lvl++; - + return true; } -- cgit v1.2.3-70-g09d2 From e4a74976574a7d89906cfbf605ef095ca90dba61 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 17 Mar 2024 17:04:28 +1100 Subject: chore: format --- src/renderer/render.h | 3 ++- src/transform_hierarchy.h | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) (limited to 'src/renderer/render.h') diff --git a/src/renderer/render.h b/src/renderer/render.h index bcdeb78..1a35488 100644 --- a/src/renderer/render.h +++ b/src/renderer/render.h @@ -20,7 +20,8 @@ void render_frame_end(renderer* ren); void model_upload_meshes(renderer* ren, model* model); void draw_model(renderer* ren, camera* camera, model* model, mat4* tf, scene* scene); void draw_mesh(renderer* ren, mesh* mesh, mat4* tf, material* mat, mat4* view, mat4* proj); -void draw_scene(arena* frame, model_darray* models, renderer* ren, camera* camera, transform_hierarchy *tfh, scene* scene); +void draw_scene(arena* frame, model_darray* models, renderer* ren, camera* camera, + transform_hierarchy* tfh, scene* scene); // --- texture texture_data_load(const char* path, bool invert_y); // #frontend diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h index 9598fe9..af77ee1 100644 --- a/src/transform_hierarchy.h +++ b/src/transform_hierarchy.h @@ -1,17 +1,18 @@ /** * @file transform_hierarchy.h -*/ + */ #pragma once #include "maths_types.h" #include "render_types.h" -#define MAX_TF_NODE_CHILDREN 32 /** TEMP: Make it simpler to manage children in `transform_node`s */ +#define MAX_TF_NODE_CHILDREN \ + 32 /** TEMP: Make it simpler to manage children in `transform_node`s */ typedef struct transform_hierarchy transform_hierarchy; struct transform_node { - model_handle model; /** A handle back to what model this node represents */ + model_handle model; /** A handle back to what model this node represents */ transform tf; mat4 local_matrix_tf; /** cached local affine transform */ mat4 world_matrix_tf; /** cached world-space affine transform */ @@ -36,7 +37,8 @@ void transform_hierarchy_free(transform_hierarchy* tfh); // --- Main usecase -/** @brief Updates matrices of any invalidated nodes based on the `is_dirty` flag inside `transform` */ +/** @brief Updates matrices of any invalidated nodes based on the `is_dirty` flag inside `transform` + */ void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh); // --- Queries @@ -45,7 +47,8 @@ void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh); transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh); // --- Mutations -transform_node* transform_hierarchy_add_node(transform_node* parent, model_handle model, transform tf); +transform_node* transform_hierarchy_add_node(transform_node* parent, model_handle model, + transform tf); void transform_hierarchy_delete_node(transform_node* node); // --- Traversal @@ -53,16 +56,21 @@ void transform_hierarchy_delete_node(transform_node* node); /** * @brief Perform a depth-first search traversal starting from `start_node`. * @param start_node The starting node of the traversal. - * @param visit_node The function to call for each node visited. The callback should return false to stop the traversal early. - * @param is_pre_order Indicates whether to do pre-order or post-order traversal i.e. when to call the `visit_node` function. - * @param ctx_data An optional pointer to data that is be passed on each call to `visit_node`. Can be used to carry additional information or context. + * @param visit_node The function to call for each node visited. The callback should return false to + stop the traversal early. + * @param is_pre_order Indicates whether to do pre-order or post-order traversal i.e. when to call + the `visit_node` function. + * @param ctx_data An optional pointer to data that is be passed on each call to `visit_node`. Can + be used to carry additional information or context. * * @note The main use-cases are: 1. traversing the whole tree to update cached 4x4 affine transform matrices (post-order) 2. freeing child nodes after deleting a node in the tree (post-order) 3. debug pretty printing the whole tree (post-order) */ -void transform_hierarchy_dfs(transform_node* start_node, bool (*visit_node)(transform_node* node, void* ctx_data), bool is_pre_order, void* ctx_data); +void transform_hierarchy_dfs(transform_node* start_node, + bool (*visit_node)(transform_node* node, void* ctx_data), + bool is_pre_order, void* ctx_data); struct core; void transform_hierarchy_debug_print(transform_node* start_node, struct core* core); \ No newline at end of file -- cgit v1.2.3-70-g09d2