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.

113 lines
4.6 KiB

  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. * Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2019 Culacant (@culacant) 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 <stdlib.h>
  20. #include "raylib.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.type = CAMERA_PERSPECTIVE; // Camera mode type
  35. Model model = LoadModel("resources/guy/guy.iqm"); // Load the animated model mesh and basic data
  36. Texture2D texture = LoadTexture("resources/guy/guytex.png"); // Load model texture and set material
  37. SetMaterialTexture(&model.materials[0], MAP_DIFFUSE, texture); // Set model material map texture
  38. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  39. // Load animation data
  40. int animsCount = 0;
  41. ModelAnimation *anims = LoadModelAnimations("resources/guy/guyanim.iqm", &animsCount);
  42. int animFrameCounter = 0;
  43. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. UpdateCamera(&camera);
  52. // Play animation when spacebar is held down
  53. if (IsKeyDown(KEY_SPACE))
  54. {
  55. animFrameCounter++;
  56. UpdateModelAnimation(model, anims[0], animFrameCounter);
  57. if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
  58. }
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. BeginMode3D(camera);
  65. DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
  66. for (int i = 0; i < model.boneCount; i++)
  67. {
  68. DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
  69. }
  70. DrawGrid(10, 1.0f); // Draw a grid
  71. EndMode3D();
  72. DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, MAROON);
  73. DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, GRAY);
  74. EndDrawing();
  75. //----------------------------------------------------------------------------------
  76. }
  77. // De-Initialization
  78. //--------------------------------------------------------------------------------------
  79. UnloadTexture(texture); // Unload texture
  80. // Unload model animations data
  81. for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
  82. RL_FREE(anims);
  83. UnloadModel(model); // Unload model
  84. CloseWindow(); // Close window and OpenGL context
  85. //--------------------------------------------------------------------------------------
  86. return 0;
  87. }