diff options
Diffstat (limited to 'gltfview/src/plugins/gltf_view.c')
| -rw-r--r-- | gltfview/src/plugins/gltf_view.c | 196 |
1 files changed, 0 insertions, 196 deletions
diff --git a/gltfview/src/plugins/gltf_view.c b/gltfview/src/plugins/gltf_view.c deleted file mode 100644 index c19d1b8..0000000 --- a/gltfview/src/plugins/gltf_view.c +++ /dev/null | |||
| @@ -1,196 +0,0 @@ | |||
| 1 | #include "plugin.h" | ||
| 2 | |||
| 3 | #include <gfx/renderer.h> | ||
| 4 | #include <gfx/scene.h> | ||
| 5 | #include <gfx/util/scene.h> | ||
| 6 | #include <gfx/util/skyquad.h> | ||
| 7 | #include <gfx/util/texture.h> | ||
| 8 | #include <math/camera.h> | ||
| 9 | #include <math/spatial3.h> | ||
| 10 | |||
| 11 | #include <stdlib.h> | ||
| 12 | |||
| 13 | // Paths to various scene files. | ||
| 14 | static const char* BOX = "/assets/models/box.gltf"; | ||
| 15 | static const char* SUZANNE = "/assets/models/suzanne.gltf"; | ||
| 16 | static const char* SPONZA = | ||
| 17 | "/assets/glTF-Sample-Models/2.0/Sponza/glTF/Sponza.gltf"; | ||
| 18 | static const char* FLIGHT_HELMET = | ||
| 19 | "/assets/glTF-Sample-Models/2.0/FlightHelmet/glTF/FlightHelmet.gltf"; | ||
| 20 | static const char* DAMAGED_HELMET = | ||
| 21 | "/assets/glTF-Sample-Models/2.0/DamagedHelmet/glTF/DamagedHelmet.gltf"; | ||
| 22 | static const char* GIRL = | ||
| 23 | "/home/jeanne/Nextcloud/assets/models/girl/girl-with-ground.gltf"; | ||
| 24 | |||
| 25 | #define DEFAULT_SCENE_FILE GIRL | ||
| 26 | |||
| 27 | struct State { | ||
| 28 | Scene* scene; | ||
| 29 | SceneCamera* camera; | ||
| 30 | }; | ||
| 31 | |||
| 32 | /// Load the skyquad texture. | ||
| 33 | static Texture* load_environment_map(RenderBackend* render_backend) { | ||
| 34 | return gfx_load_texture( | ||
| 35 | render_backend, | ||
| 36 | &(LoadTextureCmd){ | ||
| 37 | .origin = TextureFromFile, | ||
| 38 | .type = LoadCubemap, | ||
| 39 | .colour_space = sRGB, | ||
| 40 | .filtering = NearestFiltering, | ||
| 41 | .mipmaps = false, | ||
| 42 | .data.cubemap.filepaths = { | ||
| 43 | mstring_make("/assets/skybox/clouds1/clouds1_east.bmp"), | ||
| 44 | mstring_make("/assets/skybox/clouds1/clouds1_west.bmp"), | ||
| 45 | mstring_make("/assets/skybox/clouds1/clouds1_up.bmp"), | ||
| 46 | mstring_make("/assets/skybox/clouds1/clouds1_down.bmp"), | ||
| 47 | mstring_make("/assets/skybox/clouds1/clouds1_south.bmp"), | ||
| 48 | mstring_make("/assets/skybox/clouds1/clouds1_north.bmp")} | ||
| 49 | }); | ||
| 50 | } | ||
| 51 | |||
| 52 | /// Load the skyquad and return the environment light node. | ||
| 53 | static SceneNode* load_skyquad(RenderBackend* render_backend, SceneNode* root) { | ||
| 54 | assert(render_backend); | ||
| 55 | assert(root); | ||
| 56 | |||
| 57 | Texture* environment_map = load_environment_map(render_backend); | ||
| 58 | if (!environment_map) { | ||
| 59 | return 0; | ||
| 60 | } | ||
| 61 | |||
| 62 | return gfx_setup_skyquad(render_backend, root, environment_map); | ||
| 63 | } | ||
| 64 | |||
| 65 | /// Load the 3D scene. | ||
| 66 | static SceneNode* load_scene( | ||
| 67 | Game* game, State* state, const char* scene_filepath) { | ||
| 68 | assert(game); | ||
| 69 | assert(game->gfx); | ||
| 70 | assert(state); | ||
| 71 | assert(state->scene); | ||
| 72 | |||
| 73 | SceneNode* root = gfx_get_scene_root(state->scene); | ||
| 74 | RenderBackend* render_backend = gfx_get_render_backend(game->gfx); | ||
| 75 | |||
| 76 | Camera* camera = gfx_get_camera_camera(state->camera); | ||
| 77 | spatial3_set_position(&camera->spatial, vec3_make(0, 0, 2)); | ||
| 78 | |||
| 79 | SceneNode* sky_light_node = load_skyquad(render_backend, root); | ||
| 80 | if (!sky_light_node) { | ||
| 81 | return 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | SceneNode* scene_node = gfx_load_scene( | ||
| 85 | game->gfx, sky_light_node, | ||
| 86 | &(LoadSceneCmd){.origin = SceneFromFile, .filepath = scene_filepath}); | ||
| 87 | if (!scene_node) { | ||
| 88 | return 0; | ||
| 89 | } | ||
| 90 | |||
| 91 | gfx_log_node_hierarchy(root); | ||
| 92 | |||
| 93 | return scene_node; | ||
| 94 | } | ||
| 95 | |||
| 96 | bool init(Game* game, State** pp_state) { | ||
| 97 | assert(game); | ||
| 98 | |||
| 99 | State* state = calloc(1, sizeof(State)); | ||
| 100 | if (!state) { | ||
| 101 | goto cleanup; | ||
| 102 | } | ||
| 103 | |||
| 104 | if (!(state->scene = gfx_make_scene())) { | ||
| 105 | goto cleanup; | ||
| 106 | } | ||
| 107 | if (!(state->camera = gfx_make_camera())) { | ||
| 108 | goto cleanup; | ||
| 109 | } | ||
| 110 | |||
| 111 | const int argc = game->argc; | ||
| 112 | const char** argv = game->argv; | ||
| 113 | |||
| 114 | // Usage: <scene file> | ||
| 115 | const char* scene_filepath = argc > 1 ? argv[1] : DEFAULT_SCENE_FILE; | ||
| 116 | |||
| 117 | SceneNode* node = load_scene(game, state, scene_filepath); | ||
| 118 | if (!node) { | ||
| 119 | goto cleanup; | ||
| 120 | } | ||
| 121 | Anima* anima = gfx_get_node_anima(node); | ||
| 122 | gfx_play_animation( | ||
| 123 | anima, &(AnimationPlaySettings){.name = "Walk", .loop = true}); | ||
| 124 | |||
| 125 | *pp_state = state; | ||
| 126 | return true; | ||
| 127 | |||
| 128 | cleanup: | ||
| 129 | shutdown(game, state); | ||
| 130 | if (state) { | ||
| 131 | free(state); | ||
| 132 | } | ||
| 133 | return false; | ||
| 134 | } | ||
| 135 | |||
| 136 | void shutdown(Game* game, State* 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 | |||
| 145 | void update(Game* game, State* state, double t, double dt) { | ||
| 146 | assert(game); | ||
| 147 | assert(state); | ||
| 148 | assert(state->scene); | ||
| 149 | assert(state->camera); | ||
| 150 | |||
| 151 | gfx_animate_scene(state->scene, (R)t); | ||
| 152 | |||
| 153 | const vec3 orbit_point = vec3_make(0, 2, 0); | ||
| 154 | Camera* camera = gfx_get_camera_camera(state->camera); | ||
| 155 | spatial3_orbit( | ||
| 156 | &camera->spatial, orbit_point, | ||
| 157 | /*radius=*/2.5, | ||
| 158 | /*azimuth=*/t * 0.5, /*zenith=*/0); | ||
| 159 | spatial3_lookat(&camera->spatial, orbit_point); | ||
| 160 | } | ||
| 161 | |||
| 162 | /// Render the bounding boxes of all scene objects. | ||
| 163 | static void render_bounding_boxes(ImmRenderer* imm, const SceneNode* node) { | ||
| 164 | if (gfx_get_node_type(node) == ObjectNode) { | ||
| 165 | // TODO: Look at the scene log. The JointNodes are detached from the | ||
| 166 | // ObjectNodes. This is why the boxes are not being transformed as expected | ||
| 167 | // here. Anima needs to animate boxes? Use OOBB in addition to AABB? | ||
| 168 | const mat4 model = gfx_get_node_global_transform(node); | ||
| 169 | const SceneObject* obj = gfx_get_node_object(node); | ||
| 170 | const aabb3 box = gfx_calc_object_aabb(obj); | ||
| 171 | gfx_imm_set_model_matrix(imm, &model); | ||
| 172 | gfx_imm_draw_aabb(imm, box); | ||
| 173 | } | ||
| 174 | |||
| 175 | // Render children's boxes. | ||
| 176 | for (NodeIter it = gfx_get_node_child(node); it; | ||
| 177 | it = gfx_get_next_child(it)) { | ||
| 178 | render_bounding_boxes(imm, gfx_get_iter_node(it)); | ||
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | void render(const Game* game, const State* state) { | ||
| 183 | assert(state); | ||
| 184 | assert(game); | ||
| 185 | assert(game->gfx); | ||
| 186 | assert(state->scene); | ||
| 187 | assert(state->camera); | ||
| 188 | |||
| 189 | ImmRenderer* imm = gfx_get_imm_renderer(game->gfx); | ||
| 190 | assert(imm); | ||
| 191 | gfx_imm_start(imm); | ||
| 192 | gfx_imm_set_camera(imm, gfx_get_camera_camera(state->camera)); | ||
| 193 | gfx_imm_set_colour(imm, vec4_make(0.2, 0.2, 1.0, 0.3)); | ||
| 194 | render_bounding_boxes(imm, gfx_get_scene_root(state->scene)); | ||
| 195 | gfx_imm_end(imm); | ||
| 196 | } | ||
