/** * @file transform_hierarchy.h */ #pragma once #include "transform_hierarchy.h" #include #include #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); } }