summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/transforms/ex_transforms.c51
-rw-r--r--src/maths/maths.h6
-rw-r--r--src/renderer/render_types.h1
-rw-r--r--src/transform_hierarchy.c147
-rw-r--r--src/transform_hierarchy.h67
-rw-r--r--xmake.lua7
6 files changed, 277 insertions, 2 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/renderer/render_types.h b/src/renderer/render_types.h
index 483e392..aed18db 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..468be56
--- /dev/null
+++ b/src/transform_hierarchy.c
@@ -0,0 +1,147 @@
+
+/**
+ * @file transform_hierarchy.h
+ */
+#pragma once
+#include "transform_hierarchy.h"
+#include <stdlib.h>
+#include <string.h>
+
+#include "log.h"
+#include "maths.h"
+#include "maths_types.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);
+ }
+}
+
+// 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
new file mode 100644
index 0000000..9af8a97
--- /dev/null
+++ b/src/transform_hierarchy.h
@@ -0,0 +1,67 @@
+/**
+ * @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
+
+/** @brief Updates matrices of any invalidated nodes based on the `is_dirty` flag inside `transform` */
+void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh);
+
+// --- Queries
+
+/** @brief 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 (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_debug_print(transform_node* start_node, ) \ No newline at end of file
diff --git a/xmake.lua b/xmake.lua
index 611b537..2a899dd 100644
--- a/xmake.lua
+++ b/xmake.lua
@@ -118,6 +118,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")