summaryrefslogtreecommitdiff
path: root/gltfview/src/plugins/gltf_view.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-01-19 07:21:45 -0800
committer3gg <3gg@shellblade.net>2024-01-19 07:21:45 -0800
commit4212e57d06afac8a19b09fdebc24bad10b78f1ac (patch)
tree322b2e5cc2764ebd1855565bb8d871d9df33d704 /gltfview/src/plugins/gltf_view.c
parentfc883e0b0449509ba2e1c5d14d187feee098ab34 (diff)
Plugin refactor, moving scene from game to plugin.
Diffstat (limited to 'gltfview/src/plugins/gltf_view.c')
-rw-r--r--gltfview/src/plugins/gltf_view.c77
1 files changed, 53 insertions, 24 deletions
diff --git a/gltfview/src/plugins/gltf_view.c b/gltfview/src/plugins/gltf_view.c
index c3457a8..c19d1b8 100644
--- a/gltfview/src/plugins/gltf_view.c
+++ b/gltfview/src/plugins/gltf_view.c
@@ -1,6 +1,7 @@
1#include "gltf_view.h" 1#include "plugin.h"
2 2
3#include <gfx/renderer.h> 3#include <gfx/renderer.h>
4#include <gfx/scene.h>
4#include <gfx/util/scene.h> 5#include <gfx/util/scene.h>
5#include <gfx/util/skyquad.h> 6#include <gfx/util/skyquad.h>
6#include <gfx/util/texture.h> 7#include <gfx/util/texture.h>
@@ -23,6 +24,11 @@ static const char* GIRL =
23 24
24#define DEFAULT_SCENE_FILE GIRL 25#define DEFAULT_SCENE_FILE GIRL
25 26
27struct State {
28 Scene* scene;
29 SceneCamera* camera;
30};
31
26/// Load the skyquad texture. 32/// Load the skyquad texture.
27static Texture* load_environment_map(RenderBackend* render_backend) { 33static Texture* load_environment_map(RenderBackend* render_backend) {
28 return gfx_load_texture( 34 return gfx_load_texture(
@@ -57,15 +63,17 @@ static SceneNode* load_skyquad(RenderBackend* render_backend, SceneNode* root) {
57} 63}
58 64
59/// Load the 3D scene. 65/// Load the 3D scene.
60static SceneNode* load_scene(Game* game, const char* scene_filepath) { 66static SceneNode* load_scene(
67 Game* game, State* state, const char* scene_filepath) {
61 assert(game); 68 assert(game);
62 assert(game->gfx); 69 assert(game->gfx);
63 assert(game->scene); 70 assert(state);
71 assert(state->scene);
64 72
65 SceneNode* root = gfx_get_scene_root(game->scene); 73 SceneNode* root = gfx_get_scene_root(state->scene);
66 RenderBackend* render_backend = gfx_get_render_backend(game->gfx); 74 RenderBackend* render_backend = gfx_get_render_backend(game->gfx);
67 75
68 Camera* camera = gfx_get_camera_camera(game->camera); 76 Camera* camera = gfx_get_camera_camera(state->camera);
69 spatial3_set_position(&camera->spatial, vec3_make(0, 0, 2)); 77 spatial3_set_position(&camera->spatial, vec3_make(0, 0, 2));
70 78
71 SceneNode* sky_light_node = load_skyquad(render_backend, root); 79 SceneNode* sky_light_node = load_skyquad(render_backend, root);
@@ -85,16 +93,20 @@ static SceneNode* load_scene(Game* game, const char* scene_filepath) {
85 return scene_node; 93 return scene_node;
86} 94}
87 95
88State* init(Game* game) { 96bool init(Game* game, State** pp_state) {
89 assert(game); 97 assert(game);
90 98
91 State* state = calloc(1, sizeof(State)); 99 State* state = calloc(1, sizeof(State));
92 return state; 100 if (!state) {
93} 101 goto cleanup;
102 }
94 103
95bool boot(State* state, Game* game) { 104 if (!(state->scene = gfx_make_scene())) {
96 assert(state); 105 goto cleanup;
97 assert(game); 106 }
107 if (!(state->camera = gfx_make_camera())) {
108 goto cleanup;
109 }
98 110
99 const int argc = game->argc; 111 const int argc = game->argc;
100 const char** argv = game->argv; 112 const char** argv = game->argv;
@@ -102,27 +114,44 @@ bool boot(State* state, Game* game) {
102 // Usage: <scene file> 114 // Usage: <scene file>
103 const char* scene_filepath = argc > 1 ? argv[1] : DEFAULT_SCENE_FILE; 115 const char* scene_filepath = argc > 1 ? argv[1] : DEFAULT_SCENE_FILE;
104 116
105 SceneNode* node = load_scene(game, scene_filepath); 117 SceneNode* node = load_scene(game, state, scene_filepath);
106 if (!node) { 118 if (!node) {
107 return false; 119 goto cleanup;
108 } 120 }
109 Anima* anima = gfx_get_node_anima(node); 121 Anima* anima = gfx_get_node_anima(node);
110 gfx_play_animation( 122 gfx_play_animation(
111 anima, &(AnimationPlaySettings){.name = "Walk", .loop = true}); 123 anima, &(AnimationPlaySettings){.name = "Walk", .loop = true});
112 124
125 *pp_state = state;
113 return true; 126 return true;
127
128cleanup:
129 shutdown(game, state);
130 if (state) {
131 free(state);
132 }
133 return false;
114} 134}
115 135
116void update(State* state, Game* game, double t, double dt) { 136void shutdown(Game* game, State* state) {
117 assert(state); 137 assert(game);
138 if (state) {
139 gfx_destroy_camera(&state->camera);
140 gfx_destroy_scene(&state->scene);
141 // State freed by plugin engine.
142 }
143}
144
145void update(Game* game, State* state, double t, double dt) {
118 assert(game); 146 assert(game);
119 assert(game->scene); 147 assert(state);
120 assert(game->camera); 148 assert(state->scene);
149 assert(state->camera);
121 150
122 gfx_animate_scene(game->scene, (R)t); 151 gfx_animate_scene(state->scene, (R)t);
123 152
124 const vec3 orbit_point = vec3_make(0, 2, 0); 153 const vec3 orbit_point = vec3_make(0, 2, 0);
125 Camera* camera = gfx_get_camera_camera(game->camera); 154 Camera* camera = gfx_get_camera_camera(state->camera);
126 spatial3_orbit( 155 spatial3_orbit(
127 &camera->spatial, orbit_point, 156 &camera->spatial, orbit_point,
128 /*radius=*/2.5, 157 /*radius=*/2.5,
@@ -150,18 +179,18 @@ static void render_bounding_boxes(ImmRenderer* imm, const SceneNode* node) {
150 } 179 }
151} 180}
152 181
153void render(State* state, const Game* game) { 182void render(const Game* game, const State* state) {
154 assert(state); 183 assert(state);
155 assert(game); 184 assert(game);
156 assert(game->gfx); 185 assert(game->gfx);
157 assert(game->scene); 186 assert(state->scene);
158 assert(game->camera); 187 assert(state->camera);
159 188
160 ImmRenderer* imm = gfx_get_imm_renderer(game->gfx); 189 ImmRenderer* imm = gfx_get_imm_renderer(game->gfx);
161 assert(imm); 190 assert(imm);
162 gfx_imm_start(imm); 191 gfx_imm_start(imm);
163 gfx_imm_set_camera(imm, gfx_get_camera_camera(game->camera)); 192 gfx_imm_set_camera(imm, gfx_get_camera_camera(state->camera));
164 gfx_imm_set_colour(imm, vec4_make(0.2, 0.2, 1.0, 0.3)); 193 gfx_imm_set_colour(imm, vec4_make(0.2, 0.2, 1.0, 0.3));
165 render_bounding_boxes(imm, gfx_get_scene_root(game->scene)); 194 render_bounding_boxes(imm, gfx_get_scene_root(state->scene));
166 gfx_imm_end(imm); 195 gfx_imm_end(imm);
167} 196}