summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/maths/maths.h8
-rw-r--r--src/platform/path.c9
-rw-r--r--src/platform/path.h2
-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/resources/obj.c12
-rw-r--r--src/std/mem.c6
-rw-r--r--src/std/mem.h1
-rw-r--r--src/transform_hierarchy.c184
-rw-r--r--src/transform_hierarchy.h76
11 files changed, 327 insertions, 22 deletions
diff --git a/src/maths/maths.h b/src/maths/maths.h
index d832739..6834f69 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -203,12 +203,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/platform/path.c b/src/platform/path.c
index e67102b..9572941 100644
--- a/src/platform/path.c
+++ b/src/platform/path.c
@@ -1,12 +1,17 @@
#include "path.h"
#include <libgen.h>
+#include <stdlib.h>
#include <string.h>
+#include "mem.h"
#include "str.h"
#if defined(CEL_PLATFORM_LINUX) || defined(CEL_PLATFORM_MAC)
-path_opt path_parent(const char* path) {
- char* path_dirname = dirname(path);
+path_opt path_parent(arena* a, const char* path) {
+ // Duplicate the string because dirname doesnt like const literals
+ char* path_copy = arena_alloc(a, strlen(path) + 1);
+ strcpy(path_copy, path);
+ char* path_dirname = dirname(path_copy);
return (path_opt){ .path = str8_cstr_view(path_dirname), .has_value = true };
}
#endif
diff --git a/src/platform/path.h b/src/platform/path.h
index 0ec6993..73063ea 100644
--- a/src/platform/path.h
+++ b/src/platform/path.h
@@ -13,4 +13,4 @@ typedef struct path_opt {
bool has_value;
} path_opt;
-path_opt path_parent(const char* path); // TODO: convert to using str8 \ No newline at end of file
+path_opt path_parent(arena* a, const char* path); // TODO: convert to using str8 \ No newline at end of file
diff --git a/src/renderer/render.c b/src/renderer/render.c
index 7884db6..043a1fc 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>
@@ -80,7 +83,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;
@@ -103,11 +130,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
@@ -119,12 +146,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/resources/obj.c b/src/resources/obj.c
index 710d5f0..c6e9fa6 100644
--- a/src/resources/obj.c
+++ b/src/resources/obj.c
@@ -15,6 +15,7 @@
#include "file.h"
#include "log.h"
#include "maths.h"
+#include "mem.h"
#include "path.h"
#include "render.h"
#include "render_types.h"
@@ -40,16 +41,16 @@ bool model_load_obj_str(const char *file_string, str8 relative_path, model *out_
bool invert_textures_y);
model_handle model_load_obj(core *core, const char *path, bool invert_textures_y) {
+ size_t arena_size = 1024;
+ arena scratch = arena_create(malloc(arena_size), arena_size);
+
TRACE("Loading model at Path %s\n", path);
- path_opt relative_path = path_parent(path);
+ path_opt relative_path = path_parent(&scratch, path);
if (!relative_path.has_value) {
WARN("Couldnt get a relative path for the path to use for loading materials & textures later");
}
- printf("Relative path: %s\n", relative_path.path.buf);
const char *file_string = string_from_file(path);
- // TODO: store the relative path without the name.obj at the end
-
model model = { 0 };
model.name = str8_cstr_view(path);
model.meshes = mesh_darray_new(1);
@@ -64,6 +65,9 @@ model_handle model_load_obj(core *core, const char *path, bool invert_textures_y
u32 index = model_darray_len(core->models);
model_darray_push(core->models, model);
+
+ arena_free_all(&scratch);
+ arena_free_storage(&scratch);
return (model_handle){ .raw = index };
}
diff --git a/src/std/mem.c b/src/std/mem.c
index f5b92d4..d7c0f4c 100644
--- a/src/std/mem.c
+++ b/src/std/mem.c
@@ -16,7 +16,7 @@ void* arena_alloc_align(arena* a, size_t size, size_t align) {
ERROR_EXIT("Arena ran out of memory\n");
}
void* p = a->begin + padding;
- a->begin += padding + size;
+ a->curr += padding + size;
return memset(p, 0, size);
}
void* arena_alloc(arena* a, size_t size) { return arena_alloc_align(a, size, DEFAULT_ALIGNMENT); }
@@ -29,4 +29,6 @@ arena arena_create(void* backing_buffer, size_t capacity) {
void arena_free_all(arena* a) {
a->curr = a->begin; // pop everything at once and reset to the start.
-} \ No newline at end of file
+}
+
+void arena_free_storage(arena* a) { free(a->begin); } \ No newline at end of file
diff --git a/src/std/mem.h b/src/std/mem.h
index c3ec61d..2f92894 100644
--- a/src/std/mem.h
+++ b/src/std/mem.h
@@ -22,4 +22,5 @@ arena arena_create(void* backing_buffer, size_t capacity);
void* arena_alloc(arena* a, size_t size);
void* arena_alloc_align(arena* a, size_t size, size_t align);
void arena_free_all(arena* a);
+void arena_free_storage(arena* a);
// TODO: arena_resize \ No newline at end of file
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