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.

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