2
0

models_loading_gltf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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-2023 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");
  31. // Define the camera to look into our 3d world
  32. Camera camera = { 0 };
  33. camera.position = (Vector3){ 5.0f, 5.0f, 5.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. // Load gltf model animations
  41. unsigned int animsCount = 0;
  42. unsigned int animIndex = 0;
  43. unsigned int animCurrentFrame = 0;
  44. ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/robot.glb", &animsCount);
  45. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  46. DisableCursor(); // Limit cursor to relative movement inside the window
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. UpdateCamera(&camera, CAMERA_THIRD_PERSON);
  55. // Select current animation
  56. if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) animIndex = (animIndex + 1)%animsCount;
  57. else if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) animIndex = (animIndex + animsCount - 1)%animsCount;
  58. // Update model animation
  59. ModelAnimation anim = modelAnimations[animIndex];
  60. animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
  61. UpdateModelAnimation(model, anim, animCurrentFrame);
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. BeginMode3D(camera);
  68. DrawModel(model, position, 1.0f, WHITE); // Draw animated model
  69. DrawGrid(10, 1.0f);
  70. EndMode3D();
  71. DrawText("Use the LEFT/RIGHT mouse buttons to switch animation", 10, 10, 20, GRAY);
  72. DrawText(TextFormat("Animation: %s", anim.name), 10, GetScreenHeight() - 20, 10, DARKGRAY);
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. UnloadModel(model); // Unload model and meshes/material
  79. CloseWindow(); // Close window and OpenGL context
  80. //--------------------------------------------------------------------------------------
  81. return 0;
  82. }