From 95c154db45208416eb8d6c72dd191562957a35ad Mon Sep 17 00:00:00 2001 From: omnisci3nce <17525998+omnisci3nce@users.noreply.github.com> Date: Mon, 11 Mar 2024 18:46:19 +1100 Subject: first iteration of API --- src/transform_hierarchy.h | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/transform_hierarchy.h (limited to 'src/transform_hierarchy.h') diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h new file mode 100644 index 0000000..91b0559 --- /dev/null +++ b/src/transform_hierarchy.h @@ -0,0 +1,64 @@ +/** + * @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 */ + +typedef struct transform_hierarchy transform_hierarchy; + +struct transform_node { + 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 */ + + struct transform_node* parent; + struct transform_node* children[MAX_TF_NODE_CHILDREN]; + u32 n_children; + struct transform_hierarchy* tfh; +}; +typedef struct transform_node transform_node; + +// --- Lifecycle + +/** @brief Allocates and returns an empty transform hierarchy with a root node */ +transform_hierarchy* transform_hierarchy_create(); + +/** + * @brief recursively frees all the children and then finally itself + * @note in the future we can use an object pool for the nodes + */ +void transform_hierarchy_free(transform_hierarchy* tfh); + +// --- Main usecase + +void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh); + +// --- Queries + +/** Get a pointer to the root node */ +transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh); + +// --- Mutations +void transform_hierarchy_add_node(transform_node* parent, model_handle model, transform tf); +void transform_hierarchy_delete_node(transform_node* node); + +// --- Traversal + +/** + * @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. + * + * @note The main use-cases are: + 1. traversing the whole tree to update cached 4x4 affine transform matrices + 2. freeing child nodes after deleting a node in the tree + 3. debug pretty printing the whole tree + */ +void transform_hierarchy_dfs(transform_node* start_node, bool (*visit_node)(transform_node* node, void* ctx_data), bool is_pre_order, void* ctx_data); \ No newline at end of file -- cgit v1.2.3-70-g09d2 From a466cc429d8017eb0ee22237f5683cc75791fe85 Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Mon, 11 Mar 2024 19:13:53 +1100 Subject: minor docstring changes + fmt --- src/transform_hierarchy.c | 41 ++++++++++++++++++++--------------------- src/transform_hierarchy.h | 9 +++++---- 2 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src/transform_hierarchy.h') diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c index 0a4e592..25ace6c 100644 --- a/src/transform_hierarchy.c +++ b/src/transform_hierarchy.c @@ -1,15 +1,15 @@ /** * @file transform_hierarchy.h -*/ + */ #pragma once #include "transform_hierarchy.h" #include #include #include "log.h" -#include "maths_types.h" #include "maths.h" +#include "maths_types.h" #include "render_types.h" struct transform_hierarchy { @@ -19,22 +19,20 @@ struct transform_hierarchy { transform_hierarchy* transform_hierarchy_create() { transform_hierarchy* tfh = malloc(sizeof(transform_hierarchy)); - tfh->root =(transform_node){ - .model = ABSENT_MODEL_HANDLE, - .tf = TRANSFORM_DEFAULT, - .local_matrix_tf = mat4_ident(), - .world_matrix_tf = mat4_ident(), - .parent = NULL, - .children = {0}, - .n_children = 0, - .tfh = tfh - }; + tfh->root = (transform_node){ .model = { ABSENT_MODEL_HANDLE }, + .tf = TRANSFORM_DEFAULT, + .local_matrix_tf = mat4_ident(), + .world_matrix_tf = mat4_ident(), + .parent = NULL, + .children = { 0 }, + .n_children = 0, + .tfh = tfh }; return tfh; } bool free_node(transform_node* node, void* _ctx_data) { - if (!node) return true; // leaf node + if (!node) return true; // leaf node if (node == &node->tfh->root) { WARN("You can't free the root node!"); return false; @@ -50,9 +48,7 @@ void transform_hierarchy_free(transform_hierarchy* tfh) { free(tfh); } -transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh) { - return &tfh->root; -} +transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh) { return &tfh->root; } void transform_hierarchy_add_node(transform_node* parent, model_handle model, transform tf) { if (!parent) { @@ -89,17 +85,20 @@ void transform_hierarchy_delete_node(transform_node* node) { if (node->parent) { for (u32 i = 0; i < node->parent->n_children; i++) { - transform_node* child = node->parent->children[i]; - if (child == node) { - node->parent->children[i] = NULL; // HACK: this will leave behind empty slots in the children array of the parent. oh well. - } + transform_node* child = node->parent->children[i]; + if (child == node) { + node->parent->children[i] = NULL; // HACK: this will leave behind empty slots in the + // children array of the parent. oh well. + } } } free(node); } -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) { if (!start_node) return; bool continue_traversal = true; diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h index 91b0559..703baa8 100644 --- a/src/transform_hierarchy.h +++ b/src/transform_hierarchy.h @@ -36,11 +36,12 @@ void transform_hierarchy_free(transform_hierarchy* tfh); // --- Main usecase +/** @brief Updates matrices of any invalidated nodes based on the `is_dirty` flag inside `transform` */ void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh); // --- Queries -/** Get a pointer to the root node */ +/** @brief Get a pointer to the root node */ transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh); // --- Mutations @@ -57,8 +58,8 @@ void transform_hierarchy_delete_node(transform_node* node); * @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 - 2. freeing child nodes after deleting a node in the tree - 3. debug pretty printing the whole tree + 1. traversing the whole tree to update cached 4x4 affine transform matrices (pre-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); \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 1afe4876cb8133c5b47fdcfeb07decc5565c4844 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 17 Mar 2024 14:59:35 +1100 Subject: wip: transform propagation --- examples/transforms/ex_transforms.c | 51 +++++++++++++++++++++++++++++++++++++ src/maths/maths.h | 6 +++-- src/transform_hierarchy.c | 27 ++++++++++++++++++++ src/transform_hierarchy.h | 6 +++-- xmake.lua | 7 +++++ 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 examples/transforms/ex_transforms.c (limited to 'src/transform_hierarchy.h') diff --git a/examples/transforms/ex_transforms.c b/examples/transforms/ex_transforms.c new file mode 100644 index 0000000..edb1c21 --- /dev/null +++ b/examples/transforms/ex_transforms.c @@ -0,0 +1,51 @@ +#include + +#include "core.h" +#include "render.h" +#include "render_types.h" +#include "maths_types.h" +#include "maths.h" +#include "transform_hierarchy.h" + +int main() { + core* core = core_bringup(); + + // Set up scene + vec3 camera_pos = vec3(3., 4., 10.); + 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* cube = &core->models->data[cube_handle.raw]; + // 2. upload vertex data to gpu + model_upload_meshes(&core->renderer, cube); + + // Create transform hierarchy + transform_hierarchy* transform_tree = transform_hierarchy_create(); + transform_node* root_node = transform_hierarchy_root_node(transform_tree); + + // Add nodes + // -- 4 cubes + transform cube1 = transform_create(vec3(-2.0, -2.0, -2.0), quat_ident(), 2.0); + transform cube2 = transform_create(vec3(2.0, 2.0, 2.0), quat_ident(), 2.0); + transform_hierarchy_add_node(root_node, cube_handle, cube1); + transform_hierarchy_add_node(root_node, cube_handle, cube2); + + + // Main loop + while (!glfwWindowShouldClose(core->renderer.window)) { + input_update(&core->input); + threadpool_process_results(&core->threadpool, 1); + + render_frame_begin(&core->renderer); + + // insert work here + + render_frame_end(&core->renderer); + } + + transform_hierarchy_free(transform_tree); + + return 0; +} diff --git a/src/maths/maths.h b/src/maths/maths.h index d832739..390b611 100644 --- a/src/maths/maths.h +++ b/src/maths/maths.h @@ -207,8 +207,10 @@ static transform transform_create(vec3 pos, quat rot, f32 scale) { } static inline mat4 transform_to_mat(transform *tf) { - // TODO: rotation - return mat4_mult(mat4_translation(tf->position), mat4_scale(tf->scale)); + mat4 trans = mat4_translation(tf->position); + mat4 rot = mat4_rotation(tf->rotation); + mat4 scale = mat4_scale(tf->scale); + return mat4_mult(trans, mat4_mult(rot, scale)); } // --- Sizing asserts diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c index 25ace6c..468be56 100644 --- a/src/transform_hierarchy.c +++ b/src/transform_hierarchy.c @@ -117,4 +117,31 @@ void transform_hierarchy_dfs(transform_node* start_node, // post-order visit_node(start_node, ctx_data); } +} + +// Update matrix for the current node +bool update_matrix(transform_node* node, void* _ctx_data) { + if (!node) return true; // leaf node + + if (node->parent->tf.is_dirty) { + node->tf.is_dirty = true; + } + + if (node->tf.is_dirty) { + // invalidates children + mat4 updated_local_transform = transform_to_mat(&node->tf); + node->local_matrix_tf = updated_local_transform; + if (node->parent) { + mat4 updated_world_transform = + mat4_mult(node->parent->world_matrix_tf, updated_local_transform); + node->world_matrix_tf = updated_world_transform; + } + } + + return true; +} + +void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh) { + // kickoff traversal + transform_hierarchy_dfs(&tfh->root, update_matrix, false, NULL); } \ No newline at end of file diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h index 703baa8..9af8a97 100644 --- a/src/transform_hierarchy.h +++ b/src/transform_hierarchy.h @@ -58,8 +58,10 @@ void transform_hierarchy_delete_node(transform_node* node); * @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 (pre-order) + 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); \ No newline at end of file +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_debug_print(transform_node* start_node, ) \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 15159ef..0f039e7 100644 --- a/xmake.lua +++ b/xmake.lua @@ -115,6 +115,13 @@ target("obj") add_files("examples/obj_loading/ex_obj_loading.c") set_rundir("$(projectdir)") +target("transforms") + set_kind("binary") + set_group("examples") + add_deps("core_shared") + add_files("examples/transforms/ex_transforms.c") + set_rundir("$(projectdir)") + target("demo") set_kind("binary") set_group("examples") -- cgit v1.2.3-70-g09d2 From 31359e468c4f1d844f19862f63ed911b66b98068 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 17 Mar 2024 15:07:45 +1100 Subject: print desu? --- src/transform_hierarchy.c | 16 ++++++++++++++++ src/transform_hierarchy.h | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src/transform_hierarchy.h') diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c index 468be56..65d4edf 100644 --- a/src/transform_hierarchy.c +++ b/src/transform_hierarchy.c @@ -11,6 +11,7 @@ #include "maths.h" #include "maths_types.h" #include "render_types.h" +#include "core.h" struct transform_hierarchy { transform_node root; @@ -144,4 +145,19 @@ bool update_matrix(transform_node* node, void* _ctx_data) { void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh) { // kickoff traversal transform_hierarchy_dfs(&tfh->root, update_matrix, false, NULL); +} + +void print_node(transform_node* node, void* _ctx_data) { + // Grab the model + model m = core->models->data[start_node->model.raw]; + printf("Node %s\n", m.name.buf); +} + +struct print_ctx { + core* core; + u32 indentation_lvl; +}; + +void transform_hierarchy_debug_print(transform_node* start_node, core* core) { + } \ No newline at end of file diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h index 9af8a97..61989f1 100644 --- a/src/transform_hierarchy.h +++ b/src/transform_hierarchy.h @@ -64,4 +64,5 @@ void transform_hierarchy_delete_node(transform_node* node); */ 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_debug_print(transform_node* start_node, ) \ No newline at end of file +struct core; +void transform_hierarchy_debug_print(transform_node* start_nod, struct core* core); \ No newline at end of file -- cgit v1.2.3-70-g09d2 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 --- examples/transforms/ex_transforms.c | 49 +++++++++++++++++++++++++++++++------ src/maths/maths.h | 2 +- src/renderer/render.c | 16 ++++++------ src/renderer/render.h | 4 +-- src/transform_hierarchy.c | 43 +++++++++++++++++++++++--------- src/transform_hierarchy.h | 4 +-- 6 files changed, 86 insertions(+), 32 deletions(-) (limited to 'src/transform_hierarchy.h') diff --git a/examples/transforms/ex_transforms.c b/examples/transforms/ex_transforms.c index edb1c21..b11fbc7 100644 --- a/examples/transforms/ex_transforms.c +++ b/examples/transforms/ex_transforms.c @@ -7,6 +7,14 @@ #include "maths.h" #include "transform_hierarchy.h" +const vec3 pointlight_positions[4] = { + { 0.7, 0.2, 2.0 }, + { 2.3, -3.3, -4.0 }, + { -4.0, 2.0, -12.0 }, + { 0.0, 0.0, -3.0 }, +}; +point_light point_lights[4]; + int main() { core* core = core_bringup(); @@ -18,20 +26,43 @@ int main() { model_handle cube_handle = model_load_obj(core, "assets/models/obj/cube/cube.obj", true); model* cube = &core->models->data[cube_handle.raw]; - // 2. upload vertex data to gpu model_upload_meshes(&core->renderer, cube); + directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 }, + .ambient = (vec3){ 0.2, 0.2, 0.2 }, + .diffuse = (vec3){ 0.5, 0.5, 0.5 }, + .specular = (vec3){ 1.0, 1.0, 1.0 } }; + // point lights setup + for (int i = 0; i < 4; i++) { + point_lights[i].position = pointlight_positions[i]; + point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 }; + point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 }; + point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 }; + point_lights[i].constant = 1.0; + point_lights[i].linear = 0.09; + point_lights[i].quadratic = 0.032; + } + + scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 }; + memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4])); + // Create transform hierarchy transform_hierarchy* transform_tree = transform_hierarchy_create(); transform_node* root_node = transform_hierarchy_root_node(transform_tree); - // Add nodes // -- 4 cubes - transform cube1 = transform_create(vec3(-2.0, -2.0, -2.0), quat_ident(), 2.0); - transform cube2 = transform_create(vec3(2.0, 2.0, 2.0), quat_ident(), 2.0); - transform_hierarchy_add_node(root_node, cube_handle, cube1); - transform_hierarchy_add_node(root_node, cube_handle, cube2); + transform cube1 = transform_create(vec3(1.0, 1.0, 0.0), quat_ident(), 2.0); + transform cube2 = transform_create(vec3(1.0, -1.0, 0.0), quat_ident(), 2.0); + transform cube3 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 2.0); + transform cube4 = transform_create(vec3(0.0, -1.0, 0.0), quat_ident(), 2.0); + transform_node* node1 = transform_hierarchy_add_node(root_node, cube_handle, cube1); + transform_node* node2 = transform_hierarchy_add_node(root_node, cube_handle, cube2); + + transform_node* node3 = transform_hierarchy_add_node(node1, cube_handle, cube3); + transform_node* node4 = transform_hierarchy_add_node(node2, cube_handle, cube4); + transform_hierarchy_debug_print(root_node, core); + // Main loop while (!glfwWindowShouldClose(core->renderer.window)) { @@ -39,8 +70,12 @@ int main() { threadpool_process_results(&core->threadpool, 1); render_frame_begin(&core->renderer); + transform_hierarchy_propagate_transforms(transform_tree); - // insert work here + 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); } diff --git a/src/maths/maths.h b/src/maths/maths.h index 390b611..6834f69 100644 --- a/src/maths/maths.h +++ b/src/maths/maths.h @@ -203,7 +203,7 @@ static inline mat4 mat4_look_at(vec3 position, vec3 target, vec3 up) { .is_dirty = false }) static transform transform_create(vec3 pos, quat rot, f32 scale) { - return (transform){ .position = pos, .rotation = rot, .scale = scale, .is_dirty = false }; + return (transform){ .position = pos, .rotation = rot, .scale = scale, .is_dirty = true }; } static inline mat4 transform_to_mat(transform *tf) { diff --git a/src/renderer/render.c b/src/renderer/render.c index 7884db6..ca625ed 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -80,7 +80,7 @@ void default_material_init() { texture_data_upload(&DEFAULT_MATERIAL.specular_texture); } -void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene* scene) { +void draw_model(renderer* ren, camera* camera, model* model, mat4* model_tf, scene* scene) { // TRACE("Drawing model: %s", model->name); mat4 view; mat4 proj; @@ -103,11 +103,11 @@ void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene } // TRACE("Drawing mesh %d", i); material* mat = &model->materials->data[m->material_index]; - draw_mesh(ren, m, tf, mat, &view, &proj); + draw_mesh(ren, m, model_tf, mat, &view, &proj); } } -void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* view, mat4* proj) { +void draw_mesh(renderer* ren, mesh* mesh, mat4* model_tf, material* mat, mat4* view, mat4* proj) { shader lighting_shader = ren->blinn_phong; // bind buffer @@ -119,12 +119,12 @@ void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* vie uniform_f32(lighting_shader.program_id, "material.shininess", 32.); // upload model transform - mat4 trans = mat4_translation(tf.position); - mat4 rot = mat4_rotation(tf.rotation); - mat4 scale = mat4_scale(tf.scale); - mat4 model_tf = mat4_mult(trans, mat4_mult(rot, scale)); + // mat4 trans = mat4_translation(tf.position); + // mat4 rot = mat4_rotation(tf.rotation); + // mat4 scale = mat4_scale(tf.scale); + // mat4 model_tf = mat4_mult(trans, mat4_mult(rot, scale)); - uniform_mat4f(lighting_shader.program_id, "model", &model_tf); + uniform_mat4f(lighting_shader.program_id, "model", model_tf); // upload view & projection matrices uniform_mat4f(lighting_shader.program_id, "view", view); uniform_mat4f(lighting_shader.program_id, "projection", proj); 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 diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c index 65d4edf..a8b3df1 100644 --- a/src/transform_hierarchy.c +++ b/src/transform_hierarchy.c @@ -18,7 +18,7 @@ struct transform_hierarchy { }; transform_hierarchy* transform_hierarchy_create() { - transform_hierarchy* tfh = malloc(sizeof(transform_hierarchy)); + transform_hierarchy* tfh = malloc(sizeof(struct transform_hierarchy)); tfh->root = (transform_node){ .model = { ABSENT_MODEL_HANDLE }, .tf = TRANSFORM_DEFAULT, @@ -28,7 +28,6 @@ transform_hierarchy* transform_hierarchy_create() { .children = { 0 }, .n_children = 0, .tfh = tfh }; - return tfh; } @@ -51,10 +50,10 @@ void transform_hierarchy_free(transform_hierarchy* tfh) { transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh) { return &tfh->root; } -void 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; + return NULL; } transform_node* node = malloc(sizeof(transform_node)); node->model = model; @@ -75,6 +74,8 @@ void transform_hierarchy_add_node(transform_node* parent, model_handle model, tr parent->children[next_index] = node; parent->n_children++; } + + return node; } void transform_hierarchy_delete_node(transform_node* node) { @@ -124,7 +125,7 @@ void transform_hierarchy_dfs(transform_node* start_node, bool update_matrix(transform_node* node, void* _ctx_data) { if (!node) return true; // leaf node - if (node->parent->tf.is_dirty) { + if (node->parent && node->parent->tf.is_dirty) { node->tf.is_dirty = true; } @@ -147,17 +148,35 @@ void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh) { transform_hierarchy_dfs(&tfh->root, update_matrix, false, NULL); } -void print_node(transform_node* node, void* _ctx_data) { - // Grab the model - model m = core->models->data[start_node->model.raw]; - printf("Node %s\n", m.name.buf); -} - struct print_ctx { core* core; u32 indentation_lvl; }; -void transform_hierarchy_debug_print(transform_node* start_node, core* core) { +bool print_node(transform_node* node, void* ctx_data) { + struct print_ctx* ctx = (struct print_ctx*)ctx_data; + + if (!node) return true; + if (!node->parent) { + printf("Root Node\n"); + ctx->indentation_lvl++; + return true; + } + + // 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; +} + +void transform_hierarchy_debug_print(transform_node* start_node, core* core) { + struct print_ctx* ctx = malloc(sizeof(struct print_ctx)); + ctx->core = core; + ctx->indentation_lvl = 0; + transform_hierarchy_dfs(start_node, print_node, true, (void*)ctx); } \ No newline at end of file diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h index 61989f1..9598fe9 100644 --- a/src/transform_hierarchy.h +++ b/src/transform_hierarchy.h @@ -45,7 +45,7 @@ void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh); transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh); // --- Mutations -void 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 @@ -65,4 +65,4 @@ void transform_hierarchy_delete_node(transform_node* node); 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_nod, struct core* core); \ No newline at end of file +void transform_hierarchy_debug_print(transform_node* start_node, struct core* core); \ No newline at end of file -- 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/transform_hierarchy.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