summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Rowe <17525998+omnisci3nce@users.noreply.github.com>2024-04-21 11:48:06 +1000
committerGitHub <noreply@github.com>2024-04-21 11:48:06 +1000
commitd8b564ffd1257152094966784a8db3d1396a8879 (patch)
treeadbbda6eec38d0cd6f35f29e422debe09b807461
parent45f035c2174a018444a4e495ec78b4806900d903 (diff)
parent540bdfd0cbe4a95b49281cec4d3ed207070a3b5b (diff)
Merge pull request #10 from omnisci3nce/cel-41-port-over-a-basic-3d-scene-example
Cel 41 port over a basic 3d scene example
-rw-r--r--examples/gltf_loading/ex_gltf_loading.c3
-rw-r--r--examples/obj_loading/ex_obj_loading.c3
-rw-r--r--examples/transforms/ex_transforms.c88
-rw-r--r--src/maths/maths.h8
-rw-r--r--src/renderer/render.c43
-rw-r--r--src/renderer/render.h7
-rw-r--r--src/renderer/render_types.h1
-rw-r--r--src/std/mem.c2
-rw-r--r--src/transform_hierarchy.c184
-rw-r--r--src/transform_hierarchy.h76
-rw-r--r--xmake.lua7
11 files changed, 406 insertions, 16 deletions
diff --git a/examples/gltf_loading/ex_gltf_loading.c b/examples/gltf_loading/ex_gltf_loading.c
index 867ddb2..98654d6 100644
--- a/examples/gltf_loading/ex_gltf_loading.c
+++ b/examples/gltf_loading/ex_gltf_loading.c
@@ -70,7 +70,8 @@ int main() {
angle += (rot_speed * deltaTime);
transform model_tf = transform_create(vec3(0.0, 0.1, -0.1), rot,
1.8); // make the backpack a bit bigger
- draw_model(&core->renderer, &cam, cube, model_tf, &our_scene);
+ mat4 model = transform_to_mat(&model_tf);
+ draw_model(&core->renderer, &cam, cube, &model, &our_scene);
render_frame_end(&core->renderer);
}
diff --git a/examples/obj_loading/ex_obj_loading.c b/examples/obj_loading/ex_obj_loading.c
index 6e63938..e225cb2 100644
--- a/examples/obj_loading/ex_obj_loading.c
+++ b/examples/obj_loading/ex_obj_loading.c
@@ -62,7 +62,8 @@ int main() {
// Draw the backpack
transform model_tf = transform_create(vec3(0.0, -0.4, 0.0), quat_ident(),
1.8); // make the backpack a bit bigger
- draw_model(&core->renderer, &cam, backpack, model_tf, &our_scene);
+ mat4 model_matrix = transform_to_mat(&model_tf);
+ draw_model(&core->renderer, &cam, backpack, &model_matrix, &our_scene);
render_frame_end(&core->renderer);
}
diff --git a/examples/transforms/ex_transforms.c b/examples/transforms/ex_transforms.c
new file mode 100644
index 0000000..689c49d
--- /dev/null
+++ b/examples/transforms/ex_transforms.c
@@ -0,0 +1,88 @@
+#include <glfw3.h>
+
+#include "core.h"
+#include "maths.h"
+#include "maths_types.h"
+#include "mem.h"
+#include "render.h"
+#include "render_types.h"
+#include "transform_hierarchy.h"
+
+const vec3 pointlight_positions[4] = {
+ { 0.7, 0.2, 2.0 },
+ { 2.3, -3.3, -4.0 },
+ { -4.0, 2.0, -12.0 },
+ { 0.0, 0.0, -3.0 },
+};
+point_light point_lights[4];
+
+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];
+ model_upload_meshes(&core->renderer, cube);
+
+ directional_light dir_light = { .direction = (vec3){ -0.2, -1.0, -0.3 },
+ .ambient = (vec3){ 0.2, 0.2, 0.2 },
+ .diffuse = (vec3){ 0.5, 0.5, 0.5 },
+ .specular = (vec3){ 1.0, 1.0, 1.0 } };
+ // point lights setup
+ for (int i = 0; i < 4; i++) {
+ point_lights[i].position = pointlight_positions[i];
+ point_lights[i].ambient = (vec3){ 0.05, 0.05, 0.05 };
+ point_lights[i].diffuse = (vec3){ 0.8, 0.8, 0.8 };
+ point_lights[i].specular = (vec3){ 1.0, 1.0, 1.0 };
+ point_lights[i].constant = 1.0;
+ point_lights[i].linear = 0.09;
+ point_lights[i].quadratic = 0.032;
+ }
+
+ scene our_scene = { .dir_light = dir_light, .n_point_lights = 4 };
+ memcpy(&our_scene.point_lights, &point_lights, sizeof(point_light[4]));
+
+ // 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(0.0, -2.0, 0.0), quat_ident(), 0.8);
+ transform cube2 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 1.0);
+ transform cube3 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 1.0);
+ transform cube4 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 1.0);
+ transform_node* node1 = transform_hierarchy_add_node(root_node, cube_handle, cube1);
+ transform_node* node2 = transform_hierarchy_add_node(node1, cube_handle, cube2);
+ transform_node* node3 = transform_hierarchy_add_node(node2, cube_handle, cube3);
+ transform_node* node4 = transform_hierarchy_add_node(node3, cube_handle, cube4);
+
+ transform_hierarchy_debug_print(root_node, core);
+
+ char* frame_allocator_storage = malloc(1024 * 1024 * 64);
+ arena frame_arena = arena_create(frame_allocator_storage, 1024 * 1024 * 64);
+
+ // Main loop
+ while (!glfwWindowShouldClose(core->renderer.window)) {
+ input_update(&core->input);
+ threadpool_process_results(&core->threadpool, 1);
+
+ render_frame_begin(&core->renderer);
+ transform_hierarchy_propagate_transforms(transform_tree);
+
+ // TODO: Add setters to transform API
+ node1->tf.position.x += 0.004;
+ node1->tf.is_dirty = true;
+ draw_scene(&frame_arena, core->models, &core->renderer, &cam, transform_tree, &our_scene);
+
+ render_frame_end(&core->renderer);
+ arena_free_all(&frame_arena);
+ }
+
+ transform_hierarchy_free(transform_tree);
+
+ return 0;
+}
diff --git a/src/maths/maths.h b/src/maths/maths.h
index 57d575d..3301634 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -215,12 +215,14 @@ static inline mat4 mat4_look_at(vec3 position, vec3 target, vec3 up) {
.is_dirty = false })
static transform transform_create(vec3 pos, quat rot, f32 scale) {
- return (transform){ .position = pos, .rotation = rot, .scale = scale, .is_dirty = false };
+ return (transform){ .position = pos, .rotation = rot, .scale = scale, .is_dirty = true };
}
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.c b/src/renderer/render.c
index 9d250c6..ac6b80a 100644
--- a/src/renderer/render.c
+++ b/src/renderer/render.c
@@ -1,3 +1,6 @@
+#include <stdlib.h>
+#include "mem.h"
+#include "transform_hierarchy.h"
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
@@ -82,7 +85,31 @@ void default_material_init() {
texture_data_upload(&DEFAULT_MATERIAL.specular_texture);
}
-void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene* scene) {
+typedef struct draw_ctx {
+ model_darray* models;
+ renderer* ren;
+ camera* cam;
+ scene* scene;
+} draw_ctx;
+bool draw_scene_node(transform_node* node, void* ctx_data) {
+ if (!node || !node->parent) return true;
+ draw_ctx* ctx = ctx_data;
+ model* m = &ctx->models->data[node->model.raw];
+ draw_model(ctx->ren, ctx->cam, m, &node->world_matrix_tf, ctx->scene);
+ return true;
+}
+
+void draw_scene(arena* frame, model_darray* models, renderer* ren, camera* camera,
+ transform_hierarchy* tfh, scene* scene) {
+ draw_ctx* ctx = arena_alloc(frame, sizeof(draw_ctx));
+ ctx->models = models;
+ ctx->ren = ren;
+ ctx->cam = camera;
+ ctx->scene = scene;
+ transform_hierarchy_dfs(transform_hierarchy_root_node(tfh), draw_scene_node, true, ctx);
+}
+
+void draw_model(renderer* ren, camera* camera, model* model, mat4* model_tf, scene* scene) {
// TRACE("Drawing model: %s", model->name);
mat4 view;
mat4 proj;
@@ -105,11 +132,11 @@ void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene
}
// TRACE("Drawing mesh %d", i);
material* mat = &model->materials->data[m->material_index];
- draw_mesh(ren, m, tf, mat, &view, &proj);
+ draw_mesh(ren, m, model_tf, mat, &view, &proj);
}
}
-void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* view, mat4* proj) {
+void draw_mesh(renderer* ren, mesh* mesh, mat4* model_tf, material* mat, mat4* view, mat4* proj) {
shader lighting_shader = ren->blinn_phong;
// bind buffer
@@ -121,12 +148,12 @@ void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* vie
uniform_f32(lighting_shader.program_id, "material.shininess", 32.);
// upload model transform
- mat4 trans = mat4_translation(tf.position);
- mat4 rot = mat4_rotation(tf.rotation);
- mat4 scale = mat4_scale(tf.scale);
- mat4 model_tf = mat4_mult(trans, mat4_mult(rot, scale));
+ // mat4 trans = mat4_translation(tf.position);
+ // mat4 rot = mat4_rotation(tf.rotation);
+ // mat4 scale = mat4_scale(tf.scale);
+ // mat4 model_tf = mat4_mult(trans, mat4_mult(rot, scale));
- uniform_mat4f(lighting_shader.program_id, "model", &model_tf);
+ uniform_mat4f(lighting_shader.program_id, "model", model_tf);
// upload view & projection matrices
uniform_mat4f(lighting_shader.program_id, "view", view);
uniform_mat4f(lighting_shader.program_id, "projection", proj);
diff --git a/src/renderer/render.h b/src/renderer/render.h
index 10702e3..1a35488 100644
--- a/src/renderer/render.h
+++ b/src/renderer/render.h
@@ -3,6 +3,7 @@
#include "camera.h"
#include "loaders.h"
#include "render_types.h"
+#include "transform_hierarchy.h"
// --- Lifecycle
/** @brief initialise the render system frontend */
@@ -17,8 +18,10 @@ void render_frame_end(renderer* ren);
// --- models meshes
void model_upload_meshes(renderer* ren, model* model);
-void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene* scene);
-void draw_mesh(renderer* ren, mesh* mesh, transform tf, material* mat, mat4* view, mat4* proj);
+void draw_model(renderer* ren, camera* camera, model* model, mat4* tf, scene* scene);
+void draw_mesh(renderer* ren, mesh* mesh, mat4* tf, material* mat, mat4* view, mat4* proj);
+void draw_scene(arena* frame, model_darray* models, renderer* ren, camera* camera,
+ transform_hierarchy* tfh, scene* scene);
// ---
texture texture_data_load(const char* path, bool invert_y); // #frontend
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/std/mem.c b/src/std/mem.c
index d7c0f4c..00f9c39 100644
--- a/src/std/mem.c
+++ b/src/std/mem.c
@@ -11,7 +11,7 @@
void* arena_alloc_align(arena* a, size_t size, size_t align) {
ptrdiff_t padding = -(uintptr_t)a->curr & (align - 1);
ptrdiff_t available = a->end - a->curr - padding;
- TRACE("Padding %td available %td", padding, available);
+ // TRACE("Padding %td available %td", padding, available);
if (available < 0 || (ptrdiff_t)size > available) {
ERROR_EXIT("Arena ran out of memory\n");
}
diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c
new file mode 100644
index 0000000..f1c859a
--- /dev/null
+++ b/src/transform_hierarchy.c
@@ -0,0 +1,184 @@
+
+/**
+ * @file transform_hierarchy.h
+ */
+#pragma once
+#include "transform_hierarchy.h"
+#include <stdlib.h>
+#include <string.h>
+
+#include "core.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(struct 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; }
+
+transform_node* 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 NULL;
+ }
+ 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++;
+ }
+
+ return node;
+}
+
+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 && 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);
+}
+
+struct print_ctx {
+ core* core;
+ u32 indentation_lvl;
+};
+
+bool print_node(transform_node* node, void* ctx_data) {
+ struct print_ctx* ctx = (struct print_ctx*)ctx_data;
+
+ if (!node) return true;
+ if (!node->parent) {
+ printf("Root Node\n");
+ ctx->indentation_lvl++;
+ return true;
+ }
+
+ // Grab the model
+ model m = ctx->core->models->data[node->model.raw];
+ for (int i = 0; i < ctx->indentation_lvl; i++) {
+ printf(" ");
+ }
+ printf("Node %s\n", m.name.buf);
+ ctx->indentation_lvl++;
+
+ return true;
+}
+
+void transform_hierarchy_debug_print(transform_node* start_node, core* core) {
+ struct print_ctx* ctx = malloc(sizeof(struct print_ctx));
+ ctx->core = core;
+ ctx->indentation_lvl = 0;
+ transform_hierarchy_dfs(start_node, print_node, true, (void*)ctx);
+ free(ctx);
+} \ No newline at end of file
diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h
new file mode 100644
index 0000000..af77ee1
--- /dev/null
+++ b/src/transform_hierarchy.h
@@ -0,0 +1,76 @@
+/**
+ * @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
+transform_node* 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);
+
+struct core;
+void transform_hierarchy_debug_print(transform_node* start_node, struct core* core); \ No newline at end of file
diff --git a/xmake.lua b/xmake.lua
index c18e926..453fb55 100644
--- a/xmake.lua
+++ b/xmake.lua
@@ -126,6 +126,13 @@ target("gltf")
add_files("examples/gltf_loading/ex_gltf_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")