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 --- src/transform_hierarchy.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/transform_hierarchy.c') 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 -- cgit v1.2.3-70-g09d2