diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-17 14:59:35 +1100 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-03-17 14:59:35 +1100 |
commit | 1afe4876cb8133c5b47fdcfeb07decc5565c4844 (patch) | |
tree | 23c945a7b9f4cdef1cc74879548856a21a437313 | |
parent | a466cc429d8017eb0ee22237f5683cc75791fe85 (diff) |
wip: transform propagation
-rw-r--r-- | examples/transforms/ex_transforms.c | 51 | ||||
-rw-r--r-- | src/maths/maths.h | 6 | ||||
-rw-r--r-- | src/transform_hierarchy.c | 27 | ||||
-rw-r--r-- | src/transform_hierarchy.h | 6 | ||||
-rw-r--r-- | xmake.lua | 7 |
5 files changed, 93 insertions, 4 deletions
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 <glfw3.h> + +#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 @@ -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") |