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.

118 lines
4.9 KiB

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