summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-03-17 15:39:40 +1100
committeromniscient <17525998+omnisci3nce@users.noreply.github.com>2024-03-17 15:39:40 +1100
commitcbcd40391c445afb836217a64e0bd96bc54cb805 (patch)
treee8c3656283af96394432b61f07fb1284fec486e2 /src
parent31359e468c4f1d844f19862f63ed911b66b98068 (diff)
rendering using transforms
Diffstat (limited to 'src')
-rw-r--r--src/maths/maths.h2
-rw-r--r--src/renderer/render.c16
-rw-r--r--src/renderer/render.h4
-rw-r--r--src/transform_hierarchy.c43
-rw-r--r--src/transform_hierarchy.h4
5 files changed, 44 insertions, 25 deletions
diff --git a/src/maths/maths.h b/src/maths/maths.h
index 390b611..6834f69 100644
--- a/src/maths/maths.h
+++ b/src/maths/maths.h
@@ -203,7 +203,7 @@ 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) {
diff --git a/src/renderer/render.c b/src/renderer/render.c
index 7884db6..ca625ed 100644
--- a/src/renderer/render.c
+++ b/src/renderer/render.c
@@ -80,7 +80,7 @@ void default_material_init() {
texture_data_upload(&DEFAULT_MATERIAL.specular_texture);
}
-void draw_model(renderer* ren, camera* camera, model* model, transform tf, scene* scene) {
+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 +103,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 +119,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..61dda75 100644
--- a/src/renderer/render.h
+++ b/src/renderer/render.h
@@ -17,8 +17,8 @@ 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);
// ---
texture texture_data_load(const char* path, bool invert_y); // #frontend
diff --git a/src/transform_hierarchy.c b/src/transform_hierarchy.c
index 65d4edf..a8b3df1 100644
--- a/src/transform_hierarchy.c
+++ b/src/transform_hierarchy.c
@@ -18,7 +18,7 @@ struct transform_hierarchy {
};
transform_hierarchy* transform_hierarchy_create() {
- transform_hierarchy* tfh = malloc(sizeof(transform_hierarchy));
+ transform_hierarchy* tfh = malloc(sizeof(struct transform_hierarchy));
tfh->root = (transform_node){ .model = { ABSENT_MODEL_HANDLE },
.tf = TRANSFORM_DEFAULT,
@@ -28,7 +28,6 @@ transform_hierarchy* transform_hierarchy_create() {
.children = { 0 },
.n_children = 0,
.tfh = tfh };
-
return tfh;
}
@@ -51,10 +50,10 @@ void transform_hierarchy_free(transform_hierarchy* 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) {
+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;
+ return NULL;
}
transform_node* node = malloc(sizeof(transform_node));
node->model = model;
@@ -75,6 +74,8 @@ void transform_hierarchy_add_node(transform_node* parent, model_handle model, tr
parent->children[next_index] = node;
parent->n_children++;
}
+
+ return node;
}
void transform_hierarchy_delete_node(transform_node* node) {
@@ -124,7 +125,7 @@ void transform_hierarchy_dfs(transform_node* start_node,
bool update_matrix(transform_node* node, void* _ctx_data) {
if (!node) return true; // leaf node
- if (node->parent->tf.is_dirty) {
+ if (node->parent && node->parent->tf.is_dirty) {
node->tf.is_dirty = true;
}
@@ -147,17 +148,35 @@ void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh) {
transform_hierarchy_dfs(&tfh->root, update_matrix, false, NULL);
}
-void print_node(transform_node* node, void* _ctx_data) {
- // Grab the model
- model m = core->models->data[start_node->model.raw];
- printf("Node %s\n", m.name.buf);
-}
-
struct print_ctx {
core* core;
u32 indentation_lvl;
};
-void transform_hierarchy_debug_print(transform_node* start_node, core* core) {
+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);
} \ No newline at end of file
diff --git a/src/transform_hierarchy.h b/src/transform_hierarchy.h
index 61989f1..9598fe9 100644
--- a/src/transform_hierarchy.h
+++ b/src/transform_hierarchy.h
@@ -45,7 +45,7 @@ void transform_hierarchy_propagate_transforms(transform_hierarchy* tfh);
transform_node* transform_hierarchy_root_node(transform_hierarchy* tfh);
// --- Mutations
-void transform_hierarchy_add_node(transform_node* parent, model_handle model, transform tf);
+transform_node* transform_hierarchy_add_node(transform_node* parent, model_handle model, transform tf);
void transform_hierarchy_delete_node(transform_node* node);
// --- Traversal
@@ -65,4 +65,4 @@ void transform_hierarchy_delete_node(transform_node* 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);
struct core;
-void transform_hierarchy_debug_print(transform_node* start_nod, struct core* core); \ No newline at end of file
+void transform_hierarchy_debug_print(transform_node* start_node, struct core* core); \ No newline at end of file