From 27ff505b6daaf5b0ec5f6af422f727a032f83c6b Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 4 Jan 2023 15:46:22 -0800 Subject: Move ShaderProgram from Material to Mesh in preparation for shader permutations. --- gltfview/src/game.c | 104 +++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 50 deletions(-) (limited to 'gltfview/src/game.c') diff --git a/gltfview/src/game.c b/gltfview/src/game.c index d7352d4..f2e5a88 100644 --- a/gltfview/src/game.c +++ b/gltfview/src/game.c @@ -24,7 +24,7 @@ #include // usleep; TODO Remove. // Paths to various scene files. -static const char* BOX = "/assets/models/box.gltf"; +static const char* BOX = "/assets/models/box.gltf"; static const char* SUZANNE = "/assets/models/suzanne.gltf"; static const char* SPONZA = "/assets/glTF-Sample-Models/2.0/Sponza/glTF/Sponza.gltf"; @@ -37,8 +37,8 @@ static const char* DAMAGED_HELMET = static const char* CLOUDS1_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp"; -static ShaderProgram* load_shader(RenderBackend* render_backend, - const char* view_mode) { +static ShaderProgram* load_shader( + RenderBackend* render_backend, const char* view_mode) { ShaderProgram* shader = 0; if (strcmp(view_mode, "debug") == 0) { shader = gfx_make_debug3d_shader(render_backend); @@ -59,23 +59,24 @@ static Texture* load_environment_map(RenderBackend* render_backend) { return gfx_load_texture( render_backend, &(LoadTextureCmd){ - .origin = TextureFromFile, - .type = LoadCubemap, - .colour_space = sRGB, - .filtering = NearestFiltering, - .mipmaps = false, + .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_north.bmp"), - mstring_make("/assets/skybox/clouds1/clouds1_south.bmp")}}); + 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_north.bmp"), + mstring_make("/assets/skybox/clouds1/clouds1_south.bmp")} + }); } /// Creates an object to render the skyquad in the background. -static SceneNode* make_skyquad_object_node(Game* game, - const Texture* environment_map) { +static SceneNode* make_skyquad_object_node( + Game* game, const Texture* environment_map) { assert(game); SceneObject* skyquad_object = @@ -92,12 +93,12 @@ static SceneNode* make_skyquad_object_node(Game* game, } /// Creates an environment light. -static SceneNode* make_environment_light(Game* game, - const Texture* environment_light) { +static SceneNode* make_environment_light( + Game* game, const Texture* environment_light) { assert(game); Light* light = gfx_make_light(&(LightDesc){ - .type = EnvironmentLightType, + .type = EnvironmentLightType, .light = (EnvironmentLightDesc){.environment_map = environment_light}}); if (!light) { return 0; @@ -127,8 +128,8 @@ static bool load_skyquad(Game* game, SceneNode** node) { } /// Loads the 3D scene. -static bool load_scene(Game* game, const char* scene_filepath, - const char* view_mode) { +static bool load_scene( + Game* game, const char* scene_filepath, const char* view_mode) { assert(game); game->camera = gfx_make_camera(); @@ -151,9 +152,10 @@ static bool load_scene(Game* game, const char* scene_filepath, return false; } - if (!gfx_load_scene(game->gfx, sky_node, shader, - &(LoadSceneCmd){.origin = SceneFromFile, - .filepath = scene_filepath})) { + if (!gfx_load_scene( + game->gfx, sky_node, shader, + &(LoadSceneCmd){ + .origin = SceneFromFile, .filepath = scene_filepath})) { return false; } @@ -164,14 +166,14 @@ static bool load_scene(Game* game, const char* scene_filepath, static bool load_texture_debugger_scene(Game* game) { assert(game); - Texture* texture = - gfx_load_texture(game->render_backend, - &(LoadTextureCmd){.origin = TextureFromFile, - .type = LoadTexture, - .filtering = LinearFiltering, - .mipmaps = false, - .data.texture.filepath = - mstring_make(CLOUDS1_TEXTURE)}); + Texture* texture = gfx_load_texture( + game->render_backend, + &(LoadTextureCmd){ + .origin = TextureFromFile, + .type = LoadTexture, + .filtering = LinearFiltering, + .mipmaps = false, + .data.texture.filepath = mstring_make(CLOUDS1_TEXTURE)}); game->camera = gfx_make_camera(); if (!game->camera) { @@ -191,12 +193,12 @@ static bool load_texture_debugger_scene(Game* game) { } MaterialDesc material_desc = (MaterialDesc){0}; - material_desc.shader = shader; - material_desc.uniforms[0] = (ShaderUniform){.type = UniformTexture, - .value.texture = texture, - .name = sstring_make("Texture")}; + material_desc.uniforms[0] = (ShaderUniform){ + .type = UniformTexture, + .value.texture = texture, + .name = sstring_make("Texture")}; material_desc.num_uniforms = 1; - Material* material = gfx_make_material(&material_desc); + Material* material = gfx_make_material(&material_desc); if (!material) { return false; } @@ -204,7 +206,8 @@ static bool load_texture_debugger_scene(Game* game) { MeshDesc mesh_desc = (MeshDesc){0}; mesh_desc.geometry = geometry; mesh_desc.material = material; - Mesh* mesh = gfx_make_mesh(&mesh_desc); + mesh_desc.shader = shader; + Mesh* mesh = gfx_make_mesh(&mesh_desc); if (!mesh) { return false; } @@ -224,7 +227,7 @@ static bool load_texture_debugger_scene(Game* game) { bool game_new(Game* game, int argc, const char** argv) { // TODO: getopt() to implement proper argument parsing. - const char* view_mode = argc > 1 ? argv[1] : ""; + const char* view_mode = argc > 1 ? argv[1] : ""; const char* scene_filepath = argc > 2 ? argv[2] : DEFAULT_SCENE_FILE; game->gfx = gfx_init(); @@ -233,7 +236,7 @@ bool game_new(Game* game, int argc, const char** argv) { } game->render_backend = gfx_get_render_backend(game->gfx); - game->renderer = gfx_get_renderer(game->gfx); + game->renderer = gfx_get_renderer(game->gfx); game->scene = gfx_make_scene(game->gfx); if (!game->scene) { @@ -270,26 +273,27 @@ void game_update(Game* game, double t, double dt) { game->elapsed -= 1.0; } Camera* camera = gfx_get_camera_camera(game->camera); - spatial3_orbit(&camera->spatial, vec3_make(0, 0, 0), - /*radius=*/2, - /*azimuth=*/t * 0.5, /*zenith=*/0); + spatial3_orbit( + &camera->spatial, vec3_make(0, 0, 0), + /*radius=*/2, + /*azimuth=*/t * 0.5, /*zenith=*/0); spatial3_lookat(&camera->spatial, vec3_make(0, 0, 0)); } void game_render(const Game* game) { - gfx_render_scene(game->renderer, game->render_backend, game->scene, - game->camera); + gfx_render_scene( + game->renderer, game->render_backend, game->scene, game->camera); } void game_set_viewport(Game* game, int width, int height) { gfx_set_viewport(game->render_backend, width, height); - const R fovy = 90 * TO_RAD; - const R aspect = (R)width / (R)height; - const R near = 0.1; - const R far = 1000; + const R fovy = 90 * TO_RAD; + const R aspect = (R)width / (R)height; + const R near = 0.1; + const R far = 1000; const mat4 projection = mat4_perspective(fovy, aspect, near, far); - Camera* camera = gfx_get_camera_camera(game->camera); + Camera* camera = gfx_get_camera_camera(game->camera); camera->projection = projection; } -- cgit v1.2.3