You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.3 KiB

5 months ago
5 months ago
5 months ago
5 months ago
3 years ago
5 months ago
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - loading gltf with animations
  4. *
  5. * LIMITATIONS:
  6. * - Only supports 1 armature per file, and skips loading it if there are multiple armatures
  7. * - Only supports linear interpolation (default method in Blender when checked
  8. * "Always Sample Animations" when exporting a GLTF file)
  9. * - Only supports translation/rotation/scale animation channel.path,
  10. * weights not considered (i.e. morph targets)
  11. *
  12. * Example originally created with raylib 3.7, last time updated with raylib 4.2
  13. *
  14. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  15. * BSD-like license that allows static linking with closed source software
  16. *
  17. * Copyright (c) 2020-2024 Ramon Santamaria (@raysan5)
  18. *
  19. ********************************************************************************************/
  20. #include "raylib.h"
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [models] example - loading gltf animations");
  31. // Define the camera to look into our 3d world
  32. Camera camera = { 0 };
  33. camera.position = (Vector3){ 6.0f, 6.0f, 6.0f }; // Camera position
  34. camera.target = (Vector3){ 0.0f, 2.0f, 0.0f }; // Camera looking at point
  35. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  36. camera.fovy = 45.0f; // Camera field-of-view Y
  37. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  38. // Load gltf model
  39. Model model = LoadModel("resources/models/gltf/robot.glb");
  40. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  41. // Load gltf model animations
  42. int animsCount = 0;
  43. unsigned int animIndex = 0;
  44. unsigned int animCurrentFrame = 0;
  45. ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/robot.glb", &animsCount);
  46. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. UpdateCamera(&camera, CAMERA_ORBITAL);
  54. // Select current animation
  55. if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) animIndex = (animIndex + 1)%animsCount;
  56. else if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) animIndex = (animIndex + animsCount - 1)%animsCount;
  57. // Update model animation
  58. ModelAnimation anim = modelAnimations[animIndex];
  59. animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
  60. UpdateModelAnimation(model, anim, animCurrentFrame);
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. BeginMode3D(camera);
  67. DrawModel(model, position, 1.0f, WHITE); // Draw animated model
  68. DrawGrid(10, 1.0f);
  69. EndMode3D();
  70. DrawText("Use the LEFT/RIGHT mouse buttons to switch animation", 10, 10, 20, GRAY);
  71. DrawText(TextFormat("Animation: %s", anim.name), 10, GetScreenHeight() - 20, 10, DARKGRAY);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. UnloadModel(model); // Unload model and meshes/material
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }