models_loading_gltf.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load models gltf
  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. * NOTE: To export a model from Blender, make sure it is not posed, the vertices need to be
  9. * in the same position as they would be in edit mode.
  10. * Also make sure the scale parameter of your models is set to 0.0,
  11. * scaling can be applied from the export menu.
  12. *
  13. * Example contributed by Hristo Stamenov (@object71) and reviewed by Ramon Santamaria (@raysan5)
  14. *
  15. * Copyright (c) 2021 Hristo Stamenov (@object71) and Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #define MAX_GLTF_MODELS 8
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [models] example - model");
  27. // Define the camera to look into our 3d world
  28. Camera camera = { 0 };
  29. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  30. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  31. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  32. camera.fovy = 45.0f; // Camera field-of-view Y
  33. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  34. // Load some models
  35. Model model[MAX_GLTF_MODELS] = { 0 };
  36. model[0] = LoadModel("resources/models/gltf/raylib_32x32.glb");
  37. model[1] = LoadModel("resources/models/gltf/rigged_figure.glb");
  38. model[2] = LoadModel("resources/models/gltf/GearboxAssy.glb");
  39. model[3] = LoadModel("resources/models/gltf/BoxAnimated.glb");
  40. model[4] = LoadModel("resources/models/gltf/AnimatedTriangle.gltf");
  41. model[5] = LoadModel("resources/models/gltf/AnimatedMorphCube.glb");
  42. model[6] = LoadModel("resources/models/gltf/vertex_colored_object.glb");
  43. model[7] = LoadModel("resources/models/gltf/girl.glb");
  44. int currentModel = 0;
  45. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  46. SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. UpdateCamera(&camera); // Update our camera with inputs
  55. if (IsKeyReleased(KEY_RIGHT))
  56. {
  57. currentModel++;
  58. if (currentModel == MAX_GLTF_MODELS) currentModel = 0;
  59. }
  60. if (IsKeyReleased(KEY_LEFT))
  61. {
  62. currentModel--;
  63. if (currentModel < 0) currentModel = MAX_GLTF_MODELS - 1;
  64. }
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(SKYBLUE);
  70. BeginMode3D(camera);
  71. DrawModel(model[currentModel], position, 1.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_GLTF_MODELS; i++) UnloadModel(model[i]); // Unload models
  80. CloseWindow(); // Close window and OpenGL context
  81. //--------------------------------------------------------------------------------------
  82. return 0;
  83. }