diff options
-rw-r--r-- | examples/transforms/ex_transforms.c | 21 | ||||
-rw-r--r-- | src/renderer/render.c | 27 | ||||
-rw-r--r-- | src/renderer/render.h | 2 | ||||
-rw-r--r-- | src/transform_hierarchy.c | 9 |
4 files changed, 47 insertions, 12 deletions
diff --git a/examples/transforms/ex_transforms.c b/examples/transforms/ex_transforms.c index a3bede7..0f1752d 100644 --- a/examples/transforms/ex_transforms.c +++ b/examples/transforms/ex_transforms.c @@ -1,10 +1,11 @@ #include <glfw3.h> #include "core.h" +#include "maths.h" +#include "maths_types.h" +#include "mem.h" #include "render.h" #include "render_types.h" -#include "maths_types.h" -#include "maths.h" #include "transform_hierarchy.h" const vec3 pointlight_positions[4] = { @@ -23,8 +24,7 @@ int main() { 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_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); @@ -62,6 +62,9 @@ int main() { 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); @@ -72,12 +75,14 @@ int main() { node1->tf.position.x += 0.002; node1->tf.is_dirty = true; - 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); + draw_scene(&frame_arena, core->models, &core->renderer, &cam, transform_tree, &our_scene); + // 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); + arena_free_all(&frame_arena); } transform_hierarchy_free(transform_tree); diff --git a/src/renderer/render.c b/src/renderer/render.c index ca625ed..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,6 +83,30 @@ void default_material_init() { texture_data_upload(&DEFAULT_MATERIAL.specular_texture); } +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; diff --git a/src/renderer/render.h b/src/renderer/render.h index 61dda75..bcdeb78 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 */ @@ -19,6 +20,7 @@ void render_frame_end(renderer* ren); void model_upload_meshes(renderer* ren, model* model); 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/transform_hierarchy.c b/src/transform_hierarchy.c index a8b3df1..a36b8ef 100644 --- a/src/transform_hierarchy.c +++ b/src/transform_hierarchy.c @@ -7,11 +7,11 @@ #include <stdlib.h> #include <string.h> +#include "core.h" #include "log.h" #include "maths.h" #include "maths_types.h" #include "render_types.h" -#include "core.h" struct transform_hierarchy { transform_node root; @@ -50,7 +50,8 @@ void transform_hierarchy_free(transform_hierarchy* 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) { +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; @@ -163,14 +164,14 @@ bool print_node(transform_node* node, void* ctx_data) { return true; } - // Grab the model + // 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; } |