Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

122 linhas
4.8 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load 3d gltf model with animations and play them
  4. *
  5. * This example has been created using raylib 3.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Hristo Stamenov (@object71) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2021 Hristo Stamenov (@object71) and Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************
  13. *
  14. * To export a model from blender, make sure it is not posed, the vertices need to be in the
  15. * same position as they would be in edit mode.
  16. * and that the scale of your models is set to 0. Scaling can be done from the export menu.
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. #include <stdlib.h>
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
  28. // Define the camera to look into our 3d world
  29. Camera camera = { 0 };
  30. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  31. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  32. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  33. camera.fovy = 45.0f; // Camera field-of-view Y
  34. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  35. Model model = LoadModel("resources/gltf/rigged_figure.glb"); // Load the animated model mesh and
  36. // basic data
  37. // Texture2D texture = LoadTexture("resources/guy/guytex.png"); // Load model texture and set material
  38. // SetMaterialTexture(&model.materials[0], MAP_DIFFUSE, texture); // Set model material map texture
  39. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  40. // Load animation data
  41. int animsCount = 0;
  42. ModelAnimation *anims = LoadModelAnimations("resources/gltf/rigged_figure.glb", &animsCount);
  43. int animFrameCounter = 0;
  44. int animationDirection = 1;
  45. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  46. SetTargetFPS(30); // 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);
  54. // Play animation when spacebar is held down
  55. if (IsKeyDown(KEY_SPACE))
  56. {
  57. animFrameCounter += animationDirection;
  58. if (animFrameCounter >= anims[0].frameCount || animFrameCounter <= 0)
  59. {
  60. animationDirection *= -1;
  61. animFrameCounter += animationDirection;
  62. }
  63. UpdateModelAnimation(model, anims[0], animFrameCounter);
  64. }
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. BeginMode3D(camera);
  71. DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
  72. for (int i = 0; i < model.boneCount; i++)
  73. {
  74. DrawSphere(anims[0].framePoses[animFrameCounter][i].translation, 0.01f, RED);
  75. }
  76. DrawGrid(10, 1.0f); // Draw a grid
  77. EndMode3D();
  78. DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, MAROON);
  79. DrawText("(cc4) Rigged Figure by @Cesium", screenWidth - 200, screenHeight - 20, 10, GRAY);
  80. EndDrawing();
  81. //----------------------------------------------------------------------------------
  82. }
  83. // De-Initialization
  84. //--------------------------------------------------------------------------------------
  85. // UnloadTexture(texture); // Unload texture
  86. // Unload model animations data
  87. for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
  88. RL_FREE(anims);
  89. UnloadModel(model); // Unload model
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }