Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

108 строки
4.0 KiB

3 лет назад
4 лет назад
3 лет назад
3 лет назад
4 лет назад
3 лет назад
4 лет назад
3 лет назад
4 лет назад
3 лет назад
  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 8
  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");
  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. model[6] = LoadModel("resources/gltf/vertex_colored_object.glb");
  44. model[7] = LoadModel("resources/gltf/girl.glb");
  45. int currentModel = 0;
  46. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  47. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. UpdateCamera(&camera);
  56. if (IsKeyReleased(KEY_RIGHT))
  57. {
  58. currentModel++;
  59. if (currentModel == MAX_MODELS) currentModel = 0;
  60. }
  61. if (IsKeyReleased(KEY_LEFT))
  62. {
  63. currentModel--;
  64. if (currentModel < 0) currentModel = MAX_MODELS - 1;
  65. }
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(SKYBLUE);
  70. BeginMode3D(camera);
  71. DrawModelEx(model[currentModel], position, (Vector3){ 0.0f, 1.0f, 0.0f }, 180.0f, (Vector3){ 2.0f, 2.0f, 2.0f }, WHITE);
  72. DrawGrid(10, 1.0f); // Draw a grid
  73. EndMode3D();
  74. EndDrawing();
  75. //----------------------------------------------------------------------------------
  76. }
  77. // De-Initialization
  78. //--------------------------------------------------------------------------------------
  79. for (int i = 0; i < MAX_MODELS; i++) UnloadModel(model[i]); // Unload models
  80. CloseWindow(); // Close window and OpenGL context
  81. //--------------------------------------------------------------------------------------
  82. return 0;
  83. }