models_iqm_animation.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load IQM 3d model with animations and play them
  4. *
  5. * This example has been created using raylib 2.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2018 @culacant and @raysan5
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define RIQM_IMPLEMENTATION
  13. #include "riqm.h"
  14. int main()
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [models] example - iqm 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. // Load the animated model mesh and basic data
  29. AnimatedModel model = LoadAnimatedModel("resources/guy.iqm");
  30. // Load model texture and set material
  31. // NOTE: There is only 1 mesh and 1 material (both at index 0), thats what the 2 0's are
  32. model = AnimatedModelAddTexture(model, "resources/guytex.png"); // REPLACE!
  33. model = SetMeshMaterial(model, 0, 0); // REPLACE!
  34. // Load animation data
  35. Animation anim = LoadAnimationFromIQM("resources/guyanim.iqm");
  36. int animFrameCounter = 0;
  37. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. // Main game loop
  41. while (!WindowShouldClose()) // Detect window close button or ESC key
  42. {
  43. // Update
  44. //----------------------------------------------------------------------------------
  45. UpdateCamera(&camera);
  46. // Play animation when spacebar is held down
  47. if (IsKeyDown(KEY_SPACE))
  48. {
  49. animFrameCounter++;
  50. AnimateModel(model, anim, animFrameCounter); // Animate the model with animation data and frame
  51. }
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. BeginMode3D(camera);
  58. DrawAnimatedModel(model, Vector3Zero(), 1.0f, WHITE); // Draw animated model
  59. DrawGrid(10, 1.0f); // Draw a grid
  60. EndMode3D();
  61. DrawText("(c) Guy IQM 3D model by -------", screenWidth - 200, screenHeight - 20, 10, GRAY);
  62. DrawFPS(10, 10);
  63. EndDrawing();
  64. //----------------------------------------------------------------------------------
  65. }
  66. // De-Initialization
  67. //--------------------------------------------------------------------------------------
  68. UnloadAnimation(anim); // Unload animation data
  69. UnloadAnimatedModel(model); // Unload animated model
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }