You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load 3d model with animations and play them
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 3.5
  6. *
  7. * Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2019-2024 Culacant (@culacant) and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************
  15. *
  16. * NOTE: To export a model from blender, make sure it is not posed, the vertices need to be
  17. * in the same position as they would be in edit mode and the scale of your models is
  18. * set to 0. Scaling can be done from the export menu.
  19. *
  20. ********************************************************************************************/
  21. #include "raylib.h"
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
  32. // Define the camera to look into our 3d world
  33. Camera camera = { 0 };
  34. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  35. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  36. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  37. camera.fovy = 45.0f; // Camera field-of-view Y
  38. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  39. Model model = LoadModel("resources/models/iqm/guy.iqm"); // Load the animated model mesh and basic data
  40. Texture2D texture = LoadTexture("resources/models/iqm/guytex.png"); // Load model texture and set material
  41. SetMaterialTexture(&model.materials[0], MATERIAL_MAP_DIFFUSE, texture); // Set model material map texture
  42. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  43. // Load animation data
  44. int animsCount = 0;
  45. ModelAnimation *anims = LoadModelAnimations("resources/models/iqm/guyanim.iqm", &animsCount);
  46. int animFrameCounter = 0;
  47. DisableCursor(); // Catch cursor
  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_FIRST_PERSON);
  56. // Play animation when spacebar is held down
  57. if (IsKeyDown(KEY_SPACE))
  58. {
  59. animFrameCounter++;
  60. UpdateModelAnimation(model, anims[0], animFrameCounter);
  61. if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
  62. }
  63. //----------------------------------------------------------------------------------
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(RAYWHITE);
  68. BeginMode3D(camera);
  69. DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
  70. for (int i = 0; i < model.boneCount; i++)
  71. {
  72. DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
  73. }
  74. DrawGrid(10, 1.0f); // Draw a grid
  75. EndMode3D();
  76. DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, MAROON);
  77. DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, GRAY);
  78. EndDrawing();
  79. //----------------------------------------------------------------------------------
  80. }
  81. // De-Initialization
  82. //--------------------------------------------------------------------------------------
  83. UnloadTexture(texture); // Unload texture
  84. UnloadModelAnimations(anims, animsCount); // Unload model animations data
  85. UnloadModel(model); // Unload model
  86. CloseWindow(); // Close window and OpenGL context
  87. //--------------------------------------------------------------------------------------
  88. return 0;
  89. }