models_animation.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load 3d model with animations and play them
  4. *
  5. * This example has been created using raylib 2.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2019 Ramon Santamaria (@raysan5) and @culacant
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main(void)
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. const int screenWidth = 800;
  17. const int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
  19. // Define the camera to look into our 3d world
  20. Camera camera = { 0 };
  21. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  22. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  23. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  24. camera.fovy = 45.0f; // Camera field-of-view Y
  25. camera.type = CAMERA_PERSPECTIVE; // Camera mode type
  26. Model model = LoadModel("resources/guy/guy.iqm"); // Load the animated model mesh and basic data
  27. Texture2D texture = LoadTexture("resources/guy/guytex.png"); // Load model texture and set material
  28. SetMaterialTexture(&model.materials[0], MAP_DIFFUSE, texture); // Set model material map texture
  29. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  30. // Load animation data
  31. int animsCount = 0;
  32. ModelAnimation *anims = LoadModelAnimations("resources/guy/guyanim.iqm", &animsCount);
  33. int animFrameCounter = 0;
  34. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. UpdateCamera(&camera);
  43. // Play animation when spacebar is held down
  44. if (IsKeyDown(KEY_SPACE))
  45. {
  46. animFrameCounter++;
  47. UpdateModelAnimation(model, anims[0], animFrameCounter);
  48. if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
  49. }
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. BeginMode3D(camera);
  56. DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
  57. for (int i = 0; i < model.boneCount; i++)
  58. {
  59. DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
  60. }
  61. DrawGrid(10, 1.0f); // Draw a grid
  62. EndMode3D();
  63. DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, MAROON);
  64. DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, GRAY);
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. // Unload model animations data
  71. for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
  72. UnloadModel(model); // Unload model
  73. CloseWindow(); // Close window and OpenGL context
  74. //--------------------------------------------------------------------------------------
  75. return 0;
  76. }