diff options
| author | 3gg <3gg@shellblade.net> | 2024-01-19 07:21:45 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2024-01-19 07:21:45 -0800 |
| commit | 4212e57d06afac8a19b09fdebc24bad10b78f1ac (patch) | |
| tree | 322b2e5cc2764ebd1855565bb8d871d9df33d704 /gltfview/src/plugins/texture_view.c | |
| parent | fc883e0b0449509ba2e1c5d14d187feee098ab34 (diff) | |
Plugin refactor, moving scene from game to plugin.
Diffstat (limited to 'gltfview/src/plugins/texture_view.c')
| -rw-r--r-- | gltfview/src/plugins/texture_view.c | 91 |
1 files changed, 82 insertions, 9 deletions
diff --git a/gltfview/src/plugins/texture_view.c b/gltfview/src/plugins/texture_view.c index f16c8d1..b424158 100644 --- a/gltfview/src/plugins/texture_view.c +++ b/gltfview/src/plugins/texture_view.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #include "texture_view.h" | 1 | #include "plugin.h" |
| 2 | 2 | ||
| 3 | #include <gfx/render_backend.h> | 3 | #include <gfx/render_backend.h> |
| 4 | #include <gfx/renderer.h> | ||
| 4 | #include <gfx/scene.h> | 5 | #include <gfx/scene.h> |
| 5 | #include <gfx/util/geometry.h> | 6 | #include <gfx/util/geometry.h> |
| 6 | #include <gfx/util/shader.h> | 7 | #include <gfx/util/shader.h> |
| @@ -9,13 +10,25 @@ | |||
| 9 | #include <math/camera.h> | 10 | #include <math/camera.h> |
| 10 | 11 | ||
| 11 | #include <assert.h> | 12 | #include <assert.h> |
| 13 | #include <stdlib.h> | ||
| 12 | 14 | ||
| 13 | // Default texture to load if no texture is provided. | 15 | // Default texture to load if no texture is provided. |
| 14 | static const char* DEFAULT_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp"; | 16 | static const char* DEFAULT_TEXTURE = "/assets/skybox/clouds1/clouds1_west.bmp"; |
| 15 | // static const char* DEFAULT_TEXTURE = "/assets/checkerboard.jpg"; | 17 | // static const char* DEFAULT_TEXTURE = "/assets/checkerboard.jpg"; |
| 16 | 18 | ||
| 17 | bool boot(State* state, Game* game) { | 19 | struct State { |
| 20 | Scene* scene; | ||
| 21 | SceneCamera* camera; | ||
| 22 | }; | ||
| 23 | |||
| 24 | bool init(Game* game, State** pp_state) { | ||
| 18 | assert(game); | 25 | assert(game); |
| 26 | assert(pp_state); | ||
| 27 | |||
| 28 | State* state = calloc(1, sizeof(State)); | ||
| 29 | if (!state) { | ||
| 30 | goto cleanup; | ||
| 31 | } | ||
| 19 | 32 | ||
| 20 | // Usage: [texture file] | 33 | // Usage: [texture file] |
| 21 | const char* texture_file = game->argc > 1 ? game->argv[1] : DEFAULT_TEXTURE; | 34 | const char* texture_file = game->argc > 1 ? game->argv[1] : DEFAULT_TEXTURE; |
| @@ -30,17 +43,17 @@ bool boot(State* state, Game* game) { | |||
| 30 | .mipmaps = false, | 43 | .mipmaps = false, |
| 31 | .data.texture.filepath = mstring_make(texture_file)}); | 44 | .data.texture.filepath = mstring_make(texture_file)}); |
| 32 | if (!texture) { | 45 | if (!texture) { |
| 33 | return false; | 46 | goto cleanup; |
| 34 | } | 47 | } |
| 35 | 48 | ||
| 36 | ShaderProgram* shader = gfx_make_view_texture_shader(render_backend); | 49 | ShaderProgram* shader = gfx_make_view_texture_shader(render_backend); |
| 37 | if (!shader) { | 50 | if (!shader) { |
| 38 | return false; | 51 | goto cleanup; |
| 39 | } | 52 | } |
| 40 | 53 | ||
| 41 | Geometry* geometry = gfx_make_quad_11(render_backend); | 54 | Geometry* geometry = gfx_make_quad_11(render_backend); |
| 42 | if (!geometry) { | 55 | if (!geometry) { |
| 43 | return false; | 56 | goto cleanup; |
| 44 | } | 57 | } |
| 45 | 58 | ||
| 46 | MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1}; | 59 | MaterialDesc material_desc = (MaterialDesc){.num_uniforms = 1}; |
| @@ -50,25 +63,85 @@ bool boot(State* state, Game* game) { | |||
| 50 | .name = sstring_make("Texture")}; | 63 | .name = sstring_make("Texture")}; |
| 51 | Material* material = gfx_make_material(&material_desc); | 64 | Material* material = gfx_make_material(&material_desc); |
| 52 | if (!material) { | 65 | if (!material) { |
| 53 | return false; | 66 | goto cleanup; |
| 54 | } | 67 | } |
| 55 | 68 | ||
| 56 | const MeshDesc mesh_desc = | 69 | const MeshDesc mesh_desc = |
| 57 | (MeshDesc){.geometry = geometry, .material = material, .shader = shader}; | 70 | (MeshDesc){.geometry = geometry, .material = material, .shader = shader}; |
| 58 | Mesh* mesh = gfx_make_mesh(&mesh_desc); | 71 | Mesh* mesh = gfx_make_mesh(&mesh_desc); |
| 59 | if (!mesh) { | 72 | if (!mesh) { |
| 60 | return false; | 73 | goto cleanup; |
| 61 | } | 74 | } |
| 62 | 75 | ||
| 63 | SceneObject* object = gfx_make_object(); | 76 | SceneObject* object = gfx_make_object(); |
| 64 | if (!object) { | 77 | if (!object) { |
| 65 | return false; | 78 | goto cleanup; |
| 66 | } | 79 | } |
| 67 | gfx_add_object_mesh(object, mesh); | 80 | gfx_add_object_mesh(object, mesh); |
| 68 | 81 | ||
| 82 | if (!(state->scene = gfx_make_scene())) { | ||
| 83 | goto cleanup; | ||
| 84 | } | ||
| 85 | |||
| 69 | SceneNode* node = gfx_make_object_node(object); | 86 | SceneNode* node = gfx_make_object_node(object); |
| 70 | SceneNode* root = gfx_get_scene_root(game->scene); | 87 | if (!node) { |
| 88 | goto cleanup; | ||
| 89 | } | ||
| 90 | SceneNode* root = gfx_get_scene_root(state->scene); | ||
| 91 | if (!root) { | ||
| 92 | goto cleanup; | ||
| 93 | } | ||
| 71 | gfx_set_node_parent(node, root); | 94 | gfx_set_node_parent(node, root); |
| 72 | 95 | ||
| 96 | if (!(state->camera = gfx_make_camera())) { | ||
| 97 | goto cleanup; | ||
| 98 | } | ||
| 99 | |||
| 100 | *pp_state = state; | ||
| 73 | return true; | 101 | return true; |
| 102 | |||
| 103 | cleanup: | ||
| 104 | shutdown(game, state); | ||
| 105 | if (state) { | ||
| 106 | free(state); | ||
| 107 | } | ||
| 108 | return false; | ||
| 109 | } | ||
| 110 | |||
| 111 | void shutdown(Game* game, State* state) { | ||
| 112 | assert(game); | ||
| 113 | if (state) { | ||
| 114 | gfx_destroy_camera(&state->camera); | ||
| 115 | gfx_destroy_scene(&state->scene); | ||
| 116 | // State freed by plugin engine. | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | void render(const Game* game, const State* state) { | ||
| 121 | assert(game); | ||
| 122 | assert(state); | ||
| 123 | |||
| 124 | Renderer* renderer = gfx_get_renderer(game->gfx); | ||
| 125 | gfx_render_scene( | ||
| 126 | renderer, &(RenderSceneParams){ | ||
| 127 | .mode = RenderDefault, | ||
| 128 | .scene = state->scene, | ||
| 129 | .camera = state->camera}); | ||
| 130 | } | ||
| 131 | |||
| 132 | void resize(Game* game, State* state, int width, int height) { | ||
| 133 | assert(game); | ||
| 134 | assert(state); | ||
| 135 | |||
| 136 | RenderBackend* render_backend = gfx_get_render_backend(game->gfx); | ||
| 137 | gfx_set_viewport(render_backend, width, height); | ||
| 138 | |||
| 139 | const R fovy = 90 * TO_RAD; | ||
| 140 | const R aspect = (R)width / (R)height; | ||
| 141 | const R near = 0.1; | ||
| 142 | const R far = 1000; | ||
| 143 | const mat4 projection = mat4_perspective(fovy, aspect, near, far); | ||
| 144 | |||
| 145 | Camera* camera = gfx_get_camera_camera(state->camera); | ||
| 146 | camera->projection = projection; | ||
| 74 | } | 147 | } |
