Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

104 rader
4.4 KiB

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