summaryrefslogtreecommitdiff
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
parent31359e468c4f1d844f19862f63ed911b66b98068 (diff)
rendering using transforms
-rw-r--r--examples/transforms/ex_transforms.c49
-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
6 files changed, 86 insertions, 32 deletions
diff --git a/examples/transforms/ex_transforms.c b/examples/transforms/ex_transforms.c
index edb1c21..b11fbc7 100644
--- a/examples/transforms/ex_transforms.c
+++ b/examples/transforms/ex_transforms.c
@@ -7,6 +7,14 @@
#include "maths.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();
@@ -18,20 +26,43 @@ int main() {
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);
+ 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(-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);
+ transform cube1 = transform_create(vec3(1.0, 1.0, 0.0), quat_ident(), 2.0);
+ transform cube2 = transform_create(vec3(1.0, -1.0, 0.0), quat_ident(), 2.0);
+ transform cube3 = transform_create(vec3(0.0, 1.0, 0.0), quat_ident(), 2.0);
+ transform cube4 = transform_create(vec3(0.0, -1.0, 0.0), quat_ident(), 2.0);
+ transform_node* node1 = transform_hierarchy_add_node(root_node, cube_handle, cube1);
+ transform_node* node2 = transform_hierarchy_add_node(root_node, cube_handle, cube2);
+
+ transform_node* node3 = transform_hierarchy_add_node(node1, cube_handle, cube3);
+ transform_node* node4 = transform_hierarchy_add_node(node2, cube_handle, cube4);
+ transform_hierarchy_debug_print(root_node, core);
+
// Main loop
while (!glfwWindowShouldClose(core->renderer.window)) {
@@ -39,8 +70,12 @@ int main() {
threadpool_process_results(&core->threadpool, 1);
render_frame_begin(&core->renderer);
+ transform_hierarchy_propagate_transforms(transform_tree);
- // insert work here
+ draw_model(&core->renderer, &cam, cube, &node1->world_matrix_tf, &our_scene);
+ draw_model(&core->renderer, &cam, cube, &node2->world_matrix_tf, &our_scene);
+ draw_model(&core->renderer, &cam, cube, &node3->world_matrix_tf, &our_scene);
+ draw_model(&core->renderer, &cam, cube, &node4->world_matrix_tf, &our_scene);
render_frame_end(&core->renderer);
}
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