您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

106 行
3.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load 3d gltf model
  4. *
  5. * This example has been created using raylib 3.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Hristo Stamenov (@object71) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2021 Hristo Stamenov (@object71) 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 "raylib.h"
  20. #include <stdlib.h>
  21. #define MAX_MODELS 6
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
  29. // Define the camera to look into our 3d world
  30. Camera camera = { 0 };
  31. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  32. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  33. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  34. camera.fovy = 45.0f; // Camera field-of-view Y
  35. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  36. Model model[MAX_MODELS] = { 0 };
  37. model[0] = LoadModel("resources/gltf/raylib_32x32.glb");
  38. model[1] = LoadModel("resources/gltf/rigged_figure.glb");
  39. model[2] = LoadModel("resources/gltf/GearboxAssy.glb");
  40. model[3] = LoadModel("resources/gltf/BoxAnimated.glb");
  41. model[4] = LoadModel("resources/gltf/AnimatedTriangle.gltf");
  42. model[5] = LoadModel("resources/gltf/AnimatedMorphCube.glb");
  43. int currentModel = 0;
  44. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  45. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  46. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. UpdateCamera(&camera);
  54. if (IsKeyReleased(KEY_RIGHT))
  55. {
  56. currentModel++;
  57. if (currentModel == MAX_MODELS) currentModel = 0;
  58. }
  59. if (IsKeyReleased(KEY_LEFT))
  60. {
  61. currentModel--;
  62. if (currentModel < 0) currentModel = MAX_MODELS - 1;
  63. }
  64. // Draw
  65. //----------------------------------------------------------------------------------
  66. BeginDrawing();
  67. ClearBackground(SKYBLUE);
  68. BeginMode3D(camera);
  69. DrawModelEx(model[currentModel], position, (Vector3){ 0.0f, 1.0f, 0.0f }, 180.0f, (Vector3){ 2.0f, 2.0f, 2.0f }, WHITE);
  70. DrawGrid(10, 1.0f); // Draw a grid
  71. EndMode3D();
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. for(int i = 0; i < MAX_MODELS; i++) UnloadModel(model[i]); // Unload models
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }