models_animation.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load 3d model with animations and play them
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 3.5
  8. *
  9. * Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2019-2024 Culacant (@culacant) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************
  17. *
  18. * NOTE: To export a model from blender, make sure it is not posed, the vertices need to be
  19. * in the same position as they would be in edit mode and the scale of your models is
  20. * set to 0. Scaling can be done from the export menu.
  21. *
  22. ********************************************************************************************/
  23. #include "raylib.h"
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
  34. // Define the camera to look into our 3d world
  35. Camera camera = { 0 };
  36. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  37. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  38. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  39. camera.fovy = 45.0f; // Camera field-of-view Y
  40. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  41. Model model = LoadModel("resources/models/iqm/guy.iqm"); // Load the animated model mesh and basic data
  42. Texture2D texture = LoadTexture("resources/models/iqm/guytex.png"); // Load model texture and set material
  43. SetMaterialTexture(&model.materials[0], MATERIAL_MAP_DIFFUSE, texture); // Set model material map texture
  44. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  45. // Load animation data
  46. int animsCount = 0;
  47. ModelAnimation *anims = LoadModelAnimations("resources/models/iqm/guyanim.iqm", &animsCount);
  48. int animFrameCounter = 0;
  49. DisableCursor(); // Catch cursor
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  58. // Play animation when spacebar is held down
  59. if (IsKeyDown(KEY_SPACE))
  60. {
  61. animFrameCounter++;
  62. UpdateModelAnimation(model, anims[0], animFrameCounter);
  63. if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
  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. DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, 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("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, GRAY);
  80. EndDrawing();
  81. //----------------------------------------------------------------------------------
  82. }
  83. // De-Initialization
  84. //--------------------------------------------------------------------------------------
  85. UnloadTexture(texture); // Unload texture
  86. UnloadModelAnimations(anims, animsCount); // Unload model animations data
  87. UnloadModel(model); // Unload model
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }