From c593c24b62b274fbc1d465f0386a0c5d32423f4f Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 12 Feb 2024 17:42:57 -0800 Subject: Initial implementation for an asset cache. --- game/src/plugins/viewer.c | 69 +++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 26 deletions(-) (limited to 'game') diff --git a/game/src/plugins/viewer.c b/game/src/plugins/viewer.c index 83fc8ed..1ed3b9d 100644 --- a/game/src/plugins/viewer.c +++ b/game/src/plugins/viewer.c @@ -30,31 +30,33 @@ struct State { }; /// Load the skyquad texture. -static Texture* load_environment_map(RenderBackend* render_backend) { +static Texture* load_environment_map(Gfx* gfx) { + assert(gfx); return gfx_load_texture( - render_backend, - &(LoadTextureCmd){ - .origin = TextureFromFile, - .type = LoadCubemap, - .colour_space = sRGB, - .filtering = NearestFiltering, - .mipmaps = false, - .data.cubemap.filepaths = { - mstring_make("/assets/skybox/clouds1/clouds1_east.bmp"), - mstring_make("/assets/skybox/clouds1/clouds1_west.bmp"), - mstring_make("/assets/skybox/clouds1/clouds1_up.bmp"), - mstring_make("/assets/skybox/clouds1/clouds1_down.bmp"), - mstring_make("/assets/skybox/clouds1/clouds1_south.bmp"), - mstring_make("/assets/skybox/clouds1/clouds1_north.bmp")} + gfx, &(LoadTextureCmd){ + .origin = AssetFromFile, + .type = LoadCubemap, + .colour_space = sRGB, + .filtering = NearestFiltering, + .mipmaps = false, + .data.cubemap.filepaths = { + mstring_make("/assets/skybox/clouds1/clouds1_east.bmp"), + mstring_make("/assets/skybox/clouds1/clouds1_west.bmp"), + mstring_make("/assets/skybox/clouds1/clouds1_up.bmp"), + mstring_make("/assets/skybox/clouds1/clouds1_down.bmp"), + mstring_make("/assets/skybox/clouds1/clouds1_south.bmp"), + mstring_make("/assets/skybox/clouds1/clouds1_north.bmp")} }); } /// Load the skyquad and return the environment light node. -static SceneNode* load_skyquad(RenderBackend* render_backend, SceneNode* root) { - assert(render_backend); +static SceneNode* load_skyquad(Gfx* gfx, SceneNode* root) { + assert(gfx); assert(root); - Texture* environment_map = load_environment_map(render_backend); + RenderBackend* render_backend = gfx_get_render_backend(gfx); + + Texture* environment_map = load_environment_map(gfx); if (!environment_map) { return 0; } @@ -70,20 +72,19 @@ static SceneNode* load_scene( assert(state); assert(state->scene); - SceneNode* root = gfx_get_scene_root(state->scene); - RenderBackend* render_backend = gfx_get_render_backend(game->gfx); - Camera* camera = gfx_get_camera_camera(state->camera); spatial3_set_position(&camera->spatial, vec3_make(0, 0, 2)); - SceneNode* sky_light_node = load_skyquad(render_backend, root); + SceneNode* root = gfx_get_scene_root(state->scene); + SceneNode* sky_light_node = load_skyquad(game->gfx, root); if (!sky_light_node) { - return 0; + return 0; // test } SceneNode* scene_node = gfx_load_scene( game->gfx, sky_light_node, - &(LoadSceneCmd){.origin = SceneFromFile, .filepath = scene_filepath}); + &(LoadSceneCmd){ + .origin = AssetFromFile, .filepath = mstring_make(scene_filepath)}); if (!scene_node) { return 0; } @@ -136,6 +137,10 @@ cleanup: void shutdown(Game* game, State* state) { assert(game); if (state) { + // TODO: Destroying the scene here currently does not play well with asset + // reloading. The issue is that we expect to mutate the scene/model during + // animation. This needs to change if we want to be able to cache assets + // in memory. gfx_destroy_camera(&state->camera); gfx_destroy_scene(&state->scene); // State freed by plugin engine. @@ -165,8 +170,20 @@ static void render_bounding_boxes_rec(ImmRenderer* imm, const SceneNode* node) { assert(node); if (gfx_get_node_type(node) == ObjectNode) { // TODO: Look at the scene log. The JointNodes are detached from the - // ObjectNodes. This is why the boxes are not being transformed as expected - // here. Anima needs to animate boxes? Use OOBB in addition to AABB? + // ObjectNodes. This is why the boxes are not being transformed as expected + // here. Anima needs to animate boxes? Use OOBB in addition to AABB? + // + // TODO: Idea: when a model is loaded, compute an OOBB per joint using the + // vertices that are affected by the joint. Then transform this OOBB when + // animating the skeleton. Start with AABB for simplicity. The AABB/OOBB + // in the skeleton should be const. The transform AABB/OOBB is derived + // on demand. Stack allocator would be best for this kind of per-frame + // data. + // + // TODO: After computing joint AABB/OOBBs, check here whether the node has + // a skeleton, and if so, render the skeleton's boxes instead of the + // node's (the node's boxes are not animated, but computer from the rest + // pose). const mat4 model = gfx_get_node_global_transform(node); const SceneObject* obj = gfx_get_node_object(node); const aabb3 box = gfx_calc_object_aabb(obj); -- cgit v1.2.3