diff options
Diffstat (limited to 'src/plugins')
| -rw-r--r-- | src/plugins/viewer.c | 57 |
1 files changed, 36 insertions, 21 deletions
diff --git a/src/plugins/viewer.c b/src/plugins/viewer.c index 84fcca4..97c718e 100644 --- a/src/plugins/viewer.c +++ b/src/plugins/viewer.c | |||
| @@ -2,11 +2,14 @@ | |||
| 2 | 2 | ||
| 3 | #include <gfx/app.h> | 3 | #include <gfx/app.h> |
| 4 | #include <gfx/asset.h> | 4 | #include <gfx/asset.h> |
| 5 | #include <gfx/llr/llr.h> | ||
| 5 | #include <gfx/renderer.h> | 6 | #include <gfx/renderer.h> |
| 7 | #include <gfx/renderer/imm_renderer.h> | ||
| 6 | #include <gfx/scene.h> | 8 | #include <gfx/scene.h> |
| 7 | #include <gfx/util/skyquad.h> | 9 | #include <gfx/util/skyquad.h> |
| 8 | #include <math/camera.h> | 10 | #include <math/camera.h> |
| 9 | #include <math/spatial3.h> | 11 | #include <math/spatial3.h> |
| 12 | #include <math/vec2.h> | ||
| 10 | 13 | ||
| 11 | #include <log/log.h> | 14 | #include <log/log.h> |
| 12 | 15 | ||
| @@ -34,6 +37,10 @@ static const char* FLIGHT_HELMET = | |||
| 34 | static const char* DAMAGED_HELMET = | 37 | static const char* DAMAGED_HELMET = |
| 35 | "/home/jeanne/Nextcloud/assets/glTF-Sample-Models/2.0/DamagedHelmet/glTF/" | 38 | "/home/jeanne/Nextcloud/assets/glTF-Sample-Models/2.0/DamagedHelmet/glTF/" |
| 36 | "DamagedHelmet.gltf"; | 39 | "DamagedHelmet.gltf"; |
| 40 | static const char* CHRONOGRAPH_WATCH = | ||
| 41 | "/home/jeanne/Nextcloud/assets/glTF-Sample-Models/2.0/ChronographWatch/" | ||
| 42 | "glTF/" | ||
| 43 | "ChronographWatch.gltf"; | ||
| 37 | static const char* GIRL = | 44 | static const char* GIRL = |
| 38 | "/home/jeanne/Nextcloud/assets/models/girl/girl-with-ground.gltf"; | 45 | "/home/jeanne/Nextcloud/assets/models/girl/girl-with-ground.gltf"; |
| 39 | static const char* BOXES = | 46 | static const char* BOXES = |
| @@ -41,7 +48,7 @@ static const char* BOXES = | |||
| 41 | 48 | ||
| 42 | #define DEFAULT_SCENE_FILE GIRL | 49 | #define DEFAULT_SCENE_FILE GIRL |
| 43 | 50 | ||
| 44 | static const bool RenderBoundingBoxes = false; | 51 | static const bool RenderBoundingBoxes = true; |
| 45 | static const R DefaultCameraSpeed = (R)6.0; | 52 | static const R DefaultCameraSpeed = (R)6.0; |
| 46 | static const R DefaultMouseSensitivity = (R)(10 * TO_RAD); | 53 | static const R DefaultMouseSensitivity = (R)(10 * TO_RAD); |
| 47 | static const vec3 DefaultCameraPosition = (vec3){0, 2, 5}; | 54 | static const vec3 DefaultCameraPosition = (vec3){0, 2, 5}; |
| @@ -52,13 +59,13 @@ typedef struct CameraCommand { | |||
| 52 | bool CameraMoveForward : 1; | 59 | bool CameraMoveForward : 1; |
| 53 | bool CameraMoveBackward : 1; | 60 | bool CameraMoveBackward : 1; |
| 54 | bool CameraIsRotating : 1; // When true, subsequent mouse movements cause the | 61 | bool CameraIsRotating : 1; // When true, subsequent mouse movements cause the |
| 55 | // camera to rotate. | 62 | // camera to rotate. |
| 56 | } CameraCommand; | 63 | } CameraCommand; |
| 57 | 64 | ||
| 58 | typedef struct CameraController { | 65 | typedef struct CameraController { |
| 59 | R camera_speed; // Camera movement speed. | 66 | R camera_speed; // Camera movement speed. |
| 60 | R mouse_sensitivity; // Controls the degree with which mouse movements | 67 | R mouse_sensitivity; // Controls the degree with which mouse movements |
| 61 | // rotate the camera. | 68 | // rotate the camera. |
| 62 | vec2 prev_mouse_position; // Mouse position in the previous frame. | 69 | vec2 prev_mouse_position; // Mouse position in the previous frame. |
| 63 | } CameraController; | 70 | } CameraController; |
| 64 | 71 | ||
| @@ -95,7 +102,7 @@ static SceneNode* load_skyquad(Gfx* gfx, SceneNode* root) { | |||
| 95 | 102 | ||
| 96 | const Texture* environment_map = load_environment_map(gfx); | 103 | const Texture* environment_map = load_environment_map(gfx); |
| 97 | if (!environment_map) { | 104 | if (!environment_map) { |
| 98 | return 0; | 105 | return nullptr; |
| 99 | } | 106 | } |
| 100 | 107 | ||
| 101 | return gfx_setup_skyquad(gfxcore, root, environment_map); | 108 | return gfx_setup_skyquad(gfxcore, root, environment_map); |
| @@ -114,18 +121,18 @@ static Model* load_model(Game* game, State* state, const char* scene_filepath) { | |||
| 114 | SceneNode* root = gfx_get_scene_root(state->scene); | 121 | SceneNode* root = gfx_get_scene_root(state->scene); |
| 115 | SceneNode* sky_light_node = load_skyquad(game->gfx, root); | 122 | SceneNode* sky_light_node = load_skyquad(game->gfx, root); |
| 116 | if (!sky_light_node) { | 123 | if (!sky_light_node) { |
| 117 | return 0; // test | 124 | return nullptr; |
| 118 | } | 125 | } |
| 119 | 126 | ||
| 120 | Model* model = gfx_load_model( | 127 | Model* model = gfx_load_model( |
| 121 | game->gfx, &(LoadModelCmd){.origin = AssetFromFile, | 128 | game->gfx, &(LoadModelCmd){.origin = AssetFromFile, |
| 122 | .filepath = mstring_make(scene_filepath)}); | 129 | .filepath = mstring_make(scene_filepath)}); |
| 123 | if (!model) { | 130 | if (!model) { |
| 124 | return 0; | 131 | return nullptr; |
| 125 | } | 132 | } |
| 126 | SceneNode* model_node = gfx_make_model_node(model); | 133 | SceneNode* model_node = gfx_make_model_node(model); |
| 127 | if (!model_node) { | 134 | if (!model_node) { |
| 128 | return 0; | 135 | return nullptr; |
| 129 | } | 136 | } |
| 130 | gfx_set_node_parent(model_node, sky_light_node); | 137 | gfx_set_node_parent(model_node, sky_light_node); |
| 131 | 138 | ||
| @@ -146,10 +153,10 @@ bool init(Game* game, State** pp_state) { | |||
| 146 | goto cleanup; | 153 | goto cleanup; |
| 147 | } | 154 | } |
| 148 | 155 | ||
| 149 | if (!(state->scene = gfx_make_scene())) { | 156 | if (!((state->scene = gfx_make_scene()))) { |
| 150 | goto cleanup; | 157 | goto cleanup; |
| 151 | } | 158 | } |
| 152 | if (!(state->camera = gfx_make_camera())) { | 159 | if (!((state->camera = gfx_make_camera()))) { |
| 153 | goto cleanup; | 160 | goto cleanup; |
| 154 | } | 161 | } |
| 155 | 162 | ||
| @@ -267,8 +274,9 @@ void update(Game* game, State* state, double t, double dt) { | |||
| 267 | 274 | ||
| 268 | /// Render the bounding boxes of all scene objects. | 275 | /// Render the bounding boxes of all scene objects. |
| 269 | static void render_bounding_boxes_rec( | 276 | static void render_bounding_boxes_rec( |
| 270 | ImmRenderer* imm, const Anima* anima, const mat4* parent_model_matrix, | 277 | LLR* llr, ImmRenderer* imm, const Anima* anima, |
| 271 | const SceneNode* node) { | 278 | const mat4* parent_model_matrix, const SceneNode* node) { |
| 279 | assert(llr); | ||
| 272 | assert(imm); | 280 | assert(imm); |
| 273 | assert(node); | 281 | assert(node); |
| 274 | 282 | ||
| @@ -280,16 +288,17 @@ static void render_bounding_boxes_rec( | |||
| 280 | if (node_type == ModelNode) { | 288 | if (node_type == ModelNode) { |
| 281 | const Model* model = gfx_get_node_model(node); | 289 | const Model* model = gfx_get_node_model(node); |
| 282 | const SceneNode* root = gfx_get_model_root(model); | 290 | const SceneNode* root = gfx_get_model_root(model); |
| 283 | render_bounding_boxes_rec(imm, anima, &model_matrix, root); | 291 | render_bounding_boxes_rec(llr, imm, anima, &model_matrix, root); |
| 284 | } else if (node_type == AnimaNode) { | 292 | } else if (node_type == AnimaNode) { |
| 285 | anima = gfx_get_node_anima(node); | 293 | anima = gfx_get_node_anima(node); |
| 286 | } else if (node_type == ObjectNode) { | 294 | } else if (node_type == ObjectNode) { |
| 287 | gfx_imm_set_model_matrix(imm, &model_matrix); | 295 | gfx_llr_set_model_matrix(llr, &model_matrix); |
| 288 | 296 | ||
| 289 | const SceneObject* obj = gfx_get_node_object(node); | 297 | const SceneObject* obj = gfx_get_node_object(node); |
| 290 | const Skeleton* skeleton = gfx_get_object_skeleton(obj); | 298 | const Skeleton* skeleton = gfx_get_object_skeleton(obj); |
| 291 | 299 | ||
| 292 | if (skeleton) { // Animated model. | 300 | if (skeleton) { |
| 301 | // Animated model. | ||
| 293 | assert(anima); | 302 | assert(anima); |
| 294 | const size_t num_joints = gfx_get_skeleton_num_joints(skeleton); | 303 | const size_t num_joints = gfx_get_skeleton_num_joints(skeleton); |
| 295 | for (size_t i = 0; i < num_joints; ++i) { | 304 | for (size_t i = 0; i < num_joints; ++i) { |
| @@ -298,16 +307,19 @@ static void render_bounding_boxes_rec( | |||
| 298 | gfx_imm_draw_box3(imm, box.vertices); | 307 | gfx_imm_draw_box3(imm, box.vertices); |
| 299 | } | 308 | } |
| 300 | } | 309 | } |
| 301 | } else { // Static model. | 310 | } else { |
| 311 | // Static model. | ||
| 302 | const aabb3 box = gfx_get_object_aabb(obj); | 312 | const aabb3 box = gfx_get_object_aabb(obj); |
| 303 | gfx_imm_draw_aabb3(imm, box); | 313 | gfx_imm_draw_aabb3(imm, box); |
| 304 | } | 314 | } |
| 315 | |||
| 316 | gfx_imm_flush(imm); | ||
| 305 | } | 317 | } |
| 306 | 318 | ||
| 307 | // Render children's boxes. | 319 | // Render children's boxes. |
| 308 | const SceneNode* child = gfx_get_node_child(node); | 320 | const SceneNode* child = gfx_get_node_child(node); |
| 309 | while (child) { | 321 | while (child) { |
| 310 | render_bounding_boxes_rec(imm, anima, &model_matrix, child); | 322 | render_bounding_boxes_rec(llr, imm, anima, &model_matrix, child); |
| 311 | child = gfx_get_node_sibling(child); | 323 | child = gfx_get_node_sibling(child); |
| 312 | } | 324 | } |
| 313 | } | 325 | } |
| @@ -318,8 +330,10 @@ static void render_bounding_boxes(const Game* game, const State* state) { | |||
| 318 | assert(state); | 330 | assert(state); |
| 319 | 331 | ||
| 320 | GfxCore* gfxcore = gfx_get_core(game->gfx); | 332 | GfxCore* gfxcore = gfx_get_core(game->gfx); |
| 333 | LLR* llr = gfx_get_llr(game->gfx); | ||
| 321 | ImmRenderer* imm = gfx_get_imm_renderer(game->gfx); | 334 | ImmRenderer* imm = gfx_get_imm_renderer(game->gfx); |
| 322 | assert(gfxcore); | 335 | assert(gfxcore); |
| 336 | assert(llr); | ||
| 323 | assert(imm); | 337 | assert(imm); |
| 324 | 338 | ||
| 325 | const mat4 id = mat4_id(); | 339 | const mat4 id = mat4_id(); |
| @@ -330,9 +344,10 @@ static void render_bounding_boxes(const Game* game, const State* state) { | |||
| 330 | gfx_set_polygon_offset(gfxcore, -1.5f, -1.0f); | 344 | gfx_set_polygon_offset(gfxcore, -1.5f, -1.0f); |
| 331 | 345 | ||
| 332 | gfx_imm_start(imm); | 346 | gfx_imm_start(imm); |
| 333 | gfx_imm_set_camera(imm, gfx_get_camera_camera(state->camera)); | 347 | gfx_llr_set_camera(llr, gfx_get_camera_camera(state->camera)); |
| 334 | gfx_imm_set_colour(imm, vec4_make(0.3, 0.3, 0.9, 0.1)); | 348 | gfx_imm_set_colour(imm, vec4_make(0.3f, 0.3f, 0.9f, 0.1f)); |
| 335 | render_bounding_boxes_rec(imm, anima, &id, gfx_get_scene_root(state->scene)); | 349 | render_bounding_boxes_rec( |
| 350 | llr, imm, anima, &id, gfx_get_scene_root(state->scene)); | ||
| 336 | gfx_imm_end(imm); | 351 | gfx_imm_end(imm); |
| 337 | 352 | ||
| 338 | gfx_reset_polygon_offset(gfxcore); | 353 | gfx_reset_polygon_offset(gfxcore); |
