diff options
author | omnisci3nce <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-11 18:46:19 +1100 |
---|---|---|
committer | omnisci3nce <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-11 18:46:19 +1100 |
commit | 95c154db45208416eb8d6c72dd191562957a35ad (patch) | |
tree | 0c4c604cd0650802de7bcab035f16d995d16e8a3 /src | |
parent | 7b9ef1066e49fe3e0c7791e097b26445f0f35f3d (diff) |
first iteration of API
Diffstat (limited to 'src')
-rw-r--r-- | src/renderer/render_types.h | 1 | ||||
-rw-r--r-- | src/transform_hierarchy.c | 121 | ||||
-rw-r--r-- | src/transform_hierarchy.h | 64 |
3 files changed, 186 insertions, 0 deletions
diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 746dfce..3cba2ea 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -18,6 +18,7 @@ struct GLFWwindow; #ifndef RESOURCE_HANDLE_DEFS CORE_DEFINE_HANDLE(model_handle); +#define ABSENT_MODEL_HANDLE 999999999 CORE_DEFINE_HANDLE(texture_handle); #define RESOURCE_HANDLE_DEFS #endif diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c new file mode 100644 index 0000000..0a4e592 --- /dev/null +++ b/src/transform_hierarchy.c @@ -0,0 +1,121 @@ + +/** + * @file transform_hierarchy.h +*/ +#pragma once +#include "transform_hierarchy.h" +#include <stdlib.h> +#include <string.h> + +#include "log.h" +#include "maths_types.h" +#include "maths.h" +#include "render_types.h" + +struct transform_hierarchy { + transform_node root; +}; + +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 + }; + + return tfh; +} + +bool free_node(transform_node* node, void* _ctx_data) { + if (!node) return true; // leaf node + if (node == &node->tfh->root) { + WARN("You can't free the root node!"); + return false; + } + + printf("Freed node\n"); + free(node); + return true; +} + +void transform_hierarchy_free(transform_hierarchy* tfh) { + transform_hierarchy_dfs(&tfh->root, free_node, false, NULL); + free(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) { + if (!parent) { + WARN("You tried to add a node to a bad parent (NULL?)"); + return; + } + transform_node* node = malloc(sizeof(transform_node)); + node->model = model; + node->tf = tf; + node->local_matrix_tf = mat4_ident(); + node->world_matrix_tf = mat4_ident(); + node->parent = parent; + memset(node->children, 0, sizeof(node->children)); + node->n_children = 0; + node->tfh = parent->tfh; + + // push into parent's children array + u32 next_index = parent->n_children; + if (next_index == MAX_TF_NODE_CHILDREN) { + ERROR("This transform hierarchy node already has MAX children. Dropping."); + free(node); + } else { + parent->children[next_index] = node; + parent->n_children++; + } +} + +void transform_hierarchy_delete_node(transform_node* node) { + // delete all children + for (u32 i = 0; i < node->n_children; i++) { + transform_node* child = node->children[i]; + transform_hierarchy_dfs(child, free_node, false, NULL); + } + + 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. + } + } + } + + 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) { + if (!start_node) return; + + bool continue_traversal = true; + if (is_pre_order) { + continue_traversal = visit_node(start_node, ctx_data); + } + + if (continue_traversal) { + for (u32 i = 0; i < start_node->n_children; i++) { + transform_node* child = start_node->children[i]; + transform_hierarchy_dfs(child, visit_node, is_pre_order, ctx_data); + } + } + + if (!is_pre_order) { + // post-order + visit_node(start_node, ctx_data); + } +}
\ No newline at end of file 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 |