From 616c2a5903c9da722f4c06607da2705e7bc6a282 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 21 Jun 2023 08:53:09 -0700 Subject: Make camera optional in the renderer and move start/end frame calls outside so that it can be used along the imm renderer. --- gfx/src/renderer/renderer.c | 26 ++++++++++++++++---------- gltfview/src/game.c | 7 ++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/gfx/src/renderer/renderer.c b/gfx/src/renderer/renderer.c index bddde90..ac6a45f 100644 --- a/gfx/src/renderer/renderer.c +++ b/gfx/src/renderer/renderer.c @@ -264,7 +264,10 @@ static void draw_recursively( gfx_set_mat4_uniform(shader, "CameraRotation", state->camera_rotation); gfx_set_float_uniform(shader, "Fovy", state->fovy); gfx_set_float_uniform(shader, "Aspect", state->aspect); - gfx_set_vec3_uniform(shader, "CameraPosition", state->camera->spatial.p); + if (state->camera) { + gfx_set_vec3_uniform( + shader, "CameraPosition", state->camera->spatial.p); + } if (state->num_joints > 0) { gfx_set_mat4_array_uniform( shader, "JointMatrices", state->joint_matrices, state->num_joints); @@ -309,7 +312,6 @@ void gfx_render_scene(Renderer* renderer, const RenderSceneParams* params) { assert(renderer); assert(params); assert(params->scene); - assert(params->camera); ShaderProgram* const shader = load_shader(renderer, params->mode); @@ -318,11 +320,17 @@ void gfx_render_scene(Renderer* renderer, const RenderSceneParams* params) { RenderBackend* render_backend = renderer->render_backend; - const mat4 projection = camera ? camera->camera.projection : mat4_id(); - const mat4 camera_rotation = - mat4_rotation(spatial3_transform(&camera->camera.spatial)); - const mat4 view_matrix = - camera ? spatial3_inverse_transform(&camera->camera.spatial) : mat4_id(); + mat4 projection, camera_rotation, view_matrix; + if (camera) { + projection = camera->camera.projection; + camera_rotation = + mat4_rotation(spatial3_transform(&camera->camera.spatial)); + view_matrix = spatial3_inverse_transform(&camera->camera.spatial); + } else { + projection = mat4_id(); + camera_rotation = mat4_id(); + view_matrix = mat4_id(); + } int width, height; gfx_get_viewport(render_backend, &width, &height); @@ -341,8 +349,6 @@ void gfx_render_scene(Renderer* renderer, const RenderSceneParams* params) { // Assuming a perspective matrix. .fovy = atan(1.0 / (mat4_at(projection, 1, 1))) * 2, .aspect = aspect}; - - gfx_start_frame(render_backend); + draw_recursively(&state, mat4_id(), scene->root); - gfx_end_frame(render_backend); } diff --git a/gltfview/src/game.c b/gltfview/src/game.c index d618447..5ca9ad4 100644 --- a/gltfview/src/game.c +++ b/gltfview/src/game.c @@ -130,7 +130,10 @@ void game_update(Game* game, double t, double dt) { } void game_render(const Game* game) { - Renderer* renderer = gfx_get_renderer(game->gfx); + RenderBackend* render_backend = gfx_get_render_backend(game->gfx); + Renderer* renderer = gfx_get_renderer(game->gfx); + + gfx_start_frame(render_backend); gfx_render_scene( renderer, @@ -142,6 +145,8 @@ void game_render(const Game* game) { void* plugin_state = get_plugin_state(game->plugin); plugin_call(game->plugin, plugin_render, "render", plugin_state, game); } + + gfx_end_frame(render_backend); } void game_set_viewport(Game* game, int width, int height) { -- cgit v1.2.3